Class: Spektr::Checks::Xss

Inherits:
Base
  • Object
show all
Defined in:
lib/spektr/checks/xss.rb

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#app_version_between?, #dupe?, #model_attribute?, #should_run?, #target_affected?, #user_input?, #version_affected, #version_between?, #warn!

Constructor Details

#initialize(app, target) ⇒ Xss

Returns a new instance of Xss.



4
5
6
7
8
9
# File 'lib/spektr/checks/xss.rb', line 4

def initialize(app, target)
  super
  @name = "XSS"
  @type = "Cross-Site Scripting"
  @targets = ["Spektr::Targets::Base", "Spektr::Targets::Controller", "Spektr::Targets::View"]
end

Instance Method Details

#runObject

TODO: tests for haml, xml, js TODO: add check for raw calls



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/spektr/checks/xss.rb', line 13

def run
  return unless super
  calls = @target.find_calls(:safe_expr_append=)
  calls.concat(@target.find_calls(:raw))
  calls.each do |call|
    call.arguments.each do |argument|
      if user_input?(argument.type, argument.name, argument.ast)
        warn! @target, self, call.location, "Cross-Site Scripting: Unescaped user input"
      end
      if model_attribute?(argument)
        warn! @target, self, call.location, "Cross-Site Scripting: Unescaped model attribute #{argument.name}"
      end
    end
  end
  calls.each do |call|
    call.arguments.each do |argument|
      if user_input?(argument.type, argument.name, argument.ast)
        warn! @target, self, call.location, "Cross-Site Scripting: Unescaped user input"
      end
      if model_attribute?(argument)
        warn! @target, self, call.location, "Cross-Site Scripting: Unescaped model attribute #{argument.name}"
      end
    end
  end
  calls = @target.find_calls(:html_safe)
  calls.each do |call|
    if user_input?(call.receiver.type, call.receiver.name, call.receiver.ast, call.receiver)
      warn! @target, self, call.location, "Cross-Site Scripting: Unescaped user input"
    end
    if model_attribute?(call.receiver)
      warn! @target, self, call.location, "Cross-Site Scripting: Unescaped model attribute #{call.receiver.name}"
    end
  end
end