Security rules

NGB054: Untrusted base URL

Traces untrusted data (URL, messages, storage, HTTP responses, user input) into the document base URL: the href of a <base> element (base.href = v, base.setAttribute('href', v)) and Angular's APP_BASE_HREF provider (useValue or the value returned by useFactory).

Property Value
Rule ID NGB054
Category Angular security & XSS
Default severity high
CWE CWE-79, CWE-601, CWE-829
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

Traces untrusted data (URL, messages, storage, HTTP responses, user input) into the document base URL: the href of a <base> element (base.href = v, base.setAttribute('href', v)) and Angular's APP_BASE_HREF provider (useValue or the value returned by useFactory). Values with a constant same-origin path prefix ('/' + tenant) or a fixed https://host/ prefix are not reported.

Why it matters

The base URL decides where every relative URL resolves. An attacker-chosen <base href="https://evil.example/"> turns the application's own relative <script src>, lazy-loaded chunks, form actions and links into requests to the attacker — script execution without injecting a single script tag. A tainted APP_BASE_HREF makes router links and Location.prepareExternalUrl() point at another origin (open redirect, credential phishing).

Detection behavior and supported patterns

  • <base> element: document.querySelector('base').href = v, createElement('base') + setAttribute('href', v), values typed HTMLBaseElement — high
  • { provide: APP_BASE_HREF, useValue: v } and useFactory: () => v — medium
  • Not reported: a constant prefix such as '/app/' + x, `/${tenant}` after a constant segment, or https://cdn.example.com/ + x; a bare '/' + x prefix is reported with low confidence (x could start with /)
  • Host / X-Forwarded-* headers are left to NGB042

Example

TypeScript
{ provide: APP_BASE_HREF, useFactory: () => new URLSearchParams(location.search).get('base') ?? '/' }

Safer alternative

TypeScript
{ provide: APP_BASE_HREF, useFactory: () => TENANTS.includes(tenantFromPath()) ? `/t/${tenantFromPath()}/` : '/' }

Remediation

Configure the base URL from build-time configuration (<base href> in index.html, --base-href, a constant APP_BASE_HREF). If it must depend on the request (tenants), validate the segment against an allowlist and always prepend a constant / path segment so it cannot become another origin.

False-positive considerations

  • Multi-tenant deployments that derive the base from the first path segment are reported only with a bare / prefix and low confidence; validate the segment against a tenant list.

Known limitations

  • A <base> element written into server-rendered HTML by string concatenation is not followed (see NGB069 for reflected server responses).
  • useFactory dependencies (deps) are not traced into the factory's parameters.

References

Run it

Shell
ngbastion explain NGB054