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): close follow-up CodeQL flows

Rahman88847f862440
diff --git a/SECURITY.md b/SECURITY.md
index afee2a7a..dd322384 100644
--- a/SECURITY.md
+++ b/SECURITY.md
@@ -74,8 +74,10 @@ following source-level controls in addition to route authorization:
- provider/config/OAuth maps accept validated provider identifiers and rebuild records from entry
lists rather than assigning or deleting attacker-selected object properties;
-- thread identifiers are accepted or rejected as a whole and are never rewritten into colliding
- filesystem names;
+- 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`
+ 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;
- read/write root policy performs lexical containment before filesystem resolution and canonical
@@ -85,10 +87,19 @@ following source-level controls in addition to route authorization:
- external widget embeds allow only credential-free external HTTPS URLs, omit `allow-same-origin`,
and send no referrer; local image previews accept a raster MIME allowlist and revoke object URLs;
- uploaded files use random exclusive temporary names, revalidate the destination after directory
- creation, reject traversal instead of normalizing it away, and remove partial files on every exit.
+ creation, reject traversal instead of normalizing it away, and remove partial files on every exit;
+- the intentional ChatGPT-to-VPS file bridge is write-scope only, accepts temporary URLs only from
+ reviewed OpenAI content/storage hosts, revalidates every redirect, streams with a 20 MiB hard cap,
+ checks response MIME and PNG/JPEG/WebP signatures, then enters the same write-root, credential,
+ exclusive-temporary-file and atomic-rename path as an ordinary authenticated upload. Generic
+ `application/octet-stream` remains deliberately content-agnostic because this is a file-transfer
+ surface, not an executable installer; transferred bytes are never executed automatically by MSO.
These controls reduce known classes of path race, prototype-pollution, DOM trust, and unbounded
-network-to-disk issues. They do not turn automated static analysis into a proof of complete security.
+network-to-disk issues. CodeQL can still correctly describe the last bridge as network data written
+to disk: that data flow is the feature itself, so any GitHub dismissal must use the narrow
+`won't fix` classification with this control rationale—not `false positive`. None of these controls
+turn automated static analysis into a proof of complete security.
## Service and package operations
diff --git a/app/api/auth/login/route.ts b/app/api/auth/login/route.ts
index 2755e73a..47eee666 100644
--- a/app/api/auth/login/route.ts
+++ b/app/api/auth/login/route.ts
@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from "next/server";
-import { constantTimeEq, MIN_SECRET_LEN, signSession, type SessionPayload } from "@/lib/auth/session";
+import { constantTimeEq, MAX_COMPARE_BYTES, MIN_SECRET_LEN, signSession, type SessionPayload } from "@/lib/auth/session";
import { SESSION_COOKIE } from "@/lib/auth/require-session";
import { sessionCookieAttrs } from "@/lib/auth/session-cookie";
import { isApproved, isValidDeviceId, recordPending, touchApproved } from "@/lib/auth/device-store";
@@ -98,8 +98,13 @@ export async function POST(req: NextRequest) {
const sessionSecret = process.env.OS_SESSION_SECRET ?? "";
const password = process.env.OS_LOGIN_PASSWORD ?? "";
- // Signing key must be strong. Fail-closed if either secret is unset/too weak.
- if (sessionSecret.length < MIN_SECRET_LEN || password.length < 6) {
+ // Signing key must be strong and the configured login secret must fit the
+ // fixed-width comparator. Fail closed on an unusable server configuration.
+ if (
+ sessionSecret.length < MIN_SECRET_LEN ||
+ password.length < 6 ||
+ Buffer.byteLength(password, "utf8") > MAX_COMPARE_BYTES
+ ) {
return NextResponse.json({ error: "not_configured" }, { status: 500 });
}
@@ -117,7 +122,11 @@ export async function POST(req: NextRequest) {
}
const { password: provided, deviceId } = body;
- if (typeof provided !== "string" || provided.length === 0) {
+ if (
+ typeof provided !== "string" ||
+ provided.length === 0 ||
+ Buffer.byteLength(provided, "utf8") > MAX_COMPARE_BYTES
+ ) {
return NextResponse.json({ error: "bad_password" }, { status: 401 });
}
if (!isValidDeviceId(deviceId)) {
@@ -125,8 +134,7 @@ export async function POST(req: NextRequest) {
}
const label = typeof body.deviceLabel === "string" ? body.deviceLabel.slice(0, 80) : "unknown device";
- // Length-safe: constantTimeEq hashes both sides to a fixed 32-byte digest
- // before comparing, so the compare time does NOT reveal the secret's length.
+ // Length-safe fixed-width comparison; no reusable password hash is created.
if (!constantTimeEq(password, provided)) {
audit({ action: "auth.denied", actor: deviceId, ip, ok: false, detail: "bad password" });
// Charge the process-wide budget HERE, not in rateLimited(), so only a genuinely
diff --git a/docs/CHATGPT-PLUGIN.md b/docs/CHATGPT-PLUGIN.md
index c4cb92bb..07863500 100644
--- a/docs/CHATGPT-PLUGIN.md
+++ b/docs/CHATGPT-PLUGIN.md
@@ -246,12 +246,15 @@ flowchart LR
Current guardrails:
-- maximum 20 MiB;
+- write scope is required;
+- maximum 20 MiB, enforced while streaming even without a trustworthy `Content-Length`;
- PNG, WebP, JPEG or generic octet-stream only;
+- response MIME is allowlisted; PNG/JPEG/WebP bytes must match their declared signature;
- temporary URL must be from an allowed OpenAI content/storage host;
- up to three redirects, and each redirect target is revalidated;
-- destination still has to be inside `OS_FS_WRITE_ROOTS`;
-- filename is sanitized;
+- destination still has to be inside `OS_FS_WRITE_ROOTS` and pass the credential/path jail;
+- filename is sanitized and the write uses an exclusive random temporary file plus atomic rename;
+- transferred bytes are never executed automatically by MSO;
- an existing same-name file can be replaced, so treat this as a write/destructive action;
- result includes byte count and SHA-256.
diff --git a/docs/MCP.md b/docs/MCP.md
index 6f042956..8c8f556b 100644
--- a/docs/MCP.md
+++ b/docs/MCP.md
@@ -415,13 +415,16 @@ replaces it: ask the client to generate the image with its own capability.
Importing the result is unchanged and deliberately preserved: **`fs_upload_file`** takes
one ChatGPT conversation/generated file through `openai/fileParams`, downloads its
-temporary OpenAI HTTPS URL immediately, re-validates up to three redirects, caps the file
-at **20 MiB**, accepts PNG/WebP/JPEG or generic octet-stream, and writes inside
-`OS_FS_WRITE_ROOTS`. Allowed download hosts are OpenAI `*.oaiusercontent.com` content hosts
-or the explicitly matched `oaisdmntpr<region>.blob.core.windows.net` storage-account
-family—not arbitrary Azure Blob hosts. The result returns path, bytes and SHA-256. An
-existing same-name file may be replaced, so the tool is classified as a write/destructive
-action. See `CHATGPT-PLUGIN.md` for the end-to-end diagram.
+temporary OpenAI HTTPS URL immediately, re-validates up to three redirects, and consumes the
+body incrementally with a hard **20 MiB** ceiling even when `Content-Length` is absent or false.
+It accepts PNG/WebP/JPEG or generic octet-stream, rejects a conflicting response MIME, validates
+PNG/JPEG/WebP magic bytes, and writes only inside `OS_FS_WRITE_ROOTS`. Allowed download hosts are
+OpenAI `*.oaiusercontent.com` content hosts or the explicitly matched
+`oaisdmntpr<region>.blob.core.windows.net` storage-account family—not arbitrary Azure Blob hosts.
+The final write uses the normal credential/path jail, an exclusive random temporary file and an
+atomic rename; MSO never executes the transferred bytes automatically. The result returns path,
+bytes and SHA-256. An existing same-name file may be replaced, so the tool is classified as a
+write/destructive action. See `CHATGPT-PLUGIN.md` for the end-to-end diagram.
`OS_CODEX_BUILTIN_TOOLS` still exists and still takes an allowlisted list, but its
default is now EMPTY and `image_generation` is no longer an accepted value — naming it
diff --git a/docs/PROGRESS.md b/docs/PROGRESS.md
index 3e6ed611..b861152b 100644
--- a/docs/PROGRESS.md
+++ b/docs/PROGRESS.md
@@ -28,9 +28,18 @@ explicit bounded operational surfaces are Operator.
A hosted CodeQL follow-up on the first release candidate was not treated as cosmetic backlog.
The release was reopened to harden validated config maps, strict thread identifiers, external iframe
policy, descriptor-bound file reads, dual root containment, private collision fixtures, bounded
-models.dev cache writes, exclusive upload staging, and immutable CodeQL workflow pins. Regression
-tests accompany each changed boundary; the replacement SHA must repeat the full repository,
-build, hosted-analysis, deploy and ultimate-assurance gates before the branch is considered closed.
+models.dev cache writes, exclusive upload staging, and immutable CodeQL workflow pins. The first
+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
+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
+as a false positive or by weakening CodeQL. Regression tests accompany each changed boundary; the
+replacement SHA must repeat the full repository, build, hosted-analysis, deploy and
+ultimate-assurance gates before the branch is considered closed.
The same release reconciles all ten open Dependabot PRs instead of merging them blindly. Seven
minor/patch or action updates with green hosted checks are integrated, and Lucide 1.x is migrated
diff --git a/lib/ai/threads.ts b/lib/ai/threads.ts
index 982bfafa..20100b3f 100644
--- a/lib/ai/threads.ts
+++ b/lib/ai/threads.ts
@@ -18,13 +18,22 @@ export interface ChatThread {
export type ThreadSummary = Pick<ChatThread, "id" | "title" | "createdAt" | "updatedAt">;
-const DIR = process.env.OS_THREADS_DIR || path.join(os.homedir(), ".mso", "threads");
-// ids are app-generated but jail them anyway (path-traversal guard): alnum + -_ only.
-const safeId = (id: string) => {
- if (!/^[A-Za-z0-9_-]{1,64}$/.test(id)) throw new Error("invalid thread id");
- return id;
-};
-const fileFor = (id: string) => path.join(DIR, `${safeId(id)}.yml`);
+const DIR = path.resolve(process.env.OS_THREADS_DIR || path.join(os.homedir(), ".mso", "threads"));
+const THREAD_ID = /^[A-Za-z0-9_-]{1,64}$/;
+
+// ids are app-generated but still arrive over HTTP. Keep the allowlist, then use
+// an explicit path.relative containment guard before a filesystem sink. The
+// latter is both symlink-independent lexical defence and a pattern CodeQL can
+// verify rather than having to trust a project-specific sanitizer helper.
+function fileFor(id: string): string {
+ if (!THREAD_ID.test(id)) throw new Error("invalid thread id");
+ const candidate = path.resolve(DIR, `${id}.yml`);
+ const relative = path.relative(DIR, candidate);
+ if (relative === ".." || relative.startsWith(".." + path.sep) || path.isAbsolute(relative)) {
+ throw new Error("invalid thread id");
+ }
+ return candidate;
+}
export async function listThreads(): Promise<ThreadSummary[]> {
let names: string[];
diff --git a/lib/auth/session.test.ts b/lib/auth/session.test.ts
index d0a87c5b..02f0cced 100644
--- a/lib/auth/session.test.ts
+++ b/lib/auth/session.test.ts
@@ -1,6 +1,6 @@
import crypto from "node:crypto";
import { describe, expect, it } from "vitest";
-import { MIN_SECRET_LEN, signSession, verifySession, type SessionPayload } from "./session";
+import { constantTimeEq, MAX_COMPARE_BYTES, MIN_SECRET_LEN, signSession, verifySession, type SessionPayload } from "./session";
const SECRET = "s".repeat(MIN_SECRET_LEN);
@@ -21,6 +21,20 @@ function forgeToken(encodedPayload: string, secret: string): string {
return `${encodedPayload}.${b64url(hmac.digest())}`;
}
+describe("constantTimeEq fixed-width comparison", () => {
+ it("compares equal and unequal values without creating a password hash", () => {
+ expect(constantTimeEq("correct-horse", "correct-horse")).toBe(true);
+ expect(constantTimeEq("correct-horse", "correct-house")).toBe(false);
+ expect(constantTimeEq("short", "shorter")).toBe(false);
+ });
+
+ it("fails closed when either UTF-8 value exceeds the fixed-width ceiling", () => {
+ const oversized = "a".repeat(MAX_COMPARE_BYTES + 1);
+ expect(constantTimeEq(oversized, oversized)).toBe(false);
+ expect(constantTimeEq("a", oversized)).toBe(false);
+ });
+});
+
describe("signSession / verifySession roundtrip", () => {
it("verifies a token it signed and returns the exact payload", () => {
const payload = freshPayload();
diff --git a/lib/auth/session.ts b/lib/auth/session.ts
index dd858908..231fd9c6 100644
--- a/lib/auth/session.ts
+++ b/lib/auth/session.ts
@@ -6,15 +6,29 @@ import crypto from "crypto";
/** Minimum length for the signing secret. A short/empty key is forgeable. */
export const MIN_SECRET_LEN = 32;
+/** Maximum UTF-8 size accepted by the fixed-width secret comparator. */
+export const MAX_COMPARE_BYTES = 1024;
+
/**
- * Length-safe constant-time string compare. A domain-separated HMAC maps both
- * values to a fixed width before `timingSafeEqual`; it is not password storage
- * and no verifier is persisted. A naive length early-exit would leak metadata.
+ * Length-safe constant-time string compare. This is comparison, not password
+ * storage: both cleartext inputs are copied into fixed-width buffers and their
+ * byte lengths are included in the compared bytes. No fast password verifier is
+ * created or persisted, and unequal lengths do not take an early-return path.
+ * Callers reject over-limit network input before invoking it.
*/
-const COMPARE_DOMAIN_KEY = Buffer.from("mso.constant-time-compare.v1", "utf8");
+function fixedCompareBuffer(value: string): { buffer: Buffer; valid: boolean } {
+ const bytes = Buffer.from(value, "utf8");
+ const out = Buffer.alloc(MAX_COMPARE_BYTES + 4);
+ bytes.copy(out, 0, 0, Math.min(bytes.length, MAX_COMPARE_BYTES));
+ out.writeUInt32BE(Math.min(bytes.length, MAX_COMPARE_BYTES + 1), MAX_COMPARE_BYTES);
+ return { buffer: out, valid: bytes.length <= MAX_COMPARE_BYTES };
+}
+
export function constantTimeEq(a: string, b: string): boolean {
- const digest = (value: string) => crypto.createHmac("sha256", COMPARE_DOMAIN_KEY).update(value).digest();
- return crypto.timingSafeEqual(digest(a), digest(b));
+ const left = fixedCompareBuffer(a);
+ const right = fixedCompareBuffer(b);
+ const same = crypto.timingSafeEqual(left.buffer, right.buffer);
+ return left.valid && right.valid && same;
}
export interface SessionPayload {
diff --git a/lib/host/paths.ts b/lib/host/paths.ts
index 77db83cc..49e2e523 100644
--- a/lib/host/paths.ts
+++ b/lib/host/paths.ts
@@ -305,10 +305,6 @@ function lexicalRoots(list: string[]): string[] {
return list.map((root) => path.resolve(root));
}
-function assertLexicalContainment(target: string, roots: string[], message: string): void {
- if (!roots.some((root) => isUnderRoot(target, root))) throw new HostError(message);
-}
-
// Realpath-resolved WRITE roots — shared by safeWritePath/assertNotRoot here and
// exec.ts's cwd bounds, so the realpath-fallback strategy lives in one place.
export async function resolveWriteRoots(): Promise<string[]> {
@@ -325,10 +321,25 @@ export async function resolveReadable(requested: string): Promise<string> {
else absolute = path.resolve(requested);
const normalized = path.resolve(absolute);
const configured = lexicalRoots(readRootList());
- assertLexicalContainment(normalized, configured, "Path outside readable roots");
+
+ // 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.
+ 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;
+ lexicalRoot = root;
+ break;
+ }
+ if (!lexicalRoot) throw new HostError("Path outside readable roots");
+
const real = await fs.realpath(normalized);
- const rr = await realRoots(configured);
- if (!rr.some((r) => isUnderRoot(real, r))) throw new HostError("Path outside readable roots");
+ 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)) {
+ throw new HostError("Path outside readable roots");
+ }
assertNotCredential(real);
return real;
}
@@ -344,16 +355,31 @@ export async function safeWritePath(requested: string, mustExist: boolean): Prom
const normalized = path.resolve(absolute);
const configured = lexicalRoots(writeRootList());
const lexicalTarget = mustExist ? normalized : path.dirname(normalized);
- assertLexicalContainment(lexicalTarget, configured, "Path outside writable roots");
- const rr = await realRoots(configured);
+
+ 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;
+ lexicalRoot = root;
+ break;
+ }
+ if (!lexicalRoot) throw new HostError("Path outside writable roots");
+
+ const realRoot = await fs.realpath(lexicalRoot).catch(() => path.resolve(lexicalRoot));
if (mustExist) {
const real = await fs.realpath(normalized);
- if (!rr.some((r) => isUnderRoot(real, r))) throw new HostError("Path outside writable roots");
+ 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(path.dirname(normalized));
- if (!rr.some((r) => isUnderRoot(parent, r))) throw new HostError("Path outside writable roots");
+ 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;
diff --git a/lib/mcp/openai-file-upload.test.ts b/lib/mcp/openai-file-upload.test.ts
index 67d0451a..748504fb 100644
--- a/lib/mcp/openai-file-upload.test.ts
+++ b/lib/mcp/openai-file-upload.test.ts
@@ -11,9 +11,9 @@ const { importOpenAiProvidedFile } = await import("./openai-file-upload");
beforeEach(() => {
uploadInto.mockClear();
- vi.stubGlobal("fetch", vi.fn(async () => new Response(Uint8Array.from([137, 80, 78, 71]), {
+ vi.stubGlobal("fetch", vi.fn(async () => new Response(Uint8Array.from([137, 80, 78, 71, 13, 10, 26, 10]), {
status: 200,
- headers: { "content-type": "image/png", "content-length": "4" },
+ headers: { "content-type": "image/png", "content-length": "8" },
})));
});
@@ -24,7 +24,7 @@ describe("ChatGPT file host allowlist", () => {
file_id,
mime_type: "image/png",
file_name: "file.png",
- size: 4,
+ size: 8,
},
dest: "/home/antinrml/generated-images",
filename: "file.png",
@@ -38,7 +38,7 @@ describe("ChatGPT file host allowlist", () => {
"oaisdmntprfuture123.blob.core.windows.net",
])("accepts OpenAI regional Azure storage account %s", async (host) => {
const result = await importUrl(`https://${host}/container/file.png?sig=redacted`);
- expect(result.bytes).toBe(4);
+ expect(result.bytes).toBe(8);
expect(uploadInto).toHaveBeenCalledTimes(1);
});
@@ -69,14 +69,56 @@ describe("ChatGPT file host allowlist", () => {
status: 302,
headers: { location: "https://oaisdmntprfuture123.blob.core.windows.net/container/file.png?sig=next" },
}))
- .mockResolvedValueOnce(new Response(Uint8Array.from([137, 80, 78, 71]), {
+ .mockResolvedValueOnce(new Response(Uint8Array.from([137, 80, 78, 71, 13, 10, 26, 10]), {
status: 200,
- headers: { "content-type": "image/png", "content-length": "4" },
+ headers: { "content-type": "image/png", "content-length": "8" },
}));
vi.stubGlobal("fetch", fetchMock);
const result = await importUrl("https://files.oaiusercontent.com/start/file.png");
- expect(result.bytes).toBe(4);
+ expect(result.bytes).toBe(8);
expect(fetchMock).toHaveBeenCalledTimes(2);
expect(uploadInto).toHaveBeenCalledTimes(1);
});
+
+ it("rejects an image whose bytes do not match its declared MIME", async () => {
+ vi.stubGlobal("fetch", vi.fn(async () => new Response(new TextEncoder().encode("not a png"), {
+ status: 200,
+ headers: { "content-type": "image/png" },
+ })));
+ await expect(importUrl("https://files.oaiusercontent.com/file.png")).rejects.toThrow("signature does not match image/png");
+ expect(uploadInto).not.toHaveBeenCalled();
+ });
+
+ it("rejects a conflicting trusted response MIME", async () => {
+ vi.stubGlobal("fetch", vi.fn(async () => new Response(Uint8Array.from([0xff, 0xd8, 0xff]), {
+ status: 200,
+ headers: { "content-type": "image/jpeg" },
+ })));
+ await expect(importUrl("https://files.oaiusercontent.com/file.png")).rejects.toThrow("does not match image/png");
+ expect(uploadInto).not.toHaveBeenCalled();
+ });
+
+ it("stops a chunked response at 20 MiB even without Content-Length", async () => {
+ const chunk = new Uint8Array(1024 * 1024);
+ const body = new ReadableStream<Uint8Array>({
+ start(controller) {
+ for (let i = 0; i < 21; i += 1) controller.enqueue(chunk);
+ controller.close();
+ },
+ });
+ vi.stubGlobal("fetch", vi.fn(async () => new Response(body, {
+ status: 200,
+ headers: { "content-type": "application/octet-stream" },
+ })));
+ await expect(importOpenAiProvidedFile({
+ file: {
+ download_url: "https://files.oaiusercontent.com/file.bin",
+ file_id: "file_large",
+ mime_type: "application/octet-stream",
+ file_name: "file.bin",
+ },
+ dest: "/home/antinrml/generated-images",
+ })).rejects.toThrow("exceeds the 20 MiB MCP import limit");
+ expect(uploadInto).not.toHaveBeenCalled();
+ });
});
diff --git a/lib/mcp/openai-file-upload.ts b/lib/mcp/openai-file-upload.ts
index 0f8b7c2e..6f3c9269 100644
--- a/lib/mcp/openai-file-upload.ts
+++ b/lib/mcp/openai-file-upload.ts
@@ -58,6 +58,53 @@ async function fetchTrustedFile(initialUrl: URL): Promise<Response> {
throw new HostError("OpenAI file download exceeded the redirect limit");
}
+async function readBoundedBody(response: Response): Promise<Buffer> {
+ const rawLength = response.headers.get("content-length");
+ if (rawLength !== null) {
+ const declared = Number(rawLength);
+ if (!Number.isFinite(declared) || declared < 0 || declared > MAX_FILE_BYTES) {
+ throw new HostError("file exceeds the 20 MiB MCP import limit");
+ }
+ }
+ if (!response.body) throw new HostError("OpenAI file download was empty");
+
+ const reader = response.body.getReader();
+ const chunks: Uint8Array[] = [];
+ let bytes = 0;
+ try {
+ for (;;) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ bytes += value.byteLength;
+ if (bytes > MAX_FILE_BYTES) {
+ await reader.cancel("MCP import size limit exceeded").catch(() => {});
+ throw new HostError("file exceeds the 20 MiB MCP import limit");
+ }
+ chunks.push(value);
+ }
+ } finally {
+ reader.releaseLock();
+ }
+ if (bytes === 0) throw new HostError("OpenAI file download was empty");
+ return Buffer.concat(chunks.map((chunk) => Buffer.from(chunk)), bytes);
+}
+
+function responseMime(response: Response): string | null {
+ const value = response.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase();
+ if (!value) return null;
+ if (!ALLOWED_MIME.has(value)) throw new HostError(`unsupported response file type: ${value}`);
+ return value;
+}
+
+function hasExpectedSignature(data: Buffer, mimeType: string): boolean {
+ if (mimeType === "image/png") {
+ return data.length >= 8 && data.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]));
+ }
+ if (mimeType === "image/jpeg") return data.length >= 3 && data[0] === 0xff && data[1] === 0xd8 && data[2] === 0xff;
+ if (mimeType === "image/webp") return data.length >= 12 && data.subarray(0, 4).toString("ascii") === "RIFF" && data.subarray(8, 12).toString("ascii") === "WEBP";
+ return true; // application/octet-stream is intentionally a generic file bridge.
+}
+
function safeFilename(input: string | undefined, fallback: string): string {
const value = path.basename((input || fallback).trim());
if (!value || value === "." || value === ".." || value.length > 200) throw new HostError("filename is invalid");
@@ -88,11 +135,18 @@ export async function importOpenAiProvidedFile(opts: {
const response = await fetchTrustedFile(url);
if (!response.ok) throw new HostError(`OpenAI file download failed (${response.status})`);
- const declared = Number(response.headers.get("content-length") || 0);
- if (declared > MAX_FILE_BYTES) throw new HostError("file exceeds the 20 MiB MCP import limit");
- const data = Buffer.from(await response.arrayBuffer());
- if (data.byteLength === 0) throw new HostError("OpenAI file download was empty");
- if (data.byteLength > MAX_FILE_BYTES) throw new HostError("file exceeds the 20 MiB MCP import limit");
+ const wireMime = responseMime(response);
+ if (
+ mimeType !== "application/octet-stream" &&
+ wireMime &&
+ wireMime !== "application/octet-stream" &&
+ wireMime !== mimeType
+ ) {
+ throw new HostError(`response file type ${wireMime} does not match ${mimeType}`);
+ }
+ const data = await readBoundedBody(response);
+ const effectiveMime = mimeType === "application/octet-stream" ? (wireMime || mimeType) : mimeType;
+ if (!hasExpectedSignature(data, effectiveMime)) throw new HostError(`file signature does not match ${effectiveMime}`);
const fallback = file.file_name || file.name || `${file.file_id}.bin`;
const filename = safeFilename(opts.filename, fallback);
diff --git a/lib/models/catalog.test.ts b/lib/models/catalog.test.ts
index e9832036..222bb560 100644
--- a/lib/models/catalog.test.ts
+++ b/lib/models/catalog.test.ts
@@ -35,8 +35,13 @@ describe("bounded models.dev catalog cache", () => {
await expect(mod.getCatalog({ force: true })).resolves.toMatchObject({ openai: { models: { "gpt-test": {} } } });
expect(fetchMock).toHaveBeenCalledWith("https://models.dev/api.json", expect.objectContaining({ redirect: "error" }));
expect(fs.statSync(root).mode & 0o777).toBe(0o700);
- expect(fs.statSync(cache).mode & 0o777).toBe(0o600);
- expect(JSON.parse(fs.readFileSync(cache, "utf8")).data.openai.models).toHaveProperty("gpt-test");
+ const handle = fs.openSync(cache, "r");
+ try {
+ expect(fs.fstatSync(handle).mode & 0o777).toBe(0o600);
+ expect(JSON.parse(fs.readFileSync(handle, "utf8")).data.openai.models).toHaveProperty("gpt-test");
+ } finally {
+ fs.closeSync(handle);
+ }
expect(fs.readdirSync(root).filter((name) => name.endsWith(".tmp"))).toEqual([]);
});