pi-subagents-lite 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.ts ADDED
@@ -0,0 +1,40 @@
1
+ /**
2
+ * utils.ts — Security helpers: safe file access, name validation.
3
+ *
4
+ * Extracted from upstream memory.ts — pure implementations copied verbatim.
5
+ */
6
+
7
+ import { lstatSync, readFileSync } from "node:fs";
8
+
9
+ /**
10
+ * Returns true if a name contains characters not allowed in agent/skill names.
11
+ * Uses a whitelist: only alphanumeric, hyphens, underscores, and dots (no leading dot).
12
+ */
13
+ export function isUnsafeName(name: string): boolean {
14
+ if (!name || name.length > 128) return true;
15
+ return !/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(name);
16
+ }
17
+
18
+ /**
19
+ * Returns true if the given path is a symlink (defense against symlink attacks).
20
+ */
21
+ export function isSymlink(filePath: string): boolean {
22
+ try {
23
+ return lstatSync(filePath).isSymbolicLink();
24
+ } catch {
25
+ return false;
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Safely read a file, rejecting symlinks.
31
+ * Returns undefined if the file doesn't exist, is a symlink, or can't be read.
32
+ */
33
+ export function safeReadFile(filePath: string): string | undefined {
34
+ try {
35
+ if (lstatSync(filePath).isSymbolicLink()) return undefined;
36
+ return readFileSync(filePath, "utf-8");
37
+ } catch {
38
+ return undefined;
39
+ }
40
+ }