Security rules

NGB025: Authentication cookie set from JavaScript

Reports session or authentication cookies written by browser code — document.cookie = 'session=…', ngx-cookie-service CookieService.set(), js-cookie Cookies.set() — identified by the cookie name (session, sid, auth_token, access_token, jwt, …) or by a credential-named value.

Property Value
Rule ID NGB025
Category Sensitive data & payment security
Default severity high
CWE CWE-1004, CWE-614
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 session or authentication cookies written by browser code — document.cookie = 'session=…', ngx-cookie-service CookieService.set(), js-cookie Cookies.set() — identified by the cookie name (session, sid, auth_token, access_token, jwt, …) or by a credential-named value. Missing Secure / SameSite attributes are added as evidence.

Why it matters

Cookies created by JavaScript can never be HttpOnly, so any script running in the page (an XSS payload, a compromised third-party script) can read and exfiltrate the session. Session cookies should be issued by the server with HttpOnly; Secure; SameSite. Cookies without Secure are also sent over plain HTTP.

Detection behavior and supported patterns

  • document.cookie = 'token=' + value, template literals, constant cookie strings (name from the constant prefix)
  • cookieService.set(name, value, …) from ngx-cookie-service / ngx-cookie-service-ssr
  • Cookies.set(name, value, attrs) from js-cookie
  • Sensitive names: session, sid, sessionId, auth, auth_token, access_token, refresh_token, id_token, jwt, token, remember_me (and names classified as credentials); CSRF/XSRF cookies are ignored

Example

TypeScript
document.cookie = `access_token=${res.accessToken}; path=/`;

Safer alternative

TypeScript
// Server: res.cookie("__Host-session", id, { httpOnly: true, secure: true, sameSite: "lax", path: "/" });

Remediation

Issue session and token cookies from the server with Set-Cookie: __Host-session=…; HttpOnly; Secure; SameSite=Lax; Path=/. Keep access tokens in memory in the browser instead of cookies or storage.

False-positive considerations

  • Non-secret cookies that happen to use a credential-like name (e.g. a token cookie holding a feature flag) can be reported.

Known limitations

  • Cookie names built dynamically are only reported when the value is identified as a credential.
  • Cookies set through other wrappers or by the server (Set-Cookie, see NGB043) are not covered by this rule.

References

Run it

Shell
ngbastion explain NGB025