diff --git a/LifeOS/install/LIFEOS/TOOLS/lib/Boundary.test.ts b/LifeOS/install/LIFEOS/TOOLS/lib/Boundary.test.ts new file mode 100644 index 0000000000..686b7992c2 --- /dev/null +++ b/LifeOS/install/LIFEOS/TOOLS/lib/Boundary.test.ts @@ -0,0 +1,58 @@ +/** + * Tests for Boundary primitives: confinedPath and atomicWrite. + * Validates path boundary containment, traversal rejection, and cross-platform atomic writes. + * + * Issue #2774 + */ +import { describe, expect, test } from "bun:test"; +import { existsSync, readFileSync, rmSync } from "node:fs"; +import { homedir, tmpdir } from "node:os"; +import { join } from "node:path"; +import { atomicWrite, confinedPath } from "./Boundary.ts"; + +describe("Boundary primitives", () => { + describe("confinedPath", () => { + const root = join(tmpdir(), "lifeos-boundary-test"); + + test("accepts valid relative path inside root", () => { + const res = confinedPath(tmpdir(), "test-file.txt"); + expect(res).not.toBeNull(); + expect(res).toBe(join(tmpdir(), "test-file.txt")); + }); + + test("accepts valid absolute path inside root", () => { + const target = join(tmpdir(), "nested", "file.txt"); + const res = confinedPath(tmpdir(), target); + expect(res).not.toBeNull(); + expect(res).toBe(target); + }); + + test("refuses traversal escaping root via parent segments", () => { + const res = confinedPath(root, "../escaped.txt"); + expect(res).toBeNull(); + }); + + test("refuses traversal after missing component", () => { + const res = confinedPath(root, "missing/../escaped.txt"); + expect(res).toBeNull(); + }); + + test("refuses absolute path pointing outside root", () => { + const res = confinedPath(root, homedir()); + expect(res).toBeNull(); + }); + }); + + describe("atomicWrite", () => { + test("writes data atomically and creates missing parent directories", () => { + const target = join(tmpdir(), "lifeos-atomic-test", "sub", "test.txt"); + try { + atomicWrite(target, "hello boundary"); + expect(existsSync(target)).toBe(true); + expect(readFileSync(target, "utf8")).toBe("hello boundary"); + } finally { + try { rmSync(join(tmpdir(), "lifeos-atomic-test"), { recursive: true, force: true }); } catch {} + } + }); + }); +}); \ No newline at end of file diff --git a/LifeOS/install/LIFEOS/TOOLS/lib/Boundary.ts b/LifeOS/install/LIFEOS/TOOLS/lib/Boundary.ts index 10caa167f6..7d560fc16d 100644 --- a/LifeOS/install/LIFEOS/TOOLS/lib/Boundary.ts +++ b/LifeOS/install/LIFEOS/TOOLS/lib/Boundary.ts @@ -24,7 +24,7 @@ import { } from "node:fs" import { randomUUID } from "node:crypto" import { hostname } from "node:os" -import { basename, dirname, isAbsolute, join, sep } from "node:path" +import { basename, dirname, isAbsolute, join, parse, sep } from "node:path" import { invariant, InvariantViolation } from "../Invariant" import { isVerificationCode as labelIsVerificationCode, @@ -58,12 +58,14 @@ export function confinedPath(root: string, p: string): string | null { try { realRoot = realpathSync(root) } catch { return null } const relative = !isAbsolute(p) - let cur = relative ? realRoot : sep + const rootPrefix = isAbsolute(p) ? parse(p).root : "" + let cur = relative ? realRoot : rootPrefix let curIsDir = true let missing = false const tail: string[] = [] - for (const comp of p.split(sep)) { + const parts = isAbsolute(p) ? p.slice(rootPrefix.length).split(/[\\/]+/) : p.split(/[\\/]+/) + for (const comp of parts) { if (comp === "" || comp === ".") continue if (missing) { if (comp === "..") return null @@ -74,7 +76,7 @@ export function confinedPath(root: string, p: string): string | null { if (comp === "..") { cur = dirname(cur) } else { - const next = cur === sep ? sep + comp : cur + sep + comp + const next = cur.endsWith(sep) ? cur + comp : cur + sep + comp let st try { st = lstatSync(next) } catch (e) { if (errCode(e) === "ENOENT") { missing = true; tail.push(comp); continue } @@ -129,7 +131,7 @@ function ensureDir(dir: string, dirMode: number): void { if (errCode(e) === "EEXIST") continue // a peer made it; it keeps its own mode throw e } - chmodSync(m, dirMode) + if (process.platform !== "win32") chmodSync(m, dirMode) } } @@ -159,7 +161,7 @@ export function atomicWrite(path: string, data: string | Uint8Array, opts: Atomi let off = 0 while (off < bytes.length) off += writeSync(fd, bytes, off, bytes.length - off) fsyncSync(fd) - invariant((fstatSync(fd).mode & 0o777) === mode, `atomicWrite: temp file mode is not 0o${mode.toString(8)}`) + if (process.platform !== "win32") { invariant((fstatSync(fd).mode & 0o777) === mode, `atomicWrite: temp file mode is not 0o${mode.toString(8)}`) } } finally { closeSync(fd) }