Security rules

NGB061: Server-side open redirect

Traces data from the incoming request (query string, route parameters, body, cookies) into redirects issued by SSR / Express server code: res.redirect(), res.location(), Location response headers.

Property Value
Rule ID NGB061
Category Angular SSR security
Default severity medium
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

Traces data from the incoming request (query string, route parameters, body, cookies) into redirects issued by SSR / Express server code: res.redirect(), res.location(), Location response headers. Redirect targets pinned to a path of this application ('/account/' + id) or a fixed origin, and targets checked against an allowlist or same-origin test before the redirect, are not reported. Client-controlled Host / X-Forwarded-* headers are reported by NGB042 instead.

Why it matters

/login?next=https://evil.example that ends in res.redirect(req.query.next) turns your domain into a trusted-looking link to a phishing page, and in OAuth / SSO flows it can leak authorization codes or tokens to the attacker. Server redirects are invisible to browser-side checks such as NGB011.

Detection behavior and supported patterns

  • res.redirect(url) / res.redirect(302, url) and res.location(url) on Express responses (also untyped (req, res) handlers)
  • res.setHeader('Location', url), res.set/header/append('Location', url), res.writeHead(302, { Location: url }) on Express / Node responses
  • Sources: req.query, req.params, req.body, req.path, req.url, req.originalUrl, req.cookies
  • Not reported: a constant prefix that pins the destination (/app/…, https://host/…); dominating same-origin / allowlist / relative-path checks (as NGB011)

Example

TypeScript
app.get('/login/done', (req, res) => res.redirect(String(req.query.next)));

Safer alternative

TypeScript
app.get('/login/done', (req, res) => {  const next = String(req.query.next ?? '/');  res.redirect(next.startsWith('/') && !next.startsWith('//') && !next.startsWith('/\\') ? next : '/');});

Remediation

Redirect only to relative paths of your application or to an allowlist of origins: parse with new URL(next, 'https://app.example'), require url.origin === 'https://app.example' (or a path that starts with / but not // or /\), and fall back to a default page otherwise.

False-positive considerations

  • A reverse proxy that rewrites or blocks external Location headers makes the finding unexploitable; document an exception naming that control.

Known limitations

  • Validation in a separate function or middleware that runs before the handler is not seen; a project validator used as a guard lowers confidence to low.
  • Redirect helpers of other server frameworks (Fastify reply.redirect, Nest @Redirect) are not modeled.

References

Run it

Shell
ngbastion explain NGB061