Security rules

NGB002: Unsafe resource trust bypass

Reports bypassSecurityTrustUrl(), bypassSecurityTrustResourceUrl() and bypassSecurityTrustScript() calls whose argument is traced to an untrusted source.

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

Reports bypassSecurityTrustUrl(), bypassSecurityTrustResourceUrl() and bypassSecurityTrustScript() calls whose argument is traced to an untrusted source. Constant URLs are not reported. When the value is a fixed https://host/ prefix with only a dynamic path/query, confidence is lowered because the destination origin cannot change.

Why it matters

Resource URLs decide what is loaded into <iframe src>, <script src> or <object data>. Marking an attacker-influenced URL as trusted lets the attacker choose the loaded document or script (javascript: URLs, attacker-hosted frames). Trusting a script value executes it directly.

Detection behavior and supported patterns

  • DomSanitizer.bypassSecurityTrustUrl / bypassSecurityTrustResourceUrl / bypassSecurityTrustScript(value)
  • Same source and propagation model as NGB001
  • Fixed-origin detection: template literal or concatenation starting with a constant https://host/ prefix
  • Recognized sanitizers: sanitizer.sanitize(SecurityContext.URL|RESOURCE_URL, …), encodeURIComponent() (URL contexts), trustedSanitizers

Example

TypeScript
this.frameUrl = this.sanitizer.bypassSecurityTrustResourceUrl(this.route.snapshot.queryParams['src']);

Safer alternative

TypeScript
const id = this.route.snapshot.queryParamMap.get('v') ?? '';if (/^[\w-]{11}$/.test(id)) {  this.frameUrl = this.sanitizer.bypassSecurityTrustResourceUrl(`https://www.youtube-nocookie.com/embed/${id}`);}

Remediation

Avoid trust bypass for dynamic URLs. Build resource URLs from a constant origin plus a validated identifier (/^[\w-]{11}$/ for video IDs) or map keys to a constant allowlist. Never trust dynamic script values.

False-positive considerations

  • Embeds from a trusted provider whose ID comes from the URL (https://www.youtube.com/embed/${id}) are reported with reduced confidence; validate the ID format to be explicit.

Known limitations

  • Allowlist checks performed before the call (e.g. if (ALLOWED.includes(url))) are not yet credited; such findings keep their confidence. Use an exception with justification.
  • bypassSecurityTrustStyle() is not covered (style injection has a much smaller impact in modern browsers).

References

Run it

Shell
ngbastion explain NGB002