Security rules

NGB042: Host header injection in SSR

Traces the client-controlled Host, X-Forwarded-Host, X-Forwarded-Proto and Forwarded request headers (Express req.headers / req.get() / req.hostname / req.protocol, Node IncomingMessage, and the Fetch Request from Angular's REQUEST token) into security-sensitive URLs built by server-side code: outgoing requests (SSRF), provider values used as API base URLs, redirects / Location headers, and URLs rendered into meta tags or links..

Property Value
Rule ID NGB042
Category Angular SSR security
Default severity high
CWE CWE-644, CWE-918, 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

Traces the client-controlled Host, X-Forwarded-Host, X-Forwarded-Proto and Forwarded request headers (Express req.headers / req.get() / req.hostname / req.protocol, Node IncomingMessage, and the Fetch Request from Angular's REQUEST token) into security-sensitive URLs built by server-side code: outgoing requests (SSRF), provider values used as API base URLs, redirects / Location headers, and URLs rendered into meta tags or links.

Why it matters

The Host header is chosen by the client. SSR code that builds absolute URLs from it lets an attacker make the server call internal services or their own host (SSRF), poison cached pages with links to their domain, or redirect users elsewhere. Typical bug: { provide: BASE_URL, useValue: ${req.protocol}://${req.get('host')} } so every server-side HttpClient request — with forwarded cookies — goes to the attacker.

Detection behavior and supported patterns

  • Sources: req.headers.host, req.headers['x-forwarded-host'], req.get('host'), req.header('x-forwarded-proto'), req.hostname, req.host, req.protocol (Express / Node request types), request.headers.get('host') (Fetch Request / Headers)
  • Sinks: HttpClient / fetch request URLs (high), { provide: …, useValue: … } provider values (high), res.redirect(), res.location(), Location headers, Meta.updateTag/addTag({ content }), setAttribute("href" | "src" | "content", …) (medium)
  • Validation recognized: a dominating allowlist or equality check on the host value (ALLOWED_HOSTS.includes(host), host === CANONICAL_HOST)

Example

TypeScript
providers: [{ provide: API_BASE_URL, useValue: `${req.protocol}://${req.get('host')}/api` }]

Safer alternative

TypeScript
providers: [{ provide: API_BASE_URL, useValue: process.env['PUBLIC_ORIGIN'] + '/api' }]

Remediation

Build absolute URLs from configuration (a constant canonical origin or an environment variable), not from request headers. If you need the request host, compare it against an allowlist first and enable Angular SSR's host allowlist for the server engine.

False-positive considerations

  • Deployments behind a proxy that overwrites Host with a fixed value are not exploitable; document an exception that names the proxy setting.

Known limitations

  • Proxy configuration (app.set('trust proxy', …)) and Angular's host allowlisting in the SSR engine (allowedHosts) are not evaluated; a finding may be unexploitable if the deployment already rejects unknown hosts.
  • Headers copied into objects or services before use are followed only as far as the taint engine models the flow.

References

Run it

Shell
ngbastion explain NGB042