Skip to content

security: validate --from-file payload paths for parity with direct paths - #1103

Closed
garagon wants to merge 1 commit into
garrytan:mainfrom
garagon:security/from-file-validate-read-path
Closed

garagon wants to merge 1 commit into
garrytan:mainfrom
garagon:security/from-file-validate-read-path

Conversation

@garagon

@garagon garagon commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Both load-html --from-file <payload.json> (browse/src/write-commands.ts) and
pdf --from-file <payload.json> (browse/src/meta-commands.ts) read the
payload file straight through fs.readFileSync() without any path
validation. The direct load-html <file> path next to it (in the same
function, ~60 lines below) runs every caller-supplied path through
validateReadPath() so reads stay confined to SAFE_DIRECTORIES
(cwd, TEMP_DIR). The parity gap means --from-file is a
read-anywhere escape hatch for any caller that can pick the payload
path.

This PR brings the shortcut flags up to the same policy.

Root cause (same shape as R3 F002 / F008)

The recent security audits keep finding the same pattern: a defense
exists on the primary path and is missing from a sibling shortcut.

Here: validateReadPath runs for load-html <file> but not for
load-html --from-file <payload> or pdf --from-file <payload>.
Same class of miss, same class of fix.

The gap

browse/src/write-commands.ts (load-html), before this PR:

if (args[i] === '--from-file') {
  const payloadPath = args[++i];
  if (!payloadPath) throw new Error('load-html: --from-file requires a path');
  const raw = fs.readFileSync(payloadPath, 'utf8');   // <-- no validateReadPath
  // ...
}
// ... ~60 lines later, the direct-path branch:
const absolutePath = path.resolve(filePath);
try {
  validateReadPath(absolutePath);                     // <-- guarded here
} catch (e: any) { /* actionable error */ }

browse/src/meta-commands.ts (pdf), before this PR:

function parsePdfFromFile(payloadPath: string): ParsedPdfArgs {
  const raw = fs.readFileSync(payloadPath, 'utf8');   // <-- no validateReadPath
  const json = JSON.parse(raw);
  // ...
}

Threat model

An attacker-influenced caller (MCP tool call whose arguments come from
agent context that can be prompt-injected) picks an arbitrary
--from-file path. Even when the JSON parse later fails on a target
that isn't valid JSON with an html / PDF-options field, the file has
already been read into process memory and error shapes can leak
presence/permissions. In tightened deployments where SAFE_DIRECTORIES
is the trust boundary between project files and the user's home, the
shortcut flags silently skip that boundary.

Severity is LOW on its own (defense-in-depth for the policy; no direct
content exfil channel on a typical target). Bundling it is cheap.

Fix

Add validateReadPath(path.resolve(payloadPath)) before readFileSync
in both sites, with a SAFE_DIRECTORIES-aware error message that
matches the direct-path branch so operators and agents see a consistent
error shape.

Test plan

New file: browse/test/from-file-path-validation.test.ts (pattern matches
browse/test/path-validation.test.ts and browse/test/content-security.test.ts).

  • Source-level: validateReadPath precedes readFileSync in the
    load-html --from-file branch.
  • Source-level: validateReadPath precedes readFileSync in
    parsePdfFromFile().
  • Error-shape parity: both sites reference SAFE_DIRECTORIES in the
    thrown error so the UX matches the direct path.
  • bun test -- browse/test/from-file-path-validation.test.ts browse/test/path-validation.test.ts browse/test/content-security.test.ts browse/test/adversarial-security.test.ts — 81 pass / 0 fail.

Risk

  • validateReadPath is already imported in write-commands.ts; the
    meta-commands.ts change adds two symbols to an existing import. No
    new dependencies.
  • Runtime cost: one path.resolve + one realpathSync per --from-file
    call (same cost as the direct path).
  • Backward compatibility: callers that previously passed payload paths
    outside SAFE_DIRECTORIES will now see the Path must be within: ...
    error with guidance to copy the payload into cwd or TEMP_DIR. Same
    behavior the direct load-html <file> path has had since
    path-security.ts landed.

Notes

Flagged during an ongoing security parity audit. Happy to rebase on
request or split into two PRs (one per file) if that is easier to
review.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant