Security rules

NGB055: Cross-request state in SSR module scope

In projects that render on the server, reports writes of user- or request-specific data into module-level mutable state (let currentUser, const cache = new Map() at the top of a file) from functions and class members of the Angular application.

Property Value
Rule ID NGB055
Category Angular SSR security
Default severity medium
CWE CWE-488, CWE-200, CWE-362
Built-in presets recommended: warn, strict: error

Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.

What it checks

In projects that render on the server, reports writes of user- or request-specific data into module-level mutable state (let currentUser, const cache = new Map() at the top of a file) from functions and class members of the Angular application. Evidence for "user-specific": credentials / payment data (high), values traced from HTTP responses, the URL, cookies or request headers (medium), or per-user names such as user, session, tenant, cart (low). Writes guarded by isPlatformBrowser() or typeof window !== 'undefined' are ignored.

Why it matters

In the browser every user has their own copy of a module. On the server, one Node.js process renders requests from many users concurrently and modules are evaluated once: a top-level let currentUser set while rendering Alice's request is still there — or already overwritten — while Bob's page renders. Angular's root injector is created per request, so the same data in an injectable service would be isolated; module-level variables are not.

Detection behavior and supported patterns

  • Project with SSR: server.ts / main.server.ts / app.config.server.ts, or ssr / server options in angular.json
  • Top-level let x and const x = new Map() | new Set() | [] | {} in application files (server entry files and Node-only files are excluded)
  • Writes from inside functions: x = v, x.prop = v, x[k] = v, x.set(k, v), x.push(v), x.add(v), Object.assign(x, v)

Example

TypeScript
let currentUser: User | undefined;@Injectable({ providedIn: 'root' }) export class Auth {  load() { return this.http.get<User>('/api/me').pipe(tap((u) => (currentUser = u))); }}

Safer alternative

TypeScript
@Injectable({ providedIn: 'root' }) export class Auth {  readonly user = signal<User | undefined>(undefined);  load() { return this.http.get<User>('/api/me').pipe(tap((u) => this.user.set(u))); }}

Remediation

Keep request- and user-specific state in injectable services (root services are per request on the server), signals inside components, or TransferState. For genuinely global caches, key them by something that cannot leak (never by the user) and store only public data.

False-positive considerations

  • Caches of data that is identical for every user (feature flags, translations) are only reported when named or traced like user data; name-only evidence is low confidence.

Known limitations

  • Module state shared through imported objects (import { store } from …; store.user = u) is followed only when the declaration is in the same project.
  • Third-party libraries with process-wide singletons are not inspected.
  • Whether the server process actually serves concurrent users (e.g. prerendering only) is not known; such projects can turn the rule off.

References

Run it

Shell
ngbastion explain NGB055