Class: TaintedLove::Replacer::ReplaceRackRequest

Inherits:
Base
  • Object
show all
Defined in:
lib/tainted_love/replacer/replace_rack_query_parser.rb

Instance Method Summary collapse

Methods inherited from Base

replacers, #should_replace?

Instance Method Details

#replace!Object



7
8
9
10
11
12
# File 'lib/tainted_love/replacer/replace_rack_query_parser.rb', line 7

def replace!
  block = method(:taint_params)

  TaintedLove.proxy_method('Rack::QueryParser', :parse_nested_query, &block)
  TaintedLove.proxy_method('Rack::QueryParser', :parse_query, &block)
end

#taint_params(return_value, *args) ⇒ Object



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
47
# File 'lib/tainted_love/replacer/replace_rack_query_parser.rb', line 14

def taint_params(return_value, *args)
  # Assume that if tainted input uses this method, it's to parse a query string
  # It can also be cookies that are being parsed.
  return unless args.first.tainted?

  # figure out what is being parsed from the the method that called it
  name = if Thread.current.backtrace(4).first["`parse_cookies_header'"]
    'cookies'
  else
    'params'
  end

  taint = lambda do |params, path|
    source = name + path.map { |p| "[#{p.inspect}]" }.join

    if params.is_a?(String)
      TaintedLove.tag(params.taint, source: source, value: params)
    end

    if params.is_a?(Array)
      params.each.with_index do |value, index|
        taint.(value, path + [index])
      end
    end

    if params.is_a?(Hash)
      params.each do |key, value|
        taint.(value, path + [key])
      end
    end
  end

  taint.(return_value, [])
end