-
Notifications
You must be signed in to change notification settings - Fork 717
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add check for potential HTTP verb confusion
Fixes #1432
- Loading branch information
1 parent
01a1f7a
commit 462f300
Showing
4 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require 'brakeman/checks/base_check' | ||
|
||
class Brakeman::CheckVerbConfusion < Brakeman::BaseCheck | ||
Brakeman::Checks.add self | ||
|
||
@description = "Check for uses of `request.get?` that might have unintentional behavior" | ||
|
||
#Process calls | ||
def run_check | ||
calls = tracker.find_call(target: :request, methods: [:get?]) | ||
|
||
calls.each do |call| | ||
process_result call | ||
end | ||
end | ||
|
||
def process_result result | ||
@current_result = result | ||
@matched_call = result[:call] | ||
klass = tracker.find_class(result[:location][:class]) | ||
|
||
# TODO: abstract into tracker.find_location ? | ||
if klass.nil? | ||
Brakeman.debug "No class found: #{result[:location][:class]}" | ||
return | ||
end | ||
|
||
method = klass.get_method(result[:location][:method]) | ||
|
||
if method.nil? | ||
Brakeman.debug "No method found: #{result[:location][:method]}" | ||
return | ||
end | ||
|
||
process method[:src] | ||
end | ||
|
||
def process_if exp | ||
if exp.condition == @matched_call | ||
# Found `if request.get?` | ||
|
||
# Do not warn if there is an `elsif` clause | ||
if node_type? exp.else_clause, :if | ||
return exp | ||
end | ||
|
||
warn_about_result @current_result, exp | ||
end | ||
|
||
exp | ||
end | ||
|
||
def warn_about_result result, code | ||
return unless original? result | ||
|
||
confidence = :weak | ||
message = msg('Potential HTTP verb confusion. ', | ||
msg_code('HEAD'), | ||
' is routed like ', | ||
msg_code('GET'), | ||
' but ', | ||
msg_code('request.get?'), | ||
' will return ', | ||
msg_code('false') | ||
) | ||
|
||
warn :result => result, | ||
:warning_type => "HTTP Verb Confusion", | ||
:warning_code => :http_verb_confusion, | ||
:message => message, | ||
:code => code, | ||
:user_input => result[:call], | ||
:confidence => confidence | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class AccountspsController < ApplicationController | ||
def login | ||
if request.get? | ||
# Do something benign | ||
else | ||
# Do something sensitive because it's a POST | ||
# but actually it could be a HEAD :( | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters