From ac89d8847e3ddeaa20c3399bf594aa2ab2be30e2 Mon Sep 17 00:00:00 2001 From: RagavRida Date: Fri, 24 Apr 2026 00:07:52 +0530 Subject: [PATCH] fix(meta-commands): guard JSON.parse in pdf --from-file parser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parsePdfFromFile() runs JSON.parse on user-supplied file contents with no try/catch. A malformed payload surfaces as an uncaught SyntaxError from the 'pdf' command handler and the user sees an opaque stack trace instead of "this file isn't valid JSON". Worse, the same call path is used by make-pdf when header/footer HTML would overflow Windows' CreateProcess argv cap, so a corrupt payload file there can take down the make-pdf run. Wrap JSON.parse. Re-throw with a message that names the offending file and echoes the parser's own explanation. Also reject top-level non- objects (null, array, primitive) since the rest of the function treats json as an object — catching that here produces a clear error instead of a TypeError further down. --- browse/src/meta-commands.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/browse/src/meta-commands.ts b/browse/src/meta-commands.ts index c505d4cf4..bbd90f62d 100644 --- a/browse/src/meta-commands.ts +++ b/browse/src/meta-commands.ts @@ -149,7 +149,16 @@ function parsePdfFromFile(payloadPath: string): ParsedPdfArgs { ); } const raw = fs.readFileSync(payloadPath, 'utf8'); - const json = JSON.parse(raw); + let json: any; + try { + json = JSON.parse(raw); + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + throw new Error(`pdf: --from-file ${payloadPath} is not valid JSON (${msg}).`); + } + if (json === null || typeof json !== 'object' || Array.isArray(json)) { + throw new Error(`pdf: --from-file ${payloadPath} must be a JSON object, got ${Array.isArray(json) ? 'array' : typeof json}.`); + } const out: ParsedPdfArgs = { output: json.output || `${TEMP_DIR}/browse-page.pdf`, format: json.format,