Security rules
NGB056: Pass-through Trusted Types policy
Reports Trusted Types policies created with trustedTypes.createPolicy() whose createHTML, createScript or createScriptURL returns its input unchanged without any check.
| Property | Value |
|---|---|
| Rule ID | NGB056 |
| Category | Angular security & XSS |
| 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 Trusted Types policies created with trustedTypes.createPolicy() whose createHTML, createScript or createScriptURL returns its input unchanged without any check. A pass-through default policy is high severity (it silently approves every string assigned to any DOM sink); named pass-through policies are medium.
Why it matters
Trusted Types (enforced through require-trusted-types-for 'script', which Angular supports) is only as strong as the policies the application defines. A policy createHTML: (s) => s converts any string — including attacker-controlled HTML — into TrustedHTML, so the CSP header still claims protection while DOM XSS works as before. A pass-through default policy disables enforcement for the whole page.
Detection behavior and supported patterns
trustedTypes.createPolicy(name, { createHTML: (s) => s })(alsowindow./self./globalThis.trustedTypes, method or function-expression members,String(s),s as string)- Not reported: policies that sanitize (
DOMPurify.sanitize(s)), or contain any branch / throw / validation before returning the input (e.g. an origin allowlist increateScriptURL) - Policy name
'default'→ high, other names → medium
Example
trustedTypes.createPolicy('default', { createHTML: (s) => s, createScriptURL: (s) => s });Safer alternative
trustedTypes.createPolicy('app-html', { createHTML: (s) => DOMPurify.sanitize(s) });trustedTypes.createPolicy('app-scripts', { createScriptURL: (s) => { if (new URL(s, location.href).origin !== location.origin) throw new TypeError('blocked'); return s; } });Remediation
Sanitize in createHTML (DOMPurify.sanitize(s, { RETURN_TRUSTED_TYPE: true })), validate script URLs against an origin allowlist and throw otherwise, and avoid createScript. Do not define a default policy unless it rejects or sanitizes, and limit allowed policy names with the CSP trusted-types directive.
False-positive considerations
- Named pass-through policies that are only ever applied to constant strings are safe in practice; restrict them with the
trusted-typesCSP directive and document an exception.
Known limitations
- Policies defined in libraries (e.g. DOMPurify, Angular) are not inspected.
- The quality of validation in a policy is not assessed: any branch counts as a check.
References
Run it
ngbastion explain NGB056