Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

fix(security): make root containment explicit

Rahman5d85c9be8dc0
diff --git a/SECURITY.md b/SECURITY.md
index dd322384..e5dc84dc 100644
--- a/SECURITY.md
+++ b/SECURITY.md
@@ -76,7 +76,7 @@ following source-level controls in addition to route authorization:
lists rather than assigning or deleting attacker-selected object properties;
- login-secret equality uses a fixed-width constant-time byte comparison; no fast password hash
or reusable password verifier is created, and over-limit UTF-8 input fails closed;
-- thread identifiers are accepted or rejected as a whole, then pass an explicit `path.relative`
+- thread identifiers are accepted or rejected as a whole, then pass an explicit normalized exact-or-root-prefix
containment check before any filesystem operation; they are never rewritten into colliding names;
- bounded host reads, lock recovery, skill/catalog reads and model-cache reads bind metadata and
content to the same `O_NOFOLLOW` file descriptor where the platform supports it;
diff --git a/docs/PROGRESS.md b/docs/PROGRESS.md
index b861152b..3ed97138 100644
--- a/docs/PROGRESS.md
+++ b/docs/PROGRESS.md
@@ -32,8 +32,8 @@ models.dev cache writes, exclusive upload staging, and immutable CodeQL workflow
PR rescan then reported eight changed-line alerts rather than being called green because the
analyzer process exited successfully: a password-comparison model, five path/race flows, one test
TOCTOU and the intentional ChatGPT file-transfer sink. The comparator now uses fixed-width bytes
-without creating a stored/fast password hash; thread and host paths use analyzer-recognized
-`path.relative` containment adjacent to the sink; the cache test binds mode and read to one file
+without creating a stored/fast password hash; thread IDs use explicit `path.relative` containment, while host paths use normalized
+exact-or-separator-bound root-prefix checks immediately before each filesystem sink; the cache test binds mode and read to one file
descriptor; and the ChatGPT bridge now streams under 20 MiB, validates redirect host, MIME and raster
signatures, then enters the guarded atomic upload path. Its remaining network-to-disk data flow is
the documented purpose of `fs_upload_file`; it may be classified only as reviewed `won't fix`, never
diff --git a/lib/host/paths.test.ts b/lib/host/paths.test.ts
index f8f9c0c3..9aba15ff 100644
--- a/lib/host/paths.test.ts
+++ b/lib/host/paths.test.ts
@@ -22,17 +22,25 @@ import { assertNotRoot, assertUploadTarget, isUnderRoot, resolveReadable, safeWr
let base = "";
let readRoot = "";
let writeRoot = "";
+let readPrefixSibling = "";
+let writePrefixSibling = "";
let outside = "";
beforeAll(() => {
base = realpathSync(mkdtempSync(path.join(os.tmpdir(), "mso-paths-")));
readRoot = path.join(base, "read");
writeRoot = path.join(readRoot, "write");
+ readPrefixSibling = `${readRoot}-sibling`;
+ writePrefixSibling = `${writeRoot}-sibling`;
outside = path.join(base, "outside");
mkdirSync(writeRoot, { recursive: true });
+ mkdirSync(readPrefixSibling, { recursive: true });
+ mkdirSync(writePrefixSibling, { recursive: true });
mkdirSync(outside, { recursive: true });
writeFileSync(path.join(readRoot, "inside.txt"), "ok");
writeFileSync(path.join(writeRoot, "wfile.txt"), "ok");
+ writeFileSync(path.join(readPrefixSibling, "lookalike.txt"), "nope");
+ writeFileSync(path.join(writePrefixSibling, "lookalike.txt"), "nope");
writeFileSync(path.join(outside, "secret.txt"), "nope");
symlinkSync(path.join(outside, "secret.txt"), path.join(readRoot, "sneaky"));
symlinkSync(path.join(outside, "secret.txt"), path.join(writeRoot, "wlink"));
@@ -77,6 +85,12 @@ describe("resolveReadable bounds", () => {
.rejects.toThrow(/outside readable roots/i);
});
+ it("rejects a sibling whose name merely starts with the read-root string", async () => {
+ useRoots(readRoot, writeRoot);
+ await expect(resolveReadable(path.join(readPrefixSibling, "lookalike.txt")))
+ .rejects.toThrow(/outside readable roots/i);
+ });
+
it("rejects a symlink inside the root that points outside it", async () => {
useRoots(readRoot, writeRoot);
await expect(resolveReadable(path.join(readRoot, "sneaky"))).rejects.toThrow(
@@ -157,6 +171,14 @@ describe("safeWritePath bounds", () => {
);
});
+ it("rejects existing and new paths under a write-root prefix lookalike", async () => {
+ useRoots(readRoot, writeRoot);
+ await expect(safeWritePath(path.join(writePrefixSibling, "lookalike.txt"), true))
+ .rejects.toThrow(/outside writable roots/i);
+ await expect(safeWritePath(path.join(writePrefixSibling, "new.txt"), false))
+ .rejects.toThrow(/outside writable roots/i);
+ });
+
it("rejects a nonexistent outside parent before resolving filesystem metadata", async () => {
useRoots(readRoot, writeRoot);
await expect(safeWritePath(path.join(outside, "missing", "new.txt"), false))
diff --git a/lib/host/paths.ts b/lib/host/paths.ts
index 49e2e523..0186b99c 100644
--- a/lib/host/paths.ts
+++ b/lib/host/paths.ts
@@ -322,13 +322,15 @@ export async function resolveReadable(requested: string): Promise<string> {
const normalized = path.resolve(absolute);
const configured = lexicalRoots(readRootList());
- // Select one configured root with the canonical path.relative escape test.
- // This guard intentionally stays next to the tainted path and realpath sink so
- // both reviewers and CodeQL can prove the path cannot lexically leave a root.
+ // Select one configured root with an exact-or-separator-bound prefix after
+ // path.resolve normalization. `/safe/root-two` must never satisfy `/safe/root`.
+ // Keep this recognized containment guard immediately before fs.realpath: the
+ // realpath call itself touches the filesystem and must not receive an unchecked
+ // arbitrary path even though a second symlink-aware containment check follows.
let lexicalRoot: string | null = null;
for (const root of configured) {
- const relative = path.relative(root, normalized);
- if (relative === ".." || relative.startsWith(".." + path.sep) || path.isAbsolute(relative)) continue;
+ const rootPrefix = root.endsWith(path.sep) ? root : root + path.sep;
+ if (normalized !== root && !normalized.startsWith(rootPrefix)) continue;
lexicalRoot = root;
break;
}
@@ -336,8 +338,8 @@ export async function resolveReadable(requested: string): Promise<string> {
const real = await fs.realpath(normalized);
const realRoot = await fs.realpath(lexicalRoot).catch(() => path.resolve(lexicalRoot));
- const realRelative = path.relative(realRoot, real);
- if (realRelative === ".." || realRelative.startsWith(".." + path.sep) || path.isAbsolute(realRelative)) {
+ const realRootPrefix = realRoot.endsWith(path.sep) ? realRoot : realRoot + path.sep;
+ if (real !== realRoot && !real.startsWith(realRootPrefix)) {
throw new HostError("Path outside readable roots");
}
assertNotCredential(real);
@@ -358,26 +360,25 @@ export async function safeWritePath(requested: string, mustExist: boolean): Prom
let lexicalRoot: string | null = null;
for (const root of configured) {
- const relative = path.relative(root, lexicalTarget);
- if (relative === ".." || relative.startsWith(".." + path.sep) || path.isAbsolute(relative)) continue;
+ const rootPrefix = root.endsWith(path.sep) ? root : root + path.sep;
+ if (lexicalTarget !== root && !lexicalTarget.startsWith(rootPrefix)) continue;
lexicalRoot = root;
break;
}
if (!lexicalRoot) throw new HostError("Path outside writable roots");
const realRoot = await fs.realpath(lexicalRoot).catch(() => path.resolve(lexicalRoot));
+ const realRootPrefix = realRoot.endsWith(path.sep) ? realRoot : realRoot + path.sep;
if (mustExist) {
const real = await fs.realpath(normalized);
- const realRelative = path.relative(realRoot, real);
- if (realRelative === ".." || realRelative.startsWith(".." + path.sep) || path.isAbsolute(realRelative)) {
+ if (real !== realRoot && !real.startsWith(realRootPrefix)) {
throw new HostError("Path outside writable roots");
}
assertNotCredential(real);
return real;
}
const parent = await fs.realpath(path.dirname(normalized));
- const parentRelative = path.relative(realRoot, parent);
- if (parentRelative === ".." || parentRelative.startsWith(".." + path.sep) || path.isAbsolute(parentRelative)) {
+ if (parent !== realRoot && !parent.startsWith(realRootPrefix)) {
throw new HostError("Path outside writable roots");
}
const joined = path.join(parent, path.basename(normalized));