Class: Scryer::Rules::OpenRedirectRule

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

Overview

Flags redirect_to called with params[...] (or plain params) directly as the destination — an attacker can craft a link to your own site that redirects the victim onward to an attacker-controlled domain (a classic phishing enabler), unless the value is validated against an allow-list first.

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



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
# File 'lib/scryer/rules/open_redirect_rule.rb', line 14

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
    name_pair = Ast.call_name(inner)
    next unless name_pair && name_pair[1] == "redirect_to"

    args = Ast.call_arguments(node)
    next if args.empty?

    target = args.first
    next unless references_raw_params?(target)

    line = Ast.line_of(target) || Ast.line_of(node)
    findings << finding(
      line: line,
      message: "`redirect_to` receives `params` (or a subscript of it) directly as the " \
                "destination — a crafted link can redirect users from your domain to an " \
                "attacker-controlled site, which is commonly used for phishing.",
      suggested_fix: "Validate the destination against an allow-list before redirecting, " \
                      "e.g. `redirect_to params[:next] if ALLOWED_PATHS.include?(params[:next])`, " \
                      "or only allow relative paths on your own host " \
                      "(`URI.parse(params[:next]).host.nil?`), falling back to a safe default otherwise."
    )
  end

  findings
end