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): keep path sinks in validated branches

Rahmanbbb725e76e9b
diff --git a/SECURITY.md b/SECURITY.md
index e5dc84dc..44dc93a4 100644
--- a/SECURITY.md
+++ b/SECURITY.md
@@ -76,8 +76,8 @@ 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 normalized exact-or-root-prefix
- containment check before any filesystem operation; they are never rewritten into colliding names;
+- thread identifiers are accepted or rejected as a whole, then pass an explicit `path.relative`
+ containment check whose filesystem sink stays inside the proven-safe branch 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;
- read/write root policy performs lexical containment before filesystem resolution and canonical
diff --git a/docs/PROGRESS.md b/docs/PROGRESS.md
index 3ed97138..ec50e3b1 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 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
+without creating a stored/fast password hash; thread IDs and host paths use explicit `path.relative` containment, with every host
+filesystem sink kept inside the analyzer-recognized safe branch; 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 9aba15ff..f09585cc 100644
--- a/lib/host/paths.test.ts
+++ b/lib/host/paths.test.ts
@@ -19,12 +19,8 @@ import { assertNotRoot, assertUploadTarget, isUnderRoot, resolveReadable, safeWr
// base/read/write/ ← write root (narrower than read)
// base/read/write/wlink → symlink to base/outside/secret.txt
// base/outside/secret.txt
-let base = "";
-let readRoot = "";
-let writeRoot = "";
-let readPrefixSibling = "";
-let writePrefixSibling = "";
-let outside = "";
+let base = "", readRoot = "", writeRoot = "";
+let readPrefixSibling = "", writePrefixSibling = "", outside = "";
beforeAll(() => {
base = realpathSync(mkdtempSync(path.join(os.tmpdir(), "mso-paths-")));
diff --git a/lib/host/paths.ts b/lib/host/paths.ts
index 0186b99c..da1727da 100644
--- a/lib/host/paths.ts
+++ b/lib/host/paths.ts
@@ -322,28 +322,29 @@ export async function resolveReadable(requested: string): Promise<string> {
const normalized = path.resolve(absolute);
const configured = lexicalRoots(readRootList());
- // 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;
+ // Keep each filesystem sink inside the SAFE branch of CodeQL's documented
+ // path.relative sanitizer. Runtime-equivalent flags/helpers hide the proof from
+ // the analyzer. The second check uses real paths, so symlinks cannot escape.
for (const root of configured) {
- const rootPrefix = root.endsWith(path.sep) ? root : root + path.sep;
- if (normalized !== root && !normalized.startsWith(rootPrefix)) continue;
- lexicalRoot = root;
- break;
- }
- if (!lexicalRoot) throw new HostError("Path outside readable roots");
-
- const real = await fs.realpath(normalized);
- const realRoot = await fs.realpath(lexicalRoot).catch(() => path.resolve(lexicalRoot));
- const realRootPrefix = realRoot.endsWith(path.sep) ? realRoot : realRoot + path.sep;
- if (real !== realRoot && !real.startsWith(realRootPrefix)) {
- throw new HostError("Path outside readable roots");
+ const relative = path.relative(root, normalized);
+ if (relative === ".." || relative.startsWith(".." + path.sep) || path.isAbsolute(relative)) {
+ continue;
+ } else {
+ const real = await fs.realpath(normalized);
+ const realRoot = await fs.realpath(root).catch(() => path.resolve(root));
+ const realRelative = path.relative(realRoot, real);
+ if (
+ realRelative === ".." ||
+ realRelative.startsWith(".." + path.sep) ||
+ path.isAbsolute(realRelative)
+ ) {
+ throw new HostError("Path outside readable roots");
+ }
+ assertNotCredential(real);
+ return real;
+ }
}
- assertNotCredential(real);
- return real;
+ throw new HostError("Path outside readable roots");
}
// WRITE: "/" collapses to home (never the FS root). When !mustExist the parent
@@ -358,32 +359,41 @@ export async function safeWritePath(requested: string, mustExist: boolean): Prom
const configured = lexicalRoots(writeRootList());
const lexicalTarget = mustExist ? normalized : path.dirname(normalized);
- let lexicalRoot: string | null = null;
for (const root of configured) {
- 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);
- if (real !== realRoot && !real.startsWith(realRootPrefix)) {
- throw new HostError("Path outside writable roots");
+ const relative = path.relative(root, lexicalTarget);
+ if (relative === ".." || relative.startsWith(".." + path.sep) || path.isAbsolute(relative)) {
+ continue;
+ } else {
+ const realRoot = await fs.realpath(root).catch(() => path.resolve(root));
+ if (mustExist) {
+ const real = await fs.realpath(normalized);
+ const realRelative = path.relative(realRoot, real);
+ if (
+ realRelative === ".." ||
+ realRelative.startsWith(".." + path.sep) ||
+ path.isAbsolute(realRelative)
+ ) {
+ throw new HostError("Path outside writable roots");
+ }
+ assertNotCredential(real);
+ return real;
+ }
+
+ const parent = await fs.realpath(lexicalTarget);
+ const parentRelative = path.relative(realRoot, parent);
+ if (
+ parentRelative === ".." ||
+ parentRelative.startsWith(".." + path.sep) ||
+ path.isAbsolute(parentRelative)
+ ) {
+ throw new HostError("Path outside writable roots");
+ }
+ const joined = path.join(parent, path.basename(normalized));
+ assertNotCredential(joined);
+ return joined;
}
- assertNotCredential(real);
- return real;
- }
- const parent = await fs.realpath(path.dirname(normalized));
- if (parent !== realRoot && !parent.startsWith(realRootPrefix)) {
- throw new HostError("Path outside writable roots");
}
- const joined = path.join(parent, path.basename(normalized));
- assertNotCredential(joined);
- return joined;
+ throw new HostError("Path outside writable roots");
}
export async function assertNotRoot(p: string): Promise<void> {