Security rules

NGB041: Unsafe inline state serialization

Reports application state written into an inline <script> without escaping, the hand-written alternative to Angular TransferState: HTML strings such as <script>window.__STATE__ = ${JSON.stringify(state)}</script> (server-rendered responses, res.send(), html.replace('</head>', …)), and SSR code that sets textContent / text / innerHTML of a <script> element it created.

Property Value
Rule ID NGB041
Category Angular SSR security
Default severity high
CWE CWE-79, CWE-116
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 application state written into an inline <script> without escaping, the hand-written alternative to Angular TransferState: HTML strings such as `<script>window.__STATE__ = ${JSON.stringify(state)}</script>` (server-rendered responses, res.send(), html.replace('</head>', …)), and SSR code that sets textContent / text / innerHTML of a <script> element it created. Confidence is high when the value is traced to request, HTTP, storage or URL data.

Why it matters

JSON.stringify does not escape <, so a string value containing </script><script>alert(1)</script> (a user's display name, a product review, a query parameter) closes the inline script and injects markup into every page rendered with it: stored or reflected XSS during SSR. Angular's TransferState escapes <, > and & for exactly this reason. Personal data placed in such scripts is also delivered to — and cached by — every intermediary.

Detection behavior and supported patterns

  • Template literals and + concatenations whose constant text opens <script…> before a dynamic value (any type, including application/json and application/ld+json blocks)
  • script.textContent = …, script.text = …, script.innerHTML = … on elements from document.createElement('script') / Renderer2.createElement('script') in SSR files (server.ts, *.server.ts) or under isPlatformServer(…)
  • Recognized escaping: serialize-javascript, devalue, functions named escape… / sanitize…, and .replace(/</g, …)

Example

TypeScript
res.send(html.replace('</head>', `<script>window.__USER__ = ${JSON.stringify(user)}</script></head>`));

Safer alternative

TypeScript
const json = JSON.stringify(user).replace(/</g, '\\u003c');res.send(html.replace('</head>', `<script>window.__USER__ = ${json}</script></head>`));

Remediation

Use Angular TransferState (or provideClientHydration() HTTP transfer cache) instead of hand-written state scripts. If you must inline JSON, escape it: JSON.stringify(state).replace(/</g, '\\u003c') or use serialize-javascript/devalue, and never include tokens or personal data that the page does not need.

False-positive considerations

  • Values that can never contain < (numbers, booleans, enums) serialized this way are reported with medium confidence when their origin is not resolved.

Known limitations

  • Strings assembled across several functions or in templates outside TypeScript (e.g. EJS views) are not followed.
  • Browser-only code that sets a live script element's textContent is not reported (no HTML parsing happens there).

References

Run it

Shell
ngbastion explain NGB041