-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(api-service): improve url handling for webhook requests #10912
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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,113 @@ | ||
| import * as dns from 'node:dns'; | ||
| import { LRUCache } from 'lru-cache'; | ||
|
|
||
| /* Keep in sync with libs/application-generic/src/utils/ssrf-url-validation.ts (normalizeOutboundHttpUrl + validateUrlSsrf) */ | ||
|
|
||
| /** | ||
| * Resolves a webhook-style URL for outbound HTTP requests. | ||
| * Host-only or path-first values (no scheme) are treated as https, matching axios behavior. | ||
| */ | ||
| export function normalizeOutboundHttpUrl(raw: string): string | null { | ||
| const trimmed = raw.trim(); | ||
|
|
||
| if (!trimmed) { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| const parsed = new URL(trimmed); | ||
|
|
||
| if (parsed.protocol === 'http:' || parsed.protocol === 'https:') { | ||
| return trimmed; | ||
| } | ||
|
|
||
| return null; | ||
| } catch { | ||
| // Continue: scheme-less host/path (e.g. example.com/hook) | ||
| } | ||
|
|
||
| const withHttps = `https://${trimmed}`; | ||
|
|
||
| try { | ||
| const parsed = new URL(withHttps); | ||
|
|
||
| if (!parsed.hostname) { | ||
| return null; | ||
| } | ||
|
|
||
| return withHttps; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| const DNS_CACHE = new LRUCache<string, dns.LookupAddress[]>({ | ||
| max: 500, | ||
| ttl: 1000 * 60 * 5, // 5 minutes | ||
| }); | ||
|
|
||
| function isPrivateIp(ip: string): boolean { | ||
| const privateRanges = [ | ||
| /^0\.0\.0\.0$/i, | ||
| /^127\./, | ||
| /^10\./, | ||
| /^172\.(1[6-9]|2[0-9]|3[01])\./, | ||
| /^192\.168\./, | ||
| /^169\.254\./, | ||
| /^::ffff:127\./i, | ||
| /^::ffff:10\./i, | ||
| /^::ffff:172\.(1[6-9]|2[0-9]|3[01])\./i, | ||
| /^::ffff:192\.168\./i, | ||
| /^::ffff:169\.254\./i, | ||
| /^::1$/, | ||
| /^fc00:/i, | ||
| /^fe80:/i, | ||
| ]; | ||
|
|
||
| return privateRanges.some((range) => range.test(ip)); | ||
| } | ||
|
|
||
| /** | ||
| * Validates that a URL is safe to fetch server-side (http/https only, no private IPs after DNS resolution). | ||
| * Returns an error message string if blocked, or null if allowed. | ||
| */ | ||
| export async function validateUrlSsrf(url: string): Promise<string | null> { | ||
| let parsed: URL; | ||
|
|
||
| try { | ||
| parsed = new URL(url); | ||
| } catch { | ||
| return 'Invalid URL format.'; | ||
| } | ||
|
|
||
| if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') { | ||
| return `URL scheme "${parsed.protocol}" is not allowed. Only http and https are permitted.`; | ||
| } | ||
|
|
||
| const hostname = parsed.hostname.toLowerCase(); | ||
|
|
||
| const blockedHostnames = ['localhost', 'metadata.google.internal']; | ||
|
|
||
| if (blockedHostnames.includes(hostname)) { | ||
| return `Requests to "${hostname}" are not allowed.`; | ||
| } | ||
|
|
||
| let addresses = DNS_CACHE.get(hostname); | ||
|
|
||
| if (!addresses) { | ||
| try { | ||
| addresses = await dns.promises.lookup(hostname, { all: true }); | ||
| DNS_CACHE.set(hostname, addresses); | ||
| } catch { | ||
| return `Unable to resolve hostname "${hostname}".`; | ||
| } | ||
| } | ||
|
|
||
| for (const { address } of addresses) { | ||
| if (isPrivateIp(address)) { | ||
| return `Requests to private or reserved IP addresses are not allowed (resolved: ${address}).`; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
|
scopsy marked this conversation as resolved.
|
||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.