File tree Expand file tree Collapse file tree
ruby/ql/src/queries/security/cwe-1333 Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -26,31 +26,12 @@ special meaning.
2626The following examples construct regular expressions from an HTTP request
2727parameter without sanitizing it first:
2828</p >
29- <sample language =" ruby" >
30- class UsersController < ActionController::Base
31- def first_example
32- # BAD: Unsanitized user input is used to construct a regular expression
33- regex = /#{ params[:key] }/
34- end
35-
36- def second_example
37- # BAD: Unsanitized user input is used to construct a regular expression
38- regex = Regexp.new(params[:key])
39- end
40- end
41- </sample >
29+ <sample src =" examples/regexp_injection_bad.rb" />
4230<p >
4331Instead, the request parameter should be sanitized first. This ensures that the
4432user cannot insert characters that have special meanings in regular expressions.
4533</p >
46- <sample language =" ruby" >
47- class UsersController < ActionController::Base
48- def example
49- # GOOD: User input is sanitized before constructing the regular expression
50- regex = Regexp.new(Regex.escape(params[:key]))
51- end
52- end
53- </sample >
34+ <sample src =" examples/regexp_injection_good.rb" />
5435</example >
5536
5637<references >
Original file line number Diff line number Diff line change 1+ class UsersController < ActionController ::Base
2+ def first_example
3+ # BAD: Unsanitized user input is used to construct a regular expression
4+ regex = /#{ params [ :key ] } /
5+ end
6+
7+ def second_example
8+ # BAD: Unsanitized user input is used to construct a regular expression
9+ regex = Regexp . new ( params [ :key ] )
10+ end
11+ end
Original file line number Diff line number Diff line change 1+ class UsersController < ActionController ::Base
2+ def example
3+ # GOOD: User input is sanitized before constructing the regular expression
4+ regex = Regexp . new ( Regex . escape ( params [ :key ] ) )
5+ end
6+ end
You can’t perform that action at this time.
0 commit comments