fix(downloads): sanitize LLM-controlled filenames and folder paths (CWE-22)#218
Open
sebastiondev wants to merge 1 commit intoAIPexStudio:mainfrom
Open
fix(downloads): sanitize LLM-controlled filenames and folder paths (CWE-22)#218sebastiondev wants to merge 1 commit intoAIPexStudio:mainfrom
sebastiondev wants to merge 1 commit intoAIPexStudio:mainfrom
Conversation
…-22) The downloadImageTool, downloadChatImagesTool, and downloadTextAsMarkdown functions accept filename and folderPath parameters from LLM tool calls. These values were concatenated into a path and passed directly to chrome.downloads.download() without sanitization, allowing path traversal via sequences like "../../../Desktop/malware". Add sanitizeSegment() and sanitizeDownloadPath() helpers that: - Strip path separators (/ and \) from individual segments - Remove ".." traversal sequences - Filter control characters and illegal filesystem characters - Collapse leading/trailing dots and spaces Apply sanitization to every user-controlled path component in downloadTextAsMarkdown, downloadImageTool, and downloadChatImagesTool.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The download tools in
packages/browser-runtime/src/tools/tools/downloads/index.tspass LLM-suppliedfilename,folderPath, andfolderPrefixarguments directly tochrome.downloads.download()without sanitization. This PR adds a small sanitization layer plus unit tests.packages/browser-runtime/src/tools/tools/downloads/index.tsdownloadTextAsMarkdown,downloadImageTool,downloadChatImagesToolData flow
filename/folderPath/folderPrefix(e.g.../../Desktop/evil, names with control chars or Windows-illegal characters, leading dots, etc.).chrome.downloads.download({ filename: ... }).downloadImageToolanddownloadChatImagesToolthe call usessaveAs: false, so no Save-As dialog is shown — the user does not get a chance to notice the unusual path.Fix
New helpers in
downloads/sanitize-path.ts:sanitizeSegment(name, fallback)— strips/and\, removes..sequences, drops control chars (U+0000–U+001F, U+007F), strips Windows-illegal chars< > : " | ? *, trims leading/trailing dots and spaces, and falls back to a safe default if the result is empty.sanitizeDownloadPath(path, fallback)— splits on/or\, sanitizes each segment, drops empty/..-only segments, and rejoins with/.Applied at every sink in
downloads/index.ts:downloadTextAsMarkdown— sanitizefilename.downloadImageTool— sanitizefilenameandfolderPath.downloadChatImagesTool— sanitizefolderPrefixonce and reuse, sanitize each per-imagebaseFilename, and return the sanitized folder in the result so the response reflects what was actually written.The change is intentionally scoped: it does not alter tool schemas, behaviour for benign inputs, or any unrelated code paths.
Tests
Added
downloads/sanitize-path.test.tswith 15 cases covering:hello,my-file,folder/file).a/b,a\b,/etc/passwd)...traversal (..,....,../../../etc/passwd,..\..\Desktop\malware,folder/../../secret).\x00,\x1f).< > : " | ? *).../../../Desktop/malware→Desktop/malware).All tests pass under
vitest, and the repo'snpm run preflight(lint/typecheck/format) passes on the branch.Security analysis
The realistic threat model is prompt injection from page or chat content the agent is asked to process. The
saveAs: falsecalls in the image tools are the most user-invisible sink, since no dialog warns the user about the destination.Chrome's
downloadsAPI already rejects literal..back-references and absolute paths at the platform level, so this isn't an arbitrary-write-anywhere primitive — files still land under the user's Downloads root. The remaining risk that this PR addresses is:\x00truncation tricks),So this is best characterised as defense-in-depth and hardening of an LLM-reachable sink, not a sandbox escape patch.
Adversarial review
Before submitting, we tried to disprove this. The main candidate mitigation is Chrome itself:
chrome.downloads.downloaddoes sanitize obvious traversal. That eliminates the worst case (writing outside Downloads), which is why we're framing this as hardening rather than a critical bug. However, the tool args still flow into a security-relevant API from an LLM-controlled source with no validation, the image tools bypass the Save-As dialog, and there is no upstream framework protection between the tool argument and the Chrome API call — so the small sanitizer is genuinely useful and worth landing.Files changed
packages/browser-runtime/src/tools/tools/downloads/sanitize-path.ts(new, 57 lines)packages/browser-runtime/src/tools/tools/downloads/sanitize-path.test.ts(new, 90 lines)packages/browser-runtime/src/tools/tools/downloads/index.ts(sanitize calls at three sinks)Happy to adjust scope, naming, or fallback behaviour if you'd prefer a different convention.
cc @lewiswigmore