-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add SSRF protection to read_file URL fetches (#587, #560) #597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rahul188
wants to merge
6
commits into
wonderwhy-er:main
Choose a base branch
from
rahul188:fix/read-url-ssrf-guard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
084ee3c
Add SSRF protection to read_file URL fetches (#587, #560)
rahul188 2a13f38
Harden SSRF address checks with CIDR-correct IPv6 handling
rahul188 f1ffbe7
Parse PDFs from the validated response instead of re-fetching
rahul188 09c45c3
Route parsePdfToMarkdown URL sources through the SSRF guard
rahul188 7244dc8
Pin URL fetches to the addresses the SSRF guard validated
rahul188 3931203
Merge remote-tracking branch 'origin/main' into fix/read-url-ssrf-guard
rahul188 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import assert from 'assert'; | ||
| import { assertUrlIsFetchable, readFileFromUrl } from '../dist/tools/filesystem.js'; | ||
|
|
||
| /** | ||
| * Regression tests for #587 / #560: read_file with isUrl fetched arbitrary URLs | ||
| * with no SSRF protection, so a URL like http://169.254.169.254/… could read | ||
| * cloud instance metadata or reach internal services. | ||
| * | ||
| * assertUrlIsFetchable() now rejects non-http(s) schemes and any host that is | ||
| * (or resolves to) a non-public address. These cases short-circuit before any | ||
| * network access, so the test is hermetic — no outbound requests are made. | ||
| */ | ||
|
|
||
| let passed = 0; | ||
| const ok = (msg) => { passed++; console.log(`✓ ${msg}`); }; | ||
|
|
||
| const BLOCKED_URLS = [ | ||
| 'http://169.254.169.254/latest/meta-data/iam/security-credentials/', // AWS metadata | ||
| 'http://[fd00::1]/', // IPv6 unique-local | ||
| 'http://127.0.0.1:8080/admin', // loopback | ||
| 'http://localhost/internal', // resolves to loopback | ||
| 'http://10.0.0.1/', // private | ||
| 'http://192.168.1.1/', // private | ||
| 'http://172.16.5.4/', // private | ||
| 'http://[::1]/', // IPv6 loopback | ||
| 'file:///etc/passwd', // non-http scheme | ||
| 'ftp://example.com/secret', // non-http scheme | ||
| ]; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| async function run() { | ||
| for (const url of BLOCKED_URLS) { | ||
| await assert.rejects( | ||
| () => assertUrlIsFetchable(url), | ||
| (err) => err instanceof Error && /not allowed|non-public|Invalid URL|resolve/i.test(err.message), | ||
| `assertUrlIsFetchable should reject ${url}` | ||
| ); | ||
| } | ||
| ok(`assertUrlIsFetchable rejects all ${BLOCKED_URLS.length} SSRF / non-http URLs`); | ||
|
|
||
| // readFile(isUrl) routes through the same guard, so the metadata PoC is | ||
| // blocked before any request leaves the process. | ||
| await assert.rejects( | ||
| () => readFileFromUrl('http://169.254.169.254/latest/meta-data/'), | ||
| /non-public/i, | ||
| 'readFileFromUrl should refuse the cloud metadata endpoint' | ||
| ); | ||
| ok('readFileFromUrl refuses the cloud metadata endpoint'); | ||
|
|
||
| // A public address with a valid scheme passes validation. An IP literal is | ||
| // used so the check needs no outbound DNS and stays hermetic; no request is | ||
| // made — we only assert the guard itself does not reject it. | ||
| await assert.doesNotReject( | ||
| () => assertUrlIsFetchable('https://8.8.8.8/file.txt'), | ||
| 'a public https URL must pass the guard' | ||
| ); | ||
| ok('a public https address passes the guard'); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| run() | ||
| .then(() => { console.log(`\nPASS (${passed}/3)`); process.exit(0); }) | ||
| .catch((e) => { console.error(`\nFAIL: ${e.message}`); process.exit(1); }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.