Security rules

NGB037: Regular expression injection

Reports regular expressions compiled from attacker-controllable text — new RegExp(value), RegExp(value), and str.match(value) / str.search(value) / str.matchAll(value) with a string argument — when the value comes from URL / route parameters, postMessage data, window.name, the referrer or form input and is not passed through an escaping function.

Property Value
Rule ID NGB037
Category Angular security & XSS
Default severity medium
CWE CWE-1333, CWE-400
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 regular expressions compiled from attacker-controllable text — new RegExp(value), RegExp(value), and str.match(value) / str.search(value) / str.matchAll(value) with a string argument — when the value comes from URL / route parameters, postMessage data, window.name, the referrer or form input and is not passed through an escaping function. Code in SSR server files is reported with high severity.

Why it matters

Pattern syntax in the input changes what the expression matches (a search for .* matches everything, a validation regex can be bypassed) and catastrophic backtracking patterns such as (a+)+$ freeze the tab. During server-side rendering the same code runs in Node.js, where one request with a crafted query parameter blocks the event loop for every user (ReDoS).

Detection behavior and supported patterns

  • new RegExp(v, flags), RegExp(v) (global constructor, symbol-resolved)
  • text.match(v), text.search(v), text.matchAll(v) where v is a string (strings are compiled to regular expressions)
  • Sources: ActivatedRoute / location values, postMessage data, window.name, referrer, input values and [(ngModel)] bindings
  • Escaping recognized: functions whose name contains escape, quote or sanitize (e.g. _.escapeRegExp, a project escapeRegex())
  • Files named server.ts, main.server.ts, app.config.server.ts or under server/ raise the severity to high

Example

TypeScript
const q = this.route.snapshot.queryParamMap.get('q') ?? '';this.results = this.items.filter((i) => new RegExp(q, 'i').test(i.name));

Safer alternative

TypeScript
const q = escapeRegExp(this.route.snapshot.queryParamMap.get('q') ?? '');this.results = this.items.filter((i) => new RegExp(q, 'i').test(i.name));

Remediation

Escape user text before building a pattern (value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), lodash escapeRegExp), or avoid regular expressions and use includes() / startsWith() for plain-text matching. Limit input length.

False-positive considerations

  • Search boxes that intentionally accept regular expressions from the user typing them only affect that user; such findings are low confidence.

Known limitations

  • Whether the resulting pattern is exponential is not analyzed; any unescaped untrusted pattern is reported.
  • Code shared between browser and SSR is reported with medium severity unless it is in a server file.

References

Run it

Shell
ngbastion explain NGB037