Security rules
NGB063: DOM-clobberable global in a sensitive sink
Reports script / URL / HTML sinks (script.src, href, setAttribute('src' | 'href' | 'action'), import(), importScripts(), new Worker(), location.assign(), innerHTML, document.write()) whose value comes from a non-standard property of window / document — window.CONFIG.cdnUrl, (window as any).appSettings || {}, document.trustedHost — that the application never assigns itself.
| Property | Value |
|---|---|
| Rule ID | NGB063 |
| Category | Angular security & XSS |
| Default severity | medium |
| CWE | CWE-79, CWE-471 |
| 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
Reports script / URL / HTML sinks (script.src, href, setAttribute('src' | 'href' | 'action'), import(), importScripts(), new Worker(), location.assign(), innerHTML, document.write()) whose value comes from a non-standard property of window / document — window.CONFIG.cdnUrl, (window as any).appSettings || {}, document.trustedHost — that the application never assigns itself. Such globals can be created by injected HTML: an element with id="CONFIG" becomes window.CONFIG, and <form name="x"> becomes document.x (DOM clobbering).
Why it matters
HTML sanitizers (Angular's, DOMPurify with default settings) keep id and name attributes, so markup that cannot run script can still define globals. Code that treats an optional global as trusted configuration — the classic const cfg = window.CONFIG || {}; script.src = cfg.cdn + '/x.js' — then loads attacker-chosen script, navigates to javascript: URLs, or writes HTML: a script-less HTML injection becomes full XSS.
Detection behavior and supported patterns
- Global reads:
window.X,self.X,globalThis.X,window["X"],(window as any).X,document.XwhereXis not declared by the TypeScript DOM / ES libraries - Sinks:
.src/.href/.action/.formAction/.innerHTML/.outerHTML/.srcdocassignments,setAttribute("src" | "href" | "action" | "formaction" | "srcdoc" | "data")on DOM elements,import(),importScripts(),new Worker(),location.assign/replace(),document.write() - Followed: member chains (
window.cfg.cdn.url),+/ template concatenation and localconst/letvariables in the same file - Medium confidence when the global has a fallback (
window.X || {}/??), low otherwise - Not reported: globals the application assigns (
window.CONFIG = { … }) without reading the old value
Example
const cfg = (window as any).APP_CONFIG || {};script.src = cfg.cdn + '/widget.js';Safer alternative
const cfg = inject(APP_CONFIG); // provided from environment.tsscript.src = cfg.cdn + '/widget.js';Remediation
Do not read security-relevant configuration from optional globals. Inject it through Angular DI (InjectionToken provided from a build-time environment or a fetched JSON), or verify the type explicitly (typeof cfg.cdn === 'string' && !(cfg instanceof Element)) and validate URLs against an allowlist. Enable DOMPurify's SANITIZE_NAMED_PROPS when rendering untrusted HTML.
False-positive considerations
- Configuration injected by the server into a
<script>before the application loads is not clobberable in practice; if it is assigned outside TypeScript sources, document an exception.
Known limitations
- Globals passed through functions, services or Angular DI before reaching the sink are not followed.
- Whether attacker HTML can reach the page (and with which attributes) is not assessed; this is a gadget, not a complete exploit chain.
- Globals assigned by a separate inline
<script>in index.html are not seen and may still be reported.
References
Run it
ngbastion explain NGB063