mirror of
https://github.com/github/codeql.git
synced 2026-04-20 22:44:52 +02:00
Merge pull request #15987 from joefarebrother/ruby-mass-reassignment
Ruby: Add query for insecure mass assignment
This commit is contained in:
4
ruby/ql/src/change-notes/2024-03-22-mass-assignment.md
Normal file
4
ruby/ql/src/change-notes/2024-03-22-mass-assignment.md
Normal file
@@ -0,0 +1,4 @@
|
||||
---
|
||||
category: newQuery
|
||||
---
|
||||
* Added a new query, `rb/insecure-mass-assignment`, for finding instances of mass assignment operations accepting arbitrary parameters from remote user input.
|
||||
34
ruby/ql/src/queries/security/cwe-915/MassAssignment.qhelp
Normal file
34
ruby/ql/src/queries/security/cwe-915/MassAssignment.qhelp
Normal file
@@ -0,0 +1,34 @@
|
||||
<!DOCTYPE qhelp PUBLIC
|
||||
"-//Semmle//qhelp//EN"
|
||||
"qhelp.dtd">
|
||||
<qhelp>
|
||||
<overview>
|
||||
<p>
|
||||
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
|
||||
allow arbitrary parameters to be set by the user. Otherwise, unintended attributes may be set, such as an <code>is_admin</code> field for a <code>User</code> object.
|
||||
</p>
|
||||
</overview>
|
||||
<recommendation>
|
||||
<p>
|
||||
When using a mass assignment operation from user supplied parameters, use <code>ActionController::Parameters#permit</code> to restrict the possible parameters
|
||||
a user can supply, rather than <code>ActionController::Parameters#permit!</code>, which permits arbitrary parameters to be used for mass assignment.
|
||||
</p>
|
||||
</recommendation>
|
||||
<example>
|
||||
<p>
|
||||
In the following example, <code>permit!</code> is used which allows arbitrary parameters to be supplied by the user.
|
||||
</p>
|
||||
<sample src="examples/MassAssignmentBad.rb" />
|
||||
<p>
|
||||
|
||||
</p>
|
||||
<p>
|
||||
In the following example, only specific parameters are permitted, so the mass assignment is safe.
|
||||
</p>
|
||||
<sample src="examples/MassAssignmentGood.rb" />
|
||||
</example>
|
||||
|
||||
<references>
|
||||
<li>Rails guides: <a href="https://guides.rubyonrails.org/action_controller_overview.html#strong-parameters">Strong Parameters</a>.</li>
|
||||
</references>
|
||||
</qhelp>
|
||||
20
ruby/ql/src/queries/security/cwe-915/MassAssignment.ql
Normal file
20
ruby/ql/src/queries/security/cwe-915/MassAssignment.ql
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* @name Insecure Mass Assignment
|
||||
* @description Using mass assignment with user-controlled attributes allows unintended parameters to be set.
|
||||
* @kind path-problem
|
||||
* @problem.severity error
|
||||
* @security-severity 9.8
|
||||
* @precision high
|
||||
* @id rb/insecure-mass-assignment
|
||||
* @tags security
|
||||
* external/cwe/cwe-915
|
||||
*/
|
||||
|
||||
import codeql.ruby.security.MassAssignmentQuery
|
||||
import MassAssignmentFlow::PathGraph
|
||||
|
||||
from MassAssignmentFlow::PathNode source, MassAssignmentFlow::PathNode sink
|
||||
where MassAssignmentFlow::flowPath(source, sink)
|
||||
select sink.getNode(), source, sink,
|
||||
"This mass assignment operation can assign user-controlled attributes from $@.", source.getNode(),
|
||||
"this remote flow source"
|
||||
@@ -0,0 +1,10 @@
|
||||
class UserController < ActionController::Base
|
||||
def create
|
||||
# BAD: arbitrary params are permitted to be used for this assignment
|
||||
User.new(user_params).save!
|
||||
end
|
||||
|
||||
def user_params
|
||||
params.require(:user).permit!
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,10 @@
|
||||
class UserController < ActionController::Base
|
||||
def create
|
||||
# GOOD: the permitted parameters are explicitly specified
|
||||
User.new(user_params).save!
|
||||
end
|
||||
|
||||
def user_params
|
||||
params.require(:user).permit(:name, :email)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user