Security rules
NGB028: Sensitive data on global objects
Reports credentials and payment data assigned to properties of the global object — window.authToken = …, (window as any).__SESSION__ = …, globalThis.apiKey = …, window["cardNumber"] = … — and passed to Object.defineProperty(window, …).
| Property | Value |
|---|---|
| Rule ID | NGB028 |
| Category | Sensitive data & payment security |
| Default severity | medium |
| CWE | CWE-200, CWE-922 |
| 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 credentials and payment data assigned to properties of the global object — window.authToken = …, (window as any).__SESSION__ = …, globalThis.apiKey = …, window["cardNumber"] = … — and passed to Object.defineProperty(window, …). Evidence comes from the property name and the value (name, type, data flow).
Why it matters
Global properties are readable by every script in the page — analytics, tag managers, chat widgets, browser extensions and any XSS payload — without needing access to your modules. Keeping secrets in module scope or injectable services at least requires an attacker to find them; globals are one Object.keys(window) away and frequently end up in error reports.
Detection behavior and supported patterns
window.x = v,globalThis.x = v,self.x = v,window["x"] = v,(window as any).x = vObject.defineProperty(window, "x", { value: v })- Sensitive when the property name or the value is classified as a credential (
accessToken,jwt,apiKey,password,sessionId) or payment data (cardNumber,cvv) window.nameis handled by NGB029; DOM APIs (location,opener, event handlers), PascalCase API names and class / function values (polyfills) are ignored
Example
(window as any).__AUTH__ = { accessToken: res.access_token };Safer alternative
this.session.setAccessToken(res.access_token); // AuthService keeps it in a private fieldRemediation
Keep tokens in an injectable service (in memory) and pass them only to the code that needs them. If a legacy script needs a value, hand it over through a narrow function call instead of a global property.
False-positive considerations
- Public configuration intentionally exposed on
window(e.g. a publishable key) can be reported when its name looks like a credential.
Known limitations
- Globals created through
declare global+ plainvardeclarations in scripts outside the Angular build are not seen. - Assignments through aliases (
const w: any = window; w.token = …) are covered only when the alias is typed asWindow.
References
Run it
ngbastion explain NGB028