Class: Scryer::Rules::PathTraversalRule

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

Overview

Flags a filesystem operation (File.join/read/open/new/write/delete, Dir.glob/entries, send_file) where an argument references params directly — without sanitization, ../../etc/passwd-style path segments in the request let an attacker read (or write/delete) files outside whatever directory the code intended to restrict access to.

Constant Summary collapse

DANGEROUS_CALLS =
{
  "File" => %w[join read open new write delete binread binwrite],
  "Dir" => %w[glob entries]
}.freeze
BARE_METHODS =
%w[send_file send_data].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



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
48
49
50
51
52
# File 'lib/scryer/rules/path_traversal_rule.rb', line 20

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)
    next unless args.any? { |a| Ast.references_params?(a) && !sanitized_via_basename?(a) }

    line = Ast.line_of(node)
    findings << finding(
      line: line,
      message: "`#{describe_call(receiver, method_name)}` is called with a path/argument " \
                "that references `params` — a value like `../../config/master.key` reaches " \
                "the filesystem unchanged, letting an attacker read or write files outside " \
                "whatever directory this was meant to be scoped to.",
      suggested_fix: "Reduce the input to a safe basename before using it " \
                      "(`File.basename(params[:name])`), and/or verify the resolved path " \
                      "stays inside the intended directory (compare " \
                      "`File.expand_path(...)` against the allowed root) before touching " \
                      "the filesystem."
    )
  end

  findings
end