File tree Expand file tree Collapse file tree 3 files changed +19
-21
lines changed
ruby/ql/src/queries/security/cwe-1333 Expand file tree Collapse file tree 3 files changed +19
-21
lines changed Original file line number Diff line number Diff line change @@ -26,31 +26,12 @@ special meaning.
26
26
The following examples construct regular expressions from an HTTP request
27
27
parameter without sanitizing it first:
28
28
</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" />
42
30
<p >
43
31
Instead, the request parameter should be sanitized first. This ensures that the
44
32
user cannot insert characters that have special meanings in regular expressions.
45
33
</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" />
54
35
</example >
55
36
56
37
<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