Security rules

NGB027: Sensitive data in DOM attributes

Reports credentials (tokens, passwords, API keys, session IDs) and payment data (card number, CVV) written into DOM attributes: template bindings such as [attr.data-token]="accessToken", [title], [id], hidden inputs <input type="hidden" [value]="token">, and code calling setAttribute() / Renderer2.setAttribute() / element.dataset.x = …..

Property Value
Rule ID NGB027
Category Sensitive data & payment security
Default severity medium
CWE CWE-200, CWE-312
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 (tokens, passwords, API keys, session IDs) and payment data (card number, CVV) written into DOM attributes: template bindings such as [attr.data-token]="accessToken", [title], [id], hidden inputs <input type="hidden" [value]="token">, and code calling setAttribute() / Renderer2.setAttribute() / element.dataset.x = ….

Why it matters

Attributes are part of the serialized document: they appear in SSR HTML, are captured by session-replay and analytics tools, are visible to every script and browser extension through simple DOM queries ([data-token]), and survive in saved pages. Secrets that only code needs belong in memory, not in markup.

Detection behavior and supported patterns

  • Template attribute/property bindings on reflected attributes of native elements: data-*, aria-*, title, alt, name, id, placeholder, content (on <meta>); on components (<app-x>) only data-* / aria-* (other bindings are inputs)
  • Interpolated attribute values (id="row-{{ r.id }}") are classified by the expressions inside {{ }}; arguments of formatter calls and string literals (translation keys) are ignored
  • <input type="hidden" [value]="…"> (hidden form fields)
  • Expression names classified as credentials or payment data (accessToken, apiKey, password, sessionId, cardNumber, cvv); CSRF tokens, last4, maskedPan and method calls are ignored
  • el.setAttribute(name, v), renderer.setAttribute(el, name, v), el.dataset.key = v where the value is classified as sensitive (by name, type or data flow)

Example

TypeScript
<div class="widget" [attr.data-token]="session.accessToken"></div>

Safer alternative

TypeScript
<div class="widget" [attr.data-user]="session.userId"></div><!-- the widget obtains the token from AuthService in code -->

Remediation

Keep tokens and card data in component or service fields and pass them to code that needs them; never render them into attributes or hidden inputs. For CSRF tokens use Angular's XSRF cookie/header mechanism instead of hidden fields.

False-positive considerations

  • Opaque, non-secret identifiers with credential-like names (e.g. a public tokenId attribute used for testing selectors) can be reported.

Known limitations

  • Template expressions are classified by identifier names only (no type information inside templates).
  • Directive inputs on native elements that share a name with a reflected attribute ([title] consumed by a tooltip directive) are treated as attributes.
  • Attributes set by third-party components from their inputs are not followed.

References

Run it

Shell
ngbastion explain NGB027