Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/client/src/safety.ts?format=json
152 linesFirst import 97% · infra-split 2% · rename 1%Last change rename in version 100, 3 versions changed it
1// Paths from the server are other agents' work: anyone with a token can
2// publish one. Before sync, switch, join or restore touches the disk, every
3// path in the answer is screened here, so none can write outside the agent's
4// folder, into its .git, .claude or .avcs, onto a Windows device, or through
5// a link out of the folder. A refused path is left alone and named.
6
7import { lstatSync } from "node:fs";
8import { join, posix } from "node:path";
9import { ALWAYS_SKIPPED, isSkippedPart, skippedKey } from "./core.ts";
10import type { Mode } from "./modes.ts";
12/** A path the client would not touch, and why. */
13export interface Refused {
14 readonly path: string;
15 readonly why: string;
18/** Windows device names: a file of that name (with any extension) is the device, in any folder. */
19const DEVICE = /^(con|prn|aux|nul|com[0-9¹²³]|lpt[0-9¹²³]|conin\$|conout\$|clock\$)$/i;
21/**
22 * A Windows short (8.3) name: up to 8 characters with a ~ and a number, and a
23 * short extension. On NTFS it can be another file's second name, so CLAUDE~1/x
24 * is written into .claude/x and GIT~1/x into .git/x (git's
25 * CVE-2019-1353). Git refuses these with core.protectNTFS; so does avcs.
26 */
27const SHORT_NAME = /^[^.~ ]{1,6}~[0-9]{1,6}(\.[^. ]{1,3})?$/;
29/** Characters Windows can't put in a file name: writing one fails, and a sync would stop partway. */
30const WINDOWS_CHARS = /[<>"|?*\x01-\x1f]/;
32/** Why Windows can't hold `part` safely, or undefined. Checked on every platform, as git does. */
33function windowsPart(part: string): string | undefined {
34 const bad = WINDOWS_CHARS.exec(part);
35 if (bad)
36 return `"${part}" has ${bad[0] < " " ? "a control character" : bad[0]}, which Windows can't put in a file name`;
37 if (SHORT_NAME.test(part) && part.split(".")[0]!.length <= 8) {
38 return `"${part}" looks like a Windows short name, which can be another file's second name (CLAUDE~1 is .claude, GIT~1 is .git)`;
39 }
40 return undefined;
43/**
44 * Why a folder-relative path from the server can't be written here, or
45 * undefined when it can. The rules hold on every platform, so every agent
46 * refuses the same paths.
47 */
48export function unsafePath(rel: string): string | undefined {
49 if (typeof rel !== "string" || rel === "") return "it is empty";
50 if (rel.includes("\0")) return "it has a NUL byte";
51 if (rel.includes("\\")) return "it has a backslash, which Windows reads as a folder separator";
52 if (rel.includes(":")) return "it has a colon (a drive letter, or a Windows stream like file::$DATA)";
53 if (rel.startsWith("/")) return "it is absolute";
54 for (const part of rel.split("/")) {
55 if (part === "") return "it has an empty part (//, or a trailing /)";
56 if (part === "." || part === "..") return `it has a "${part}" part`;
57 if (/[. ]$/.test(part)) return `"${part}" ends in a dot or a space, which Windows drops`;
58 if (DEVICE.test(part.split(".")[0]!.trimEnd())) return `"${part}" is a Windows device name`;
59 const windows = windowsPart(part);
60 if (windows !== undefined) return windows;
61 // The exact names are handled as local files (never written). Another spelling (another case, a long s,
62 // a dotless i, a character HFS+ leaves out) can be the same folder on Windows or macOS.
63 if (isSkippedPart(part) && !ALWAYS_SKIPPED.has(part))
64 return `${JSON.stringify(part)} is ${skippedKey(part)} in another spelling, which stays local`;
65 }
66 return undefined;
69/** Why a link at `rel` pointing at `target` can't be made here, or undefined when it can. */
70export function unsafeLink(rel: string, target: string): string | undefined {
71 if (target === "" || target.includes("\0")) return "its link target is empty";
72 if (target.startsWith("/") || target.includes("\\") || target.includes(":"))
73 return `it links to ${target}, outside this folder`;
74 const to = posix.normalize(posix.join(posix.dirname(rel), target));
75 if (to === ".." || to.startsWith("../")) return `it links to ${target}, outside this folder`;
76 if (to.split("/").some(isSkippedPart)) return `it links into ${to}, which stays local`;
77 // A short name in the target can be .git or .claude on Windows.
78 for (const part of to.split("/")) {
79 const windows = windowsPart(part);
80 if (windows !== undefined) return `its link target goes through ${windows}`;
81 }
82 return undefined;
85/** True when a folder on the way to `rel` is a link (or a Windows junction): writing there would land wherever it points. */
86export function throughLink(root: string, rel: string): boolean {
87 let dir = root;
88 for (const part of rel.split("/").slice(0, -1)) {
89 dir = join(dir, part);
90 try {
91 const st = lstatSync(dir);
92 if (st.isSymbolicLink()) return true;
93 if (!st.isDirectory()) return false;
94 } catch {
95 return false;
96 }
97 }
98 return false;
101/** The parts of a sync, switch or restore answer that name paths. */
102interface Paths {
103 readonly files: Readonly<Record<string, string | null>>;
104 readonly blobs?: Readonly<Record<string, string | null>>;
105 readonly modes?: Readonly<Record<string, Mode>>;
108/**
109 * The answer with every unsafe path taken out, and the paths taken out. A
110 * path refused anywhere (text, blob or mode) is taken out everywhere.
111 */
112export function screenAnswer<T extends Paths>(root: string, answer: T): { answer: T; refused: Refused[] } {
113 const refused = new Map<string, string>();
114 const all = new Set([
115 ...Object.keys(answer.files),
116 ...Object.keys(answer.blobs ?? {}),
117 ...Object.keys(answer.modes ?? {}),
118 ]);
119 for (const rel of all) {
120 const why =
121 unsafePath(rel) ??
122 (throughLink(root, rel) ? "a folder on its way is a link, which could point outside this folder" : undefined);
123 if (why !== undefined) refused.set(rel, why);
124 }
125 for (const [rel, m] of Object.entries(answer.modes ?? {})) {
126 if (refused.has(rel) || typeof m.link !== "string") continue;
127 const why = unsafeLink(rel, m.link);
128 if (why !== undefined) refused.set(rel, why);
129 }
130 if (refused.size === 0) return { answer, refused: [] };
131 const keep = <V>(o: Readonly<Record<string, V>> | undefined) =>
132 o === undefined ? undefined : Object.fromEntries(Object.entries(o).filter(([rel]) => !refused.has(rel)));
133 const screened = {
134 ...answer,
135 files: keep(answer.files)!,
136 ...(answer.blobs ? { blobs: keep(answer.blobs) } : {}),
137 ...(answer.modes ? { modes: keep(answer.modes) } : {}),
138 };
139 return {
140 answer: screened,
141 refused: [...refused].sort(([a], [b]) => a.localeCompare(b)).map(([path, why]) => ({ path, why })),
142 };
145/** What a sync, switch, join or restore says about the paths it refused. Empty when there are none. */
146export function refusedText(refused: readonly Refused[]): string[] {
147 if (refused.length === 0) return [];
148 return [
149 `REFUSED ${refused.length === 1 ? "1 path" : `${refused.length} paths`} from the server, and wrote nothing there (another agent pushed ${refused.length === 1 ? "it" : "them"}; tell whoever runs the project):`,
150 ...refused.map((r) => ` ${JSON.stringify(r.path)}: ${r.why}`),
151 ];