-
Notifications
You must be signed in to change notification settings - Fork 2
add URL scheme validation in DAHR and handleWeb2ProxyRequest #465
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a242c53
add URL scheme validation in DAHR and handleWeb2ProxyRequest
ce64b28
refactor: implement centralized URL validation and normalization in D…
bb54060
feat: enhance URL validation to reject embedded credentials, localhos…
4471fa9
feat: enhance URL validation and proxy server security by blocking pr…
a4b798c
feat: improve URL validation and proxy security by blocking additiona…
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| export type UrlValidationResult = | ||
| | { ok: true; normalizedUrl: string } | ||
| | { ok: false; status: 400; message: string } | ||
|
|
||
| /** | ||
| * Validate and normalize a URL for DAHR. | ||
| * - Trims whitespace | ||
| * - Ensures protocol is http(s) | ||
| * - Rejects URLs with embedded credentials (username/password) | ||
| * - Rejects URLs without a hostname | ||
| * - Rejects localhost and loopback IP addresses/hostnames (SSRF protection) | ||
| * - Lowercases host, strips default ports, and removes fragments for canonicalization | ||
| * - Redacts sensitive data in error messages (does not echo the full URL) | ||
| */ | ||
| import net from "node:net" | ||
|
|
||
| export function validateAndNormalizeHttpUrl( | ||
| input: string, | ||
| ): UrlValidationResult { | ||
| const trimmed = (input ?? "").trim() | ||
| if (!trimmed) { | ||
| return { ok: false, status: 400, message: "Invalid URL: empty value" } | ||
| } | ||
| try { | ||
| const parsed = new URL(trimmed) | ||
|
|
||
| // 1. Ensure protocol is http(s) | ||
| if (parsed.protocol !== "http:" && parsed.protocol !== "https:") { | ||
| return { | ||
| ok: false, | ||
| status: 400, | ||
| message: "Invalid URL scheme. Only http(s) are allowed", | ||
| } | ||
| } | ||
|
|
||
| // 2. Reject URLs with embedded credentials (username/password) | ||
| if (parsed.username || parsed.password) { | ||
| return { | ||
| ok: false, | ||
| status: 400, | ||
| message: "Invalid URL: embedded credentials are not allowed", | ||
| } | ||
| } | ||
|
|
||
| // 3. Reject URLs without a hostname | ||
| if (!parsed.hostname) { | ||
| return { | ||
| ok: false, | ||
| status: 400, | ||
| message: "Invalid URL: URL must have a hostname", | ||
| } | ||
| } | ||
|
|
||
| const hostLower = parsed.hostname.toLowerCase() | ||
|
|
||
| // 4. Reject localhost and loopback hostnames | ||
| if (hostLower === "localhost" || hostLower.endsWith(".localhost")) { | ||
| return { | ||
| ok: false, | ||
| status: 400, | ||
| message: "Localhost targets are not allowed", | ||
| } | ||
| } | ||
|
|
||
| // 5. Block loopback and private/link-local/reserved ranges (IPv4, IPv6, and IPv4-mapped IPv6) | ||
| const ipVersion = net.isIP(hostLower) | ||
| const isIPv6Unspecified = hostLower === "::" | ||
| const isIPv6Loopback = hostLower === "::1" | ||
| const isIPv6Multicast = hostLower.startsWith("ff") | ||
| const isIPv4MappedLoopback = hostLower.startsWith("::ffff:127.") | ||
| const isIPv4Loopback = /^127(?:\.\d{1,3}){3}$/.test(hostLower) | ||
| const isIPv4Private = | ||
| /^10\./.test(hostLower) || | ||
| (/^172\.(\d{1,3})\./.test(hostLower) && | ||
| (() => { | ||
| const m = hostLower.match(/^172\.(\d{1,3})\./) | ||
| if (!m) return false | ||
| const o = Number(m[1]) | ||
| return o >= 16 && o <= 31 | ||
| })()) || | ||
| /^192\.168\./.test(hostLower) || | ||
| /^169\.254\./.test(hostLower) || | ||
| /^100\.(6[4-9]|[7-9]\d|1[01]\d|12[0-7])\./.test(hostLower) || // 100.64.0.0/10 | ||
| /^0\./.test(hostLower) | ||
| const isIPv6ULAorLL = | ||
| ipVersion === 6 && | ||
| (hostLower.startsWith("fc") || | ||
| hostLower.startsWith("fd") || | ||
| /^fe[89ab][0-9a-f]*:/i.test(hostLower)) | ||
| // Also block IPv4-mapped private ranges (::ffff:10.x, ::ffff:172.16-31.x, ::ffff:192.168.x, ::ffff:169.254.x, ::ffff:100.64-127.x) | ||
| const mappedMatch = hostLower.match(/^::ffff:(\d+\.\d+\.\d+\.\d+)$/) | ||
| let isMappedPrivate = false | ||
| if (mappedMatch) { | ||
| const v4 = mappedMatch[1] | ||
| if (/^127(?:\.\d{1,3}){3}$/.test(v4)) isMappedPrivate = true | ||
| if (/^10\./.test(v4)) isMappedPrivate = true | ||
| const m172 = v4.match(/^172\.(\d{1,3})\./) | ||
| if (m172) { | ||
| const o = Number(m172[1]) | ||
| if (o >= 16 && o <= 31) isMappedPrivate = true | ||
| } | ||
| if (/^192\.168\./.test(v4)) isMappedPrivate = true | ||
| if (/^169\.254\./.test(v4)) isMappedPrivate = true | ||
| if (/^100\.(6[4-9]|[7-9]\d|1[01]\d|12[0-7])\./.test(v4)) | ||
| isMappedPrivate = true | ||
| if (/^0\./.test(v4)) isMappedPrivate = true | ||
| } | ||
| if ( | ||
| isIPv6Unspecified || | ||
| isIPv6Loopback || | ||
| isIPv6Multicast || | ||
| isIPv4MappedLoopback || | ||
| isIPv4Private || | ||
| isMappedPrivate || | ||
| isIPv6ULAorLL | ||
| ) { | ||
| return { | ||
| ok: false, | ||
| status: 400, | ||
| message: | ||
| "Private, link-local, or loopback targets are not allowed", | ||
| } | ||
| } | ||
|
|
||
| // 6. Canonicalize the URL (lowercase host, strip default ports, remove fragment) | ||
| const canonicalUrlObject = new URL(parsed.toString()) | ||
| canonicalUrlObject.hostname = canonicalUrlObject.hostname.toLowerCase() | ||
|
|
||
| // Strip default ports | ||
| if ( | ||
| (canonicalUrlObject.protocol === "http:" && | ||
| canonicalUrlObject.port === "80") || | ||
| (canonicalUrlObject.protocol === "https:" && | ||
| canonicalUrlObject.port === "443") | ||
| ) { | ||
| canonicalUrlObject.port = "" | ||
| } | ||
|
|
||
| // Remove fragment | ||
| canonicalUrlObject.hash = "" | ||
|
|
||
| return { ok: true, normalizedUrl: canonicalUrlObject.toString() } | ||
| } catch { | ||
| return { ok: false, status: 400, message: "Invalid URL format" } | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SSRF gaps remain: DNS and redirects can still target private/metadata IPs.
Blocking only localhost/loopback at parse-time is insufficient. Attackers can use a public hostname that resolves to 169.254.169.254, 10.0.0.0/8, fc00::/7, etc., or abuse redirects. Enforce IP allow/block after DNS resolution and on every redirect in the Proxy layer.
Action:
Want a patch against Proxy to add a guarded lookup + redirect guard?
🤖 Prompt for AI Agents