Class: Brakeman::CheckCrossSiteScripting
- Inherits:
-
BaseCheck
- Object
- SexpProcessor
- BaseCheck
- Brakeman::CheckCrossSiteScripting
- Defined in:
- lib/brakeman/checks/check_cross_site_scripting.rb
Overview
This check looks for unescaped output in templates which contains parameters or model attributes.
For example:
<%= User.find(:id).name %> <%= params %>
Direct Known Subclasses
CheckContentTag, CheckLinkTo, CheckRenderInline, CheckSimpleFormat
Constant Summary collapse
- IGNORE_MODEL_METHODS =
Model methods which are known to be harmless
Set[:average, :count, :maximum, :minimum, :sum, :id]
- MODEL_METHODS =
Set[:all, :find, :first, :last, :new]
- IGNORE_LIKE =
/^link_to_|(_path|_tag|_url)$/
- HAML_HELPERS =
Sexp.new(:colon2, Sexp.new(:const, :Haml), :Helpers)
- XML_HELPER =
Sexp.new(:colon2, Sexp.new(:const, :Erubis), :XmlHelper)
- URI =
Sexp.new(:const, :URI)
- CGI =
Sexp.new(:const, :CGI)
- FORM_BUILDER =
Sexp.new(:call, Sexp.new(:const, :FormBuilder), :new)
Constants inherited from BaseCheck
Constants included from Util
Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, Util::PARAMETERS, Util::PARAMS_SEXP, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_ENV, Util::REQUEST_PARAMETERS, Util::REQUEST_PARAMS, Util::SESSION, Util::SESSION_SEXP
Constants inherited from SexpProcessor
Instance Attribute Summary
Attributes inherited from BaseCheck
Attributes inherited from SexpProcessor
Instance Method Summary collapse
- #actually_process_call(exp) ⇒ Object
- #boolean_method?(method) ⇒ Boolean
- #cgi_escaped?(target, method) ⇒ Boolean
- #check_for_immediate_xss(exp) ⇒ Object
- #form_builder_method?(target, method) ⇒ Boolean
- #haml_escaped?(target, method) ⇒ Boolean
- #ignore_call?(target, method) ⇒ Boolean
- #ignored_method?(target, method) ⇒ Boolean
- #ignored_model_method?(method) ⇒ Boolean
-
#likely_model_attribute?(exp) ⇒ Boolean
Call already involves a model, but might not be acting on a record.
-
#process_call(exp) ⇒ Object
Check a call for user input.
-
#process_cookies(exp) ⇒ Object
Note that cookies have been found.
-
#process_escaped_output(exp) ⇒ Object
Look for calls to raw() Otherwise, ignore.
-
#process_format(exp) ⇒ Object
Process as default.
-
#process_format_escaped(exp) ⇒ Object
Ignore output HTML escaped via HAML.
-
#process_if(exp) ⇒ Object
Ignore condition in if Sexp.
-
#process_output(exp) ⇒ Object
Process an output Sexp.
-
#process_params(exp) ⇒ Object
Note that params have been found.
-
#process_render(exp) ⇒ Object
Ignore calls to render.
-
#process_string_interp(exp) ⇒ Object
Process as default.
- #raw_call?(exp) ⇒ Boolean
-
#run_check ⇒ Object
Run check.
- #safe_input_attribute?(target, method) ⇒ Boolean
- #setup ⇒ Object
- #xml_escaped?(target, method) ⇒ Boolean
Methods inherited from BaseCheck
#add_result, inherited, #initialize, #process_default
Methods included from Util
#array?, #block?, #call?, #camelize, #contains_class?, #context_for, #cookies?, #false?, #file_by_name, #file_for, #github_url, #hash?, #hash_access, #hash_insert, #hash_iterate, #integer?, #make_call, #node_type?, #number?, #params?, #pluralize, #regexp?, #relative_path, #request_env?, #request_value?, #result?, #set_env_defaults, #sexp?, #string?, #symbol?, #table_to_csv, #template_path_to_name, #true?, #truncate_table, #underscore
Methods included from ProcessorHelper
#class_name, #process_all, #process_all!, #process_call_args, #process_class, #process_module
Methods inherited from SexpProcessor
#error_handler, #in_context, #initialize, #process, #process_dummy, #scope
Constructor Details
This class inherits a constructor from Brakeman::BaseCheck
Instance Method Details
#actually_process_call(exp) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 201 def actually_process_call exp return if @matched target = exp.target if sexp? target target = process target end method = exp.method #Ignore safe items if ignore_call? target, method @matched = false elsif sexp? target and model_name? target[1] #TODO: use method call? @matched = Match.new(:model, exp) elsif exp @matched = Match.new(:cookies, exp) elsif @inspect_arguments and params? exp @matched = Match.new(:params, exp) elsif @inspect_arguments process_call_args exp end end |
#boolean_method?(method) ⇒ Boolean
355 356 357 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 355 def boolean_method? method method.to_s.end_with? "?" end |
#cgi_escaped?(target, method) ⇒ Boolean
334 335 336 337 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 334 def cgi_escaped? target, method method == :escape and (target == URI or target == CGI) end |
#check_for_immediate_xss(exp) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 55 def check_for_immediate_xss exp return :duplicate if duplicate? exp if exp.node_type == :output out = exp.value elsif exp.node_type == :escaped_output and raw_call? exp out = exp.value.first_arg end if input = has_immediate_user_input?(out) add_result exp = "Unescaped #{friendly_type_of input}" warn :template => @current_template, :warning_type => "Cross Site Scripting", :warning_code => :cross_site_scripting, :message => , :code => input.match, :confidence => CONFIDENCE[:high] elsif not tracker.[:ignore_model_output] and match = has_immediate_model?(out) method = if call? match match.method else nil end unless IGNORE_MODEL_METHODS.include? method add_result exp if likely_model_attribute? match confidence = CONFIDENCE[:high] else confidence = CONFIDENCE[:med] end = "Unescaped model attribute" link_path = "cross_site_scripting" warning_code = :cross_site_scripting if node_type?(out, :call, :attrasgn) && out.method == :to_json += " in JSON hash" link_path += "_to_json" warning_code = :xss_to_json end code = if match == out nil else match end warn :template => @current_template, :warning_type => "Cross Site Scripting", :warning_code => warning_code, :message => , :code => match, :confidence => confidence, :link_path => link_path end else false end end |
#form_builder_method?(target, method) ⇒ Boolean
347 348 349 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 347 def form_builder_method? target, method target == FORM_BUILDER and @ignore_methods.include? method end |
#haml_escaped?(target, method) ⇒ Boolean
339 340 341 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 339 def haml_escaped? target, method method == :html_escape and target == HAML_HELPERS end |
#ignore_call?(target, method) ⇒ Boolean
312 313 314 315 316 317 318 319 320 321 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 312 def ignore_call? target, method ignored_method?(target, method) or safe_input_attribute?(target, method) or ignored_model_method?(method) or form_builder_method?(target, method) or haml_escaped?(target, method) or boolean_method?(method) or cgi_escaped?(target, method) or xml_escaped?(target, method) end |
#ignored_method?(target, method) ⇒ Boolean
329 330 331 332 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 329 def ignored_method? target, method target.nil? and (@ignore_methods.include? method or method.to_s =~ IGNORE_LIKE) end |
#ignored_model_method?(method) ⇒ Boolean
323 324 325 326 327 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 323 def ignored_model_method? method @matched and @matched.type == :model and IGNORE_MODEL_METHODS.include? method end |
#likely_model_attribute?(exp) ⇒ Boolean
Call already involves a model, but might not be acting on a record
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 123 def likely_model_attribute? exp return false unless call? exp method = exp.method if MODEL_METHODS.include? method or method.to_s.start_with? "find_by_" true else likely_model_attribute? exp.target end end |
#process_call(exp) ⇒ Object
Check a call for user input
Since we want to report an entire call and not just part of one, use @mark to mark when a call is started. Any dangerous values inside will then report the entire call chain.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 157 def process_call exp if @mark actually_process_call exp else @mark = true actually_process_call exp = nil if @matched unless @matched.type and tracker.[:ignore_model_output] = "Unescaped #{friendly_type_of @matched}" end if and not duplicate? exp add_result exp link_path = "cross_site_scripting" if @known_dangerous.include? exp.method confidence = CONFIDENCE[:high] if exp.method == :to_json += " in JSON hash" link_path += "_to_json" end else confidence = CONFIDENCE[:low] end warn :template => @current_template, :warning_type => "Cross Site Scripting", :warning_code => :xss_to_json, :message => , :code => exp, :user_input => @matched.match, :confidence => confidence, :link_path => link_path end end @mark = @matched = false end exp end |
#process_cookies(exp) ⇒ Object
Note that cookies have been found
231 232 233 234 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 231 def exp @matched = Match.new(:cookies, exp) exp end |
#process_escaped_output(exp) ⇒ Object
Look for calls to raw() Otherwise, ignore
142 143 144 145 146 147 148 149 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 142 def process_escaped_output exp unless check_for_immediate_xss exp if raw_call? exp and not duplicate? exp process exp.value.first_arg end end exp end |
#process_format(exp) ⇒ Object
Process as default
247 248 249 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 247 def process_format exp process_default exp end |
#process_format_escaped(exp) ⇒ Object
Ignore output HTML escaped via HAML
252 253 254 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 252 def process_format_escaped exp exp end |
#process_if(exp) ⇒ Object
Ignore condition in if Sexp
257 258 259 260 261 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 257 def process_if exp process exp.then_clause if sexp? exp.then_clause process exp.else_clause if sexp? exp.else_clause exp end |
#process_output(exp) ⇒ Object
Process an output Sexp
136 137 138 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 136 def process_output exp process exp.value.dup end |
#process_params(exp) ⇒ Object
Note that params have been found
225 226 227 228 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 225 def process_params exp @matched = Match.new(:params, exp) exp end |
#process_render(exp) ⇒ Object
Ignore calls to render
237 238 239 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 237 def process_render exp exp end |
#process_string_interp(exp) ⇒ Object
Process as default
242 243 244 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 242 def process_string_interp exp process_default exp end |
#raw_call?(exp) ⇒ Boolean
308 309 310 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 308 def raw_call? exp exp.value.node_type == :call and exp.value.method == :raw end |
#run_check ⇒ Object
Run check
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 37 def run_check setup tracker.each_template do |name, template| Brakeman.debug "Checking #{name} for XSS" @current_template = template template[:outputs].each do |out| unless check_for_immediate_xss out @matched = false @mark = false process out end end end end |
#safe_input_attribute?(target, method) ⇒ Boolean
351 352 353 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 351 def safe_input_attribute? target, method target and @safe_input_attributes.include? method end |
#setup ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 263 def setup @ignore_methods = Set[:button_to, :check_box, :content_tag, :escapeHTML, :escape_once, :field_field, :fields_for, :h, :hidden_field, :hidden_field, :hidden_field_tag, :image_tag, :label, :link_to, :mail_to, :radio_button, :select, :submit_tag, :text_area, :text_field, :text_field_tag, :url_encode, :url_for, :will_paginate].merge tracker.[:safe_methods] @models = tracker.models.keys @inspect_arguments = tracker.[:check_arguments] @known_dangerous = Set[:truncate, :concat] if version_between? "2.0.0", "3.0.5" @known_dangerous << :auto_link elsif version_between? "3.0.6", "3.0.99" @ignore_methods << :auto_link end if version_between? "2.0.0", "2.3.14" @known_dangerous << :strip_tags end json_escape_on = false initializers = tracker.check_initializers :ActiveSupport, :escape_html_entities_in_json= initializers.each {|result| json_escape_on = true?(result.call.first_arg) } if tracker.config[:rails][:active_support] and true? tracker.config[:rails][:active_support][:escape_html_entities_in_json] json_escape_on = true elsif version_between? "4.0.0", "5.0.0" json_escape_on = true end if !json_escape_on or version_between? "0.0.0", "2.0.99" @known_dangerous << :to_json Brakeman.debug("Automatic to_json escaping not enabled, consider to_json dangerous") else @safe_input_attributes << :to_json Brakeman.debug("Automatic to_json escaping is enabled.") end end |
#xml_escaped?(target, method) ⇒ Boolean
343 344 345 |
# File 'lib/brakeman/checks/check_cross_site_scripting.rb', line 343 def xml_escaped? target, method method == :escape_xml and target == XML_HELPER end |