Security rules

NGB012: Unsafe navigation scheme

Reports URLs that may carry a dangerous scheme (javascript:, vbscript:, data:) into direct browser navigation or DOM URL properties that Angular does not sanitize: untrusted data reaching navigation sinks or anchor/iframe/form/object URL properties without scheme validation, and code that builds javascript: URLs from dynamic values..

Property Value
Rule ID NGB012
Category Navigation & authentication
Default severity high
CWE CWE-79, 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 URLs that may carry a dangerous scheme (javascript:, vbscript:, data:) into direct browser navigation or DOM URL properties that Angular does not sanitize: untrusted data reaching navigation sinks or anchor/iframe/form/object URL properties without scheme validation, and code that builds javascript: URLs from dynamic values.

Why it matters

Navigating to javascript: executes script in the current origin. Angular sanitizes [href]/[src] template bindings, but direct DOM writes (a.href = v, location.href = v, iframe.src = v) and window.open() receive the raw string.

Detection behavior and supported patterns

  • Sinks: location.href = v, location = v, location.assign()/replace(), window.open(), anchor.href = v, iframe.src = v, form.action = v, object.data = v, setAttribute("href"|"src", v) and Renderer2.setAttribute/setProperty on those element types
  • Recognized scheme validation: new URL(v).protocol compared or allowlisted, /^https?:/i.test(v), v.startsWith("https://"), relative paths (v.startsWith("/")), same-origin comparison
  • A constant non-empty prefix ("/profile/" + id, `https://example.com/${x}`) fixes the scheme and is not reported
  • Pattern: a constant javascript: / data: prefix concatenated with a dynamic value

Example

TypeScript
this.link.nativeElement.href = this.route.snapshot.queryParams['next'];

Safer alternative

TypeScript
const next = new URL(this.route.snapshot.queryParams['next'] ?? '/', location.origin);if (next.protocol === 'https:') this.link.nativeElement.href = next.href;

Remediation

Parse the URL with new URL(value, location.origin) and allow only https: (and http: if required) before assigning it, or use Angular template bindings which sanitize unsafe URLs.

False-positive considerations

  • Constant javascript:void(0) links are not reported; only dynamic values or dynamic javascript: construction are.

Known limitations

  • Only validation inside the function containing the sink is considered.
  • Calls to project validation functions in a guard are not analyzed; such findings are kept with low confidence.
  • Template bindings are not reported because Angular sanitizes them.

References

Run it

Shell
ngbastion explain NGB012