Skip to content

pattern-sink ​

A regular expression compiled from text that isn't a constant the source shows or a RegexSource: new RegExp(text), RegExp(text), and a string given to str.match(text), str.matchAll(text) or str.search(text), which compile it into a pattern too.

Why: a pattern from a user is a program the regex engine runs. (a+)+$ against thirty as and a ! takes seconds, and one more a doubles it: one request holds the server's only thread (ReDoS). A search box that passes its query to search also matches . against any character, and a query with an unbalanced ( throws.

✕ Rejectedpattern-sink
ts
export function mentions(text: string, query: string): boolean {
  return text.search(query) !== -1;
}

✅ Accepted: the query matched literally

✓ Accepted0 diagnostics
ts
import { escapeRegExp } from "@evobug_com/safe-ts-std/regexp";

export function mentions(text: string, query: string): boolean {
  return text.includes(query);
}

export function mentionsWord(text: string, word: string): boolean {
  return new RegExp(`\\b${escapeRegExp(word)}\\b`, "i").test(text);
}

Accepted: a constant the source shows (whose own patterns no-redos checks), escapeRegExp(text) and a value typed RegexSource, numbers, and a pattern built from those with +, a template or .join(sep) (words.map(escapeRegExp).join("|")). It is the definition unhandled-failure uses to decide whether new RegExp can throw: where that rule already reports a new RegExp(text) for its text, this one says nothing more. It reports what that one can't: a new RegExp whose throw is handled (unsafe(() => new RegExp(q)) still runs the pattern) and the string methods, which throw too.

str.replace(text, ...), replaceAll and split given a string match it literally: not sinks.

What the rule can't see: a pattern a library compiles from text it is given (a router's path patterns, a search library's query syntax); a receiver of search whose type the checker can't tell (only a string's counts, so index.search(query) on an object of yours isn't reported).

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra