regex-injection
Input written into a regular expression.
- Group
- Code
- Runs
- Runs under ur scan:
ur scan --rule regex-injection - Level
- warning
- Reads
- Every file
- Fix
- None: the safe form depends on what the value is meant to be
Why
A value from a request becomes part of a regular expression, given to Regexp.new or Regexp.union or interpolated into a literal, as /#{params[:q]}/ is, so whoever sends it chooses the pattern: what it matches, and one that takes a backtracking matcher exponential time, as (a+)+$ does. Escape the value with Regexp.escape to match it as text, or match it with a linear-time engine such as RE2.
The fix, as Ur suggests it: escape it with Regexp.escape.
For agents
Never write input into a regular expression; escape it with Regexp.escape.
Example
app/controllers/search_controller.rbclass SearchController < ApplicationController
def index
@posts = Post.all.select { |post| post.title =~ /#{params[:q]}/i }
end
end
$ ur scan --rule regex-injection
warning[regex-injection]: `params`, a request parameter, reaches `Regexp.new`
--> ./app/controllers/search_controller.rb:3:53 method `index`
= step 1: ./app/controllers/search_controller.rb:3:56 `params`, a request parameter,
= step 2: ./app/controllers/search_controller.rb:3:53 interpolated into a string
= hint: escape it with `Regexp.escape`
= see: CWE-625: Permissive Regular Expression, MITRE (2024-11-19), https://cwe.mitre.org/data/definitions/625.html
scanned 1 file (no framework recognized)
regex-injection: 1 high, 0 medium
1 finding and 0 of medium confidence
Source
CWE-625: Permissive Regular Expression, MITRE (2024-11-19).