Security rules

NGB005: Untrusted dynamic script loading

Tracks <script> elements created with document.createElement("script") or Renderer2.createElement("script") and reports when their src (or inline text) comes from an untrusted source, is fully dynamic with an unknown origin, or uses plain http://.

Property Value
Rule ID NGB005
Category Angular security & XSS
Default severity high
CWE CWE-829
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

Tracks <script> elements created with document.createElement("script") or Renderer2.createElement("script") and reports when their src (or inline text) comes from an untrusted source, is fully dynamic with an unknown origin, or uses plain http://. Also covers dynamic import() with a non-constant specifier and importScripts().

Why it matters

A script loaded from an attacker-chosen URL runs with full access to your application. Plain-HTTP script URLs can be replaced by anyone on the network path.

Detection behavior and supported patterns

  • const s = document.createElement("script"); s.src = url / s.setAttribute("src", url) / s.text|textContent|innerHTML = code
  • const s = renderer.createElement("script"); renderer.setAttribute(s, "src", url) / renderer.setProperty(s, "src", url)
  • import(specifier) and importScripts(url) with a non-literal argument
  • Constant https:// URLs and URLs with a fixed https://host/ prefix are treated as explicitly trusted locations
  • Script elements given a constant non-JavaScript type (e.g. application/ld+json structured data) are data blocks and are not reported

Example

TypeScript
const s = document.createElement('script');s.src = this.route.snapshot.queryParams['widget'];document.body.appendChild(s);

Safer alternative

TypeScript
const WIDGETS = { chat: 'https://cdn.example.com/chat.v3.js' } as const;const s = document.createElement('script');s.src = WIDGETS.chat;document.body.appendChild(s);

Remediation

Load third-party scripts only from constant HTTPS URLs (ideally with Subresource Integrity via s.integrity and s.crossOrigin = "anonymous"). Map dynamic choices to a constant allowlist instead of accepting URLs.

False-positive considerations

  • Loader helpers that receive URLs only from your own configuration may be reported with low confidence when that configuration is loaded at runtime.

Known limitations

  • Only script elements stored in a local variable or class field are tracked; elements passed through helper functions are not.
  • Script tags injected via HTML strings are covered by NGB003, not this rule.

Run it

Shell
ngbastion explain NGB005