Class: Scryer::Rules::SsrfRule

Inherits:
Scryer::Rule show all
Defined in:
lib/scryer/rules/ssrf_rule.rb

Overview

Flags an outbound HTTP call (Net::HTTP, URI.open/bare open via open-uri, HTTParty, Faraday, RestClient) where an argument references params directly — the request's destination is user-controlled, which lets an attacker make the server issue requests to internal services, cloud metadata endpoints, or anywhere else on its network (server-side request forgery).

Constant Summary collapse

DANGEROUS_CALLS =
{
  "Net::HTTP" => %w[get get_response post post_form],
  "URI" => %w[open],
  "HTTParty" => %w[get post put delete],
  "Faraday" => %w[get post put delete],
  "RestClient" => %w[get post put delete]
}.freeze

Instance Attribute Summary

Attributes inherited from Scryer::Rule

#file, #sexp, #source

Instance Method Summary collapse

Methods inherited from Scryer::Rule

inherited, #initialize

Constructor Details

This class inherits a constructor from Scryer::Rule

Instance Method Details

#scanObject



26
27
28
29
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
# File 'lib/scryer/rules/ssrf_rule.rb', line 26

def scan
  findings = []

  Ast.each_node(sexp) do |node|
    next unless Ast.tagged?(node, :method_add_arg, :command, :command_call)

    inner = Ast.tagged?(node, :method_add_arg) ? node[1] : node
    receiver_and_name = Ast.call_name(inner)
    next unless receiver_and_name

    receiver, method_name = receiver_and_name
    next unless dangerous_call?(receiver, method_name)

    args = Ast.call_arguments(node)
    matched_arg = args.find { |a| Ast.references_params?(a) }
    next unless matched_arg

    line = Ast.line_of(node)
    if (fixed_host = fixed_host_with_dynamic_path(matched_arg))
      findings << finding(
        line: line,
        severity: "warning",
        message: "`#{describe_call(receiver, method_name)}` is called with a URL whose host " \
                  "(`#{fixed_host}`) is a fixed literal — only a path/query segment is " \
                  "`params`-derived, so this can't redirect the request to an arbitrary " \
                  "attacker-chosen host the way a fully dynamic URL could. Still worth a " \
                  "look: a params-derived path segment can enumerate/probe endpoints on " \
                  "that fixed host, or (if the target app doesn't validate it either) reach " \
                  "unintended paths there.",
        suggested_fix: "If the path segment is meant to be one of a known set of values " \
                        "(an id, an enum), validate/allowlist it rather than interpolating " \
                        "`params` straight into the URL — a stray `/` or `..` segment can " \
                        "still change which path on #{fixed_host} gets requested."
      )
    else
      findings << finding(
        line: line,
        message: "`#{describe_call(receiver, method_name)}` is called with a URL/argument " \
                  "that references `params` — the server can be made to issue a request to " \
                  "any host an attacker chooses, including internal services and cloud " \
                  "metadata endpoints (e.g. `169.254.169.254`) that aren't meant to be " \
                  "reachable from outside.",
        suggested_fix: "Validate the destination against an allowlist of known-safe hosts " \
                        "before making the request, rather than passing the user-supplied " \
                        "value straight through — and reject internal/link-local IP ranges " \
                        "explicitly if the allowlist is host-based (DNS can still resolve to one)."
      )
    end
  end

  findings
end