Enhance IP checks and hostname parsing#84
Conversation
Added checks for IPv4-mapped and IPv4-compatible addresses in is_ip_internal function. Updated hostname parsing in is_url_safe_debug to remove brackets.
Review Summary by QodoEnhance IP checks and hostname parsing for security
WalkthroughsDescription• Enhanced IPv6 internal IP detection with IPv4-mapped and IPv4-compatible address handling • Added checks for additional IPv6 CIDR ranges (64:ff9b:1::/48, 5f00::/8, 3fff::/20, fec0::/10) • Fixed hostname parsing in URL validation by removing IPv6 bracket notation • Imported node:net module for improved IP validation capabilities Diagramflowchart LR
A["URL Input"] --> B["Normalize & Parse"]
B --> C["Extract Hostname"]
C --> D["Remove IPv6 Brackets"]
D --> E["Validate IP Type"]
E --> F["IPv4 Check"]
E --> G["IPv6 Check"]
G --> H["Check IPv4-mapped/Compatible"]
H --> I["Check Extra CIDR Ranges"]
I --> J["Security Decision"]
F --> J
File Changes1. src/helpers.ts
|
Code Review by Qodo
1. Redirect IPv6 normalization gap
|
CI Feedback 🧐A test triggered by this PR failed. Here is an AI-generated analysis of the failure:
|
Up to standards ✅🟢 Issues
|
|
|
||
| const parsed = new URL(u); | ||
| const hostname = parsed.hostname; | ||
| const hostname = parsed.hostname.replace(/^\[|\]$/g, ""); |
There was a problem hiding this comment.
1. Redirect ipv6 normalization gap 🐞 Bug ⛨ Security
is_url_safe() now strips IPv6 literal brackets before internal/DNS checks, but is_redirect_safe() still passes current.hostname directly into is_hostname_resolve_to_internal_ip(), so redirect-hop safety can differ from the main URL safety logic for bracketed IPv6 literals.
Agent Prompt
### Issue description
`is_url_safe()` normalizes IPv6-literal hostnames by stripping `[` and `]` before calling `is_hostname_resolve_to_internal_ip()`, but `is_redirect_safe()` does not. This can make redirect-hop validation inconsistent with the primary URL validation for IPv6 literals.
### Issue Context
- `is_url_safe()` now does `parsed.hostname.replace(/^\[|\]$/g, "")`.
- `is_redirect_safe()` currently passes `current.hostname` directly into `is_hostname_resolve_to_internal_ip()`.
- `is_hostname_resolve_to_internal_ip()` only performs direct-IP classification when `ipaddr.isValid(host)` is true.
### Fix Focus Areas
- src/helpers.ts[451-477]
- src/helpers.ts[310-317]
- src/helpers.ts[504-532]
### Suggested fix
Prefer a single canonical normalization point:
1) Add bracket-stripping inside `is_hostname_resolve_to_internal_ip()` (e.g., `const host = hostname.trim().replace(/^\[|\]$/g, "");`) so *all* callers are protected.
2) Optionally also normalize `current.hostname` in `is_redirect_safe()` for clarity/defense-in-depth.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
No description provided.