Module: Aikido::Zen::Sinks::Patron::Extensions

Defined in:
lib/aikido/zen/sinks/patron.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.wrap_response(request, response) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/aikido/zen/sinks/patron.rb', line 15

def self.wrap_response(request, response)
  # In this case, automatic redirection happened inside libcurl.
  if response.url != request.url && !response.url.to_s.empty?
    Aikido::Zen::Scanners::SSRFScanner::Response.new(
      status: 302, # We can't know what the actual status was, but we just need a 3XX
      headers: response.headers.merge("Location" => response.url)
    )
  else
    Aikido::Zen::Scanners::SSRFScanner::Response.new(
      status: response.status,
      headers: response.headers
    )
  end
end

Instance Method Details

#handle_request(request) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
# File 'lib/aikido/zen/sinks/patron.rb', line 30

def handle_request(request)
  wrapped_request = Aikido::Zen::Scanners::SSRFScanner::Request.new(
    verb: request.action,
    uri: URI(request.url),
    headers: request.headers
  )

  # Store the request information so the DNS sinks can pick it up.
  if (context = Aikido::Zen.current_context)
    prev_request = context["ssrf.request"]
    context["ssrf.request"] = wrapped_request
  end

  SINK.scan(
    connection: Aikido::Zen::OutboundConnection.from_uri(URI(request.url)),
    request: wrapped_request,
    operation: "request"
  )

  response = super

  Aikido::Zen::Scanners::SSRFScanner.track_redirects(
    request: wrapped_request,
    response: Extensions.wrap_response(request, response)
  )

  # When libcurl has follow_location set, it will handle redirections
  # internally, and expose the response.url as the URI that was last
  # requested in the redirect chain.
  #
  # In this case, we can't actually stop the request from happening, but
  # we can scan again (now that we know another request happened), to
  # stop the response from being exposed to the user. This downgrades
  # the SSRF into a blind SSRF, which is better than doing nothing.
  if request.url != response.url && !response.url.to_s.empty?
    last_effective_request = Aikido::Zen::Scanners::SSRFScanner::Request.new(
      verb: request.action,
      uri: URI(response.url),
      headers: request.headers
    )
    context["ssrf.request"] = last_effective_request if context

    SINK.scan(
      connection: Aikido::Zen::OutboundConnection.from_uri(URI(response.url)),
      request: last_effective_request,
      operation: "request"
    )
  end

  response
ensure
  context["ssrf.request"] = prev_request if context
end