Skip to content

Commit a8aac31

Browse files
Add qhelp
1 parent 8983898 commit a8aac31

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>
7+
Operations that allow for mass assignment (setting multiple attributes of an object using a hash), such as <code>ActiveRecord::Base.new</code>, should take care not to
8+
allow arbitrary parameters to be set by the user. Otherwise, unintended attributes may be set, such as an <code>isAdmin</code> feild for a <code>User</code> object.
9+
</p>
10+
</overview>
11+
<recommendation>
12+
<p>
13+
When using a mass assignment operation from user supplied parameters, use <code>ActionController::Parameters#permit</code> to restrict the possible parameters
14+
a user can supply, rather than <code>ActionController::Parameters#permit!</code>, which permits arbitrary parameters to be used for mass assignment.
15+
</p>
16+
</recommendation>
17+
<example>
18+
<p>
19+
In the following example, <code>permit!</code> is used which allows arbitrary parameters to be supplied by the user.
20+
</p>
21+
<sample src="examples/MassAssignmentBad.rb" />
22+
<p>
23+
24+
</p>
25+
<p>
26+
In the following example, only specific parameters are permitted, so the mass assignment is safe.
27+
</p>
28+
<sample src="examples/MassAssignmentGood.rb" />
29+
</example>
30+
31+
<references>
32+
33+
</references>
34+
</qhelp>

ruby/ql/src/queries/security/cwe-915/MassAssignment.ql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @name Insecure Mass Assignment
3-
* @description Using mass assignment with user-controlled keys allows unintended parameters to be set.
3+
* @description Using mass assignment with user-controlled attributes allows unintended parameters to be set.
44
* @kind path-problem
55
* @problem.severity error
66
* @security-severity 7.5
@@ -15,4 +15,6 @@ import MassAssignmentFlow::PathGraph
1515

1616
from MassAssignmentFlow::PathNode source, MassAssignmentFlow::PathNode sink
1717
where MassAssignmentFlow::flowPath(source, sink)
18-
select sink.getNode(), source, sink, "mass assignment"
18+
select sink.getNode(), source, sink,
19+
"This mass assignment operation can assign user-controlled attributes from $@.", source.getNode(),
20+
"this remote flow source"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class UserController < ActionController::Base
2+
def create
3+
# BAD: arbitrary params are permitted to be used for this assignment
4+
User.new(user_params).save!
5+
end
6+
7+
def user_params
8+
params.require(:user).permit!
9+
end
10+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class UserController < ActionController::Base
2+
def create
3+
# GOOD: the permitted parameters are explicitly specified
4+
User.new(user_params).save!
5+
end
6+
7+
def user_params
8+
params.require(:user).permit(:name, :email)
9+
end
10+
end

0 commit comments

Comments
 (0)