Security rules
NGB011: Untrusted redirect
Reports untrusted data (URL parameters, postMessage data, window.name, referrer, HTTP responses, storage) that controls the destination of a browser navigation — location.href = v, location = v, location.assign()/replace(), window.open() — including redirect helper functions that receive the URL as a parameter.
| Property | Value |
|---|---|
| Rule ID | NGB011 |
| Category | Navigation & authentication |
| Default severity | high |
| CWE | CWE-601 |
| Built-in presets | recommended: error, strict: error |
Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.
What it checks
Reports untrusted data (URL parameters, postMessage data, window.name, referrer, HTTP responses, storage) that controls the destination of a browser navigation — location.href = v, location = v, location.assign()/replace(), window.open() — including redirect helper functions that receive the URL as a parameter. Navigation through the Angular Router (navigate(), navigateByUrl()) is application-internal and never reported.
Why it matters
Open redirects let attackers use your trusted domain to bounce victims to phishing pages (/login?returnUrl=https://evil.example), and they frequently break OAuth / SSO flows that rely on redirect allowlists.
Detection behavior and supported patterns
window.location.href = v,location = v,document.location = v,location.assign(v),location.replace(v),window.open(v)- Redirect helpers:
function go(url) { location.href = url }traced back to its callers - Recognized host validation guarding the sink (if / early return / ternary / &&): same-origin comparison (
new URL(u, location.origin).origin === location.origin), host or origin allowlists (ALLOWED.includes(new URL(u).hostname),.has()), relative paths (u.startsWith("/") && !u.startsWith("//")), prefix checks against a constanthttps://host/or a configured API URL, anchored origin regexes - A constant prefix pinning the destination (
"/account/" + id,`https://example.com/${path}`) is not reported
Example
const returnUrl = this.route.snapshot.queryParamMap.get('returnUrl') ?? '/';window.location.href = returnUrl;Safer alternative
const returnUrl = this.route.snapshot.queryParamMap.get('returnUrl') ?? '/';const target = new URL(returnUrl, location.origin);if (target.origin === location.origin) { this.router.navigateByUrl(target.pathname + target.search);}Remediation
Accept only relative paths or compare the parsed origin with an explicit allowlist before navigating. For in-app navigation, use router.navigateByUrl() with a path.
False-positive considerations
- Destinations already validated by a server-side allowlist before reaching the client.
- Weak checks such as
url.includes("example.com")lower the confidence but are still reported because they are bypassable (https://evil.example/?example.com).
Known limitations
- Only guards inside the function containing the sink are considered; validation performed by a caller of a redirect helper is not seen.
- Validation inside a project function used as a guard (
if (isSafeUrl(u))) is not analyzed; the finding is kept with low confidence. u.startsWith("/") && !u.startsWith("//")is accepted as a relative-path check; browsers also treat/\evil.exampleas protocol-relative, so also reject backslashes.
References
Run it
ngbastion explain NGB011