scryer 0.3.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +322 -0
- data/README.md +546 -43
- data/lib/generators/scryer/USAGE +10 -2
- data/lib/generators/scryer/templates/scryer_initializer.rb +15 -0
- data/lib/scryer/ai_fix_suggester.rb +29 -9
- data/lib/scryer/ast.rb +95 -6
- data/lib/scryer/authorization_watcher.rb +156 -0
- data/lib/scryer/baseline.rb +75 -0
- data/lib/scryer/cli.rb +209 -11
- data/lib/scryer/dependency_audit.rb +108 -5
- data/lib/scryer/finding.rb +6 -0
- data/lib/scryer/fix_verifier.rb +82 -0
- data/lib/scryer/minitest.rb +48 -0
- data/lib/scryer/performance_rules/inefficient_save_loop_rule.rb +32 -0
- data/lib/scryer/performance_rules/missing_pagination_rule.rb +1 -0
- data/lib/scryer/performance_rules/n_plus_one_query_rule.rb +1 -0
- data/lib/scryer/performance_rules/unbounded_table_scan_rule.rb +1 -0
- data/lib/scryer/report_renderer.rb +637 -44
- data/lib/scryer/rspec.rb +55 -0
- data/lib/scryer/rule.rb +22 -2
- data/lib/scryer/rules/action_cable_forgery_protection_rule.rb +50 -0
- data/lib/scryer/rules/active_storage_inline_disposition_rule.rb +50 -0
- data/lib/scryer/rules/active_storage_missing_content_type_validation_rule.rb +79 -0
- data/lib/scryer/rules/authentication_bypass_rule.rb +95 -0
- data/lib/scryer/rules/command_injection_rule.rb +3 -0
- data/lib/scryer/rules/consider_all_requests_local_rule.rb +51 -0
- data/lib/scryer/rules/cors_misconfiguration_rule.rb +100 -0
- data/lib/scryer/rules/csrf_protection_rule.rb +60 -11
- data/lib/scryer/rules/force_ssl_rule.rb +48 -0
- data/lib/scryer/rules/graphql_missing_query_limits_rule.rb +106 -0
- data/lib/scryer/rules/hardcoded_basic_auth_rule.rb +51 -0
- data/lib/scryer/rules/hardcoded_secret_key_base_rule.rb +58 -0
- data/lib/scryer/rules/hardcoded_secret_rule.rb +3 -0
- data/lib/scryer/rules/host_authorization_disabled_rule.rb +50 -0
- data/lib/scryer/rules/idor_rule.rb +165 -0
- data/lib/scryer/rules/insecure_cookie_serializer_rule.rb +47 -0
- data/lib/scryer/rules/job_raw_params_rule.rb +131 -0
- data/lib/scryer/rules/jwt_insecure_rule.rb +123 -0
- data/lib/scryer/rules/mass_assignment_rule.rb +34 -10
- data/lib/scryer/rules/missing_authorization_rule.rb +103 -0
- data/lib/scryer/rules/missing_policy_scope_rule.rb +134 -0
- data/lib/scryer/rules/open_redirect_rule.rb +3 -0
- data/lib/scryer/rules/path_traversal_rule.rb +110 -0
- data/lib/scryer/rules/security_headers_rule.rb +133 -0
- data/lib/scryer/rules/sql_injection_rule.rb +3 -0
- data/lib/scryer/rules/ssrf_rule.rb +139 -0
- data/lib/scryer/rules/unsafe_deserialization_rule.rb +3 -0
- data/lib/scryer/rules/verbose_production_log_level_rule.rb +53 -0
- data/lib/scryer/rules/weak_crypto_rule.rb +37 -2
- data/lib/scryer/rules/weak_session_cookie_rule.rb +51 -0
- data/lib/scryer/rules/xss_unsafe_html_rule.rb +41 -0
- data/lib/scryer/style_rules/frozen_string_literal_rule.rb +1 -0
- data/lib/scryer/version.rb +1 -1
- data/lib/scryer.rb +15 -0
- data/lib/tasks/scryer.rake +138 -13
- metadata +41 -12
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
module Scryer
|
|
2
|
+
module Rules
|
|
3
|
+
# Flags an explicit insecure override of Rails' default security
|
|
4
|
+
# headers/config — NOT absence of a header, which would require knowing
|
|
5
|
+
# every environment file and initializer in the app (too noisy/
|
|
6
|
+
# environment-dependent for a per-file static rule). Only the explicit
|
|
7
|
+
# opt-out is a reliable, low-noise signal on its own — same reasoning as
|
|
8
|
+
# ForceSslRule.
|
|
9
|
+
#
|
|
10
|
+
# config.action_dispatch.default_headers['X-Frame-Options'] = 'ALLOWALL' (or false)
|
|
11
|
+
# config.action_dispatch.default_headers['X-Content-Type-Options'] = false
|
|
12
|
+
# config.action_dispatch.default_headers.merge!('X-Frame-Options' => 'ALLOWALL', ...)
|
|
13
|
+
# config.content_security_policy = nil
|
|
14
|
+
class SecurityHeadersRule < Rule
|
|
15
|
+
self.rule_id = "security_headers_disabled"
|
|
16
|
+
self.category = "security"
|
|
17
|
+
self.default_severity = "critical"
|
|
18
|
+
self.title = "Rails default security header explicitly disabled"
|
|
19
|
+
self.cwe = "CWE-1021"
|
|
20
|
+
self.owasp_category = "A05:2021-Security Misconfiguration"
|
|
21
|
+
self.confidence = "high"
|
|
22
|
+
|
|
23
|
+
HEADERS = ["X-Frame-Options", "X-Content-Type-Options"].freeze
|
|
24
|
+
|
|
25
|
+
def scan
|
|
26
|
+
findings = []
|
|
27
|
+
|
|
28
|
+
Ast.each_node(sexp) do |node|
|
|
29
|
+
if Ast.tagged?(node, :assign)
|
|
30
|
+
scan_assign(findings, node)
|
|
31
|
+
elsif Ast.tagged?(node, :method_add_arg)
|
|
32
|
+
scan_merge_bang(findings, node)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
findings
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def scan_assign(findings, node)
|
|
42
|
+
target = node[1]
|
|
43
|
+
value = node[2]
|
|
44
|
+
|
|
45
|
+
if Ast.tagged?(target, :aref_field) && default_headers_receiver?(target[1])
|
|
46
|
+
key = Ast.unwrap_args(target[2]).first
|
|
47
|
+
header = Ast.plain_string_value(key)
|
|
48
|
+
add_header_finding(findings, header, value, Ast.line_of(node)) if header
|
|
49
|
+
elsif Ast.tagged?(target, :field) && Ast.ident_text(target[3]) == "content_security_policy"
|
|
50
|
+
return unless Ast.kw_literal?(value) == "nil"
|
|
51
|
+
|
|
52
|
+
findings << finding(
|
|
53
|
+
line: Ast.line_of(node),
|
|
54
|
+
message: "`config.content_security_policy = nil` explicitly disables Content-Security-Policy — " \
|
|
55
|
+
"a header that mitigates XSS/data-injection by restricting which sources scripts, " \
|
|
56
|
+
"styles, and other resources can load from.",
|
|
57
|
+
suggested_fix: "Configure an actual policy instead of nil-ing it out — see " \
|
|
58
|
+
"`config.content_security_policy do |policy| ... end` in " \
|
|
59
|
+
"config/initializers/content_security_policy.rb."
|
|
60
|
+
)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def scan_merge_bang(findings, node)
|
|
65
|
+
inner = node[1]
|
|
66
|
+
name_pair = Ast.call_name(inner)
|
|
67
|
+
return unless name_pair && name_pair[1] == "merge!"
|
|
68
|
+
return unless default_headers_receiver?(name_pair[0])
|
|
69
|
+
|
|
70
|
+
each_assoc_pair(node).each do |key_node, value_node|
|
|
71
|
+
header = Ast.plain_string_value(key_node)
|
|
72
|
+
add_header_finding(findings, header, value_node, Ast.line_of(node)) if header
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def add_header_finding(findings, header, value_node, line)
|
|
77
|
+
return unless HEADERS.include?(header)
|
|
78
|
+
return unless disabling_value?(header, value_node)
|
|
79
|
+
|
|
80
|
+
findings << finding(
|
|
81
|
+
line: line,
|
|
82
|
+
message: header_message(header),
|
|
83
|
+
suggested_fix: header_fix(header)
|
|
84
|
+
)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def disabling_value?(header, value_node)
|
|
88
|
+
return true if Ast.false_literal?(value_node) || Ast.kw_literal?(value_node) == "nil"
|
|
89
|
+
|
|
90
|
+
header == "X-Frame-Options" && Ast.plain_string_value(value_node).to_s.upcase == "ALLOWALL"
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def header_message(header)
|
|
94
|
+
case header
|
|
95
|
+
when "X-Frame-Options"
|
|
96
|
+
"The `X-Frame-Options` header is explicitly overridden to allow framing (`ALLOWALL`/falsy) — " \
|
|
97
|
+
"this removes Rails' default clickjacking protection, letting this app be embedded in an " \
|
|
98
|
+
"attacker-controlled `<iframe>`."
|
|
99
|
+
when "X-Content-Type-Options"
|
|
100
|
+
"The `X-Content-Type-Options` header is explicitly disabled — this removes Rails' default " \
|
|
101
|
+
"MIME-sniffing protection, letting browsers reinterpret a response's content type (e.g. " \
|
|
102
|
+
"executing an uploaded file as script)."
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def header_fix(header)
|
|
107
|
+
case header
|
|
108
|
+
when "X-Frame-Options"
|
|
109
|
+
"Remove this override (Rails defaults to `SAMEORIGIN`), or set an explicit safe value if " \
|
|
110
|
+
"framing from a specific trusted origin is actually needed."
|
|
111
|
+
when "X-Content-Type-Options"
|
|
112
|
+
"Remove this override — leave `X-Content-Type-Options` at Rails' default (`nosniff`)."
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# True if `node` is a call chain ending in `.default_headers` (any
|
|
117
|
+
# receiver — usually `config.action_dispatch`, but the receiver chain
|
|
118
|
+
# itself isn't verified, same looseness as WeakSessionCookieRule not
|
|
119
|
+
# verifying its receiver is really `config`).
|
|
120
|
+
def default_headers_receiver?(node)
|
|
121
|
+
Ast.call_name(node)&.last == "default_headers"
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def each_assoc_pair(call_node)
|
|
125
|
+
Ast.call_arguments(call_node).each_with_object([]) do |arg, acc|
|
|
126
|
+
next unless Ast.tagged?(arg, :bare_assoc_hash) && arg[1].is_a?(Array)
|
|
127
|
+
|
|
128
|
+
arg[1].each { |p| acc << [p[1], p[2]] if Ast.tagged?(p, :assoc_new) }
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
@@ -10,6 +10,9 @@ module Scryer
|
|
|
10
10
|
self.category = "security"
|
|
11
11
|
self.default_severity = "critical"
|
|
12
12
|
self.title = "Possible SQL injection via string interpolation"
|
|
13
|
+
self.cwe = "CWE-89"
|
|
14
|
+
self.owasp_category = "A03:2021-Injection"
|
|
15
|
+
self.confidence = "high"
|
|
13
16
|
|
|
14
17
|
QUERY_METHODS = %w[
|
|
15
18
|
where find_by find_by! order pluck select group having
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
module Scryer
|
|
2
|
+
module Rules
|
|
3
|
+
# Flags an outbound HTTP call (`Net::HTTP`, `URI.open`/bare `open`
|
|
4
|
+
# via open-uri, `HTTParty`, `Faraday`, `RestClient`) where an argument
|
|
5
|
+
# references `params` directly — the request's destination is
|
|
6
|
+
# user-controlled, which lets an attacker make the server issue requests
|
|
7
|
+
# to internal services, cloud metadata endpoints, or anywhere else on
|
|
8
|
+
# its network (server-side request forgery).
|
|
9
|
+
class SsrfRule < Rule
|
|
10
|
+
self.rule_id = "ssrf"
|
|
11
|
+
self.category = "security"
|
|
12
|
+
self.default_severity = "critical"
|
|
13
|
+
self.title = "Possible server-side request forgery (SSRF)"
|
|
14
|
+
self.cwe = "CWE-918"
|
|
15
|
+
self.owasp_category = "A10:2021-Server-Side Request Forgery"
|
|
16
|
+
self.confidence = "medium"
|
|
17
|
+
|
|
18
|
+
DANGEROUS_CALLS = {
|
|
19
|
+
"Net::HTTP" => %w[get get_response post post_form],
|
|
20
|
+
"URI" => %w[open],
|
|
21
|
+
"HTTParty" => %w[get post put delete],
|
|
22
|
+
"Faraday" => %w[get post put delete],
|
|
23
|
+
"RestClient" => %w[get post put delete]
|
|
24
|
+
}.freeze
|
|
25
|
+
|
|
26
|
+
def scan
|
|
27
|
+
findings = []
|
|
28
|
+
|
|
29
|
+
Ast.each_node(sexp) do |node|
|
|
30
|
+
next unless Ast.tagged?(node, :method_add_arg, :command, :command_call)
|
|
31
|
+
|
|
32
|
+
inner = Ast.tagged?(node, :method_add_arg) ? node[1] : node
|
|
33
|
+
receiver_and_name = Ast.call_name(inner)
|
|
34
|
+
next unless receiver_and_name
|
|
35
|
+
|
|
36
|
+
receiver, method_name = receiver_and_name
|
|
37
|
+
next unless dangerous_call?(receiver, method_name)
|
|
38
|
+
|
|
39
|
+
args = Ast.call_arguments(node)
|
|
40
|
+
matched_arg = args.find { |a| Ast.references_params?(a) }
|
|
41
|
+
next unless matched_arg
|
|
42
|
+
|
|
43
|
+
line = Ast.line_of(node)
|
|
44
|
+
if (fixed_host = fixed_host_with_dynamic_path(matched_arg))
|
|
45
|
+
findings << finding(
|
|
46
|
+
line: line,
|
|
47
|
+
severity: "warning",
|
|
48
|
+
message: "`#{describe_call(receiver, method_name)}` is called with a URL whose host " \
|
|
49
|
+
"(`#{fixed_host}`) is a fixed literal — only a path/query segment is " \
|
|
50
|
+
"`params`-derived, so this can't redirect the request to an arbitrary " \
|
|
51
|
+
"attacker-chosen host the way a fully dynamic URL could. Still worth a " \
|
|
52
|
+
"look: a params-derived path segment can enumerate/probe endpoints on " \
|
|
53
|
+
"that fixed host, or (if the target app doesn't validate it either) reach " \
|
|
54
|
+
"unintended paths there.",
|
|
55
|
+
suggested_fix: "If the path segment is meant to be one of a known set of values " \
|
|
56
|
+
"(an id, an enum), validate/allowlist it rather than interpolating " \
|
|
57
|
+
"`params` straight into the URL — a stray `/` or `..` segment can " \
|
|
58
|
+
"still change which path on #{fixed_host} gets requested."
|
|
59
|
+
)
|
|
60
|
+
else
|
|
61
|
+
findings << finding(
|
|
62
|
+
line: line,
|
|
63
|
+
message: "`#{describe_call(receiver, method_name)}` is called with a URL/argument " \
|
|
64
|
+
"that references `params` — the server can be made to issue a request to " \
|
|
65
|
+
"any host an attacker chooses, including internal services and cloud " \
|
|
66
|
+
"metadata endpoints (e.g. `169.254.169.254`) that aren't meant to be " \
|
|
67
|
+
"reachable from outside.",
|
|
68
|
+
suggested_fix: "Validate the destination against an allowlist of known-safe hosts " \
|
|
69
|
+
"before making the request, rather than passing the user-supplied " \
|
|
70
|
+
"value straight through — and reject internal/link-local IP ranges " \
|
|
71
|
+
"explicitly if the allowlist is host-based (DNS can still resolve to one)."
|
|
72
|
+
)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
findings
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
def dangerous_call?(receiver, method_name)
|
|
82
|
+
return true if receiver.nil? && method_name == "open"
|
|
83
|
+
|
|
84
|
+
DANGEROUS_CALLS[const_receiver_name(receiver)]&.include?(method_name) || false
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def describe_call(receiver, method_name)
|
|
88
|
+
name = const_receiver_name(receiver)
|
|
89
|
+
name ? "#{name}.#{method_name}" : method_name
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# A URL's host is fully attacker-controlled the moment `params` reaches
|
|
93
|
+
# any part of it — but a `params` reference confined to the *path* of
|
|
94
|
+
# an otherwise-literal URL (`"https://api.example.com/users/#{params[:id]}"`)
|
|
95
|
+
# can't redirect the request to a different host at all, which is the
|
|
96
|
+
# actual SSRF risk (reaching internal services / cloud metadata
|
|
97
|
+
# endpoints). Distinguishing the two isn't possible in general — a URL
|
|
98
|
+
# built via string concatenation, a helper method, `URI.join`, or a
|
|
99
|
+
# variable can put `params` anywhere including the host, and this rule
|
|
100
|
+
# has no way to trace that. This only recognizes the one shape where
|
|
101
|
+
# it's unambiguous: a direct string_literal argument whose *first*
|
|
102
|
+
# literal chunk (everything before the first interpolation) already
|
|
103
|
+
# spells out a complete `scheme://host/` — meaning every `params`
|
|
104
|
+
# reference in the string necessarily lands after that fixed host, in
|
|
105
|
+
# the path/query. Returns the literal host text for the message, or
|
|
106
|
+
# nil if this argument doesn't match that exact shape (in which case
|
|
107
|
+
# the call is treated with full SSRF severity, same as before this
|
|
108
|
+
# existed — this only *adds* a more precise, lower-severity message
|
|
109
|
+
# for the case that's provably narrower, it never suppresses the
|
|
110
|
+
# finding).
|
|
111
|
+
def fixed_host_with_dynamic_path(node)
|
|
112
|
+
return nil unless Ast.tagged?(node, :string_literal)
|
|
113
|
+
|
|
114
|
+
content = node[1]
|
|
115
|
+
return nil unless Ast.tagged?(content, :string_content)
|
|
116
|
+
|
|
117
|
+
first_chunk = content[1]
|
|
118
|
+
return nil unless Ast.tagged?(first_chunk, :@tstring_content)
|
|
119
|
+
|
|
120
|
+
match = /\A(https?:\/\/[^\/\s{}]+)\//.match(first_chunk[1])
|
|
121
|
+
match && match[1]
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Textual name of a (possibly namespaced) constant receiver —
|
|
125
|
+
# "URI" for `[:var_ref, [:@const, "URI", pos]]`, "Net::HTTP" for the
|
|
126
|
+
# `[:const_path_ref, ...]` chain `Net::HTTP` parses into. nil for
|
|
127
|
+
# anything else (a variable, a method call, ...).
|
|
128
|
+
def const_receiver_name(node)
|
|
129
|
+
if Ast.tagged?(node, :var_ref) && node[1].is_a?(Array) && node[1][0] == :@const
|
|
130
|
+
node[1][1]
|
|
131
|
+
elsif Ast.tagged?(node, :const_path_ref)
|
|
132
|
+
left = const_receiver_name(node[1])
|
|
133
|
+
right = node[2].is_a?(Array) && node[2][0] == :@const ? node[2][1] : nil
|
|
134
|
+
[left, right].compact.join("::")
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
@@ -9,6 +9,9 @@ module Scryer
|
|
|
9
9
|
self.category = "security"
|
|
10
10
|
self.default_severity = "critical"
|
|
11
11
|
self.title = "Unsafe deserialization of untrusted data"
|
|
12
|
+
self.cwe = "CWE-502"
|
|
13
|
+
self.owasp_category = "A08:2021-Software and Data Integrity Failures"
|
|
14
|
+
self.confidence = "high"
|
|
12
15
|
|
|
13
16
|
UNSAFE_CALLS = {
|
|
14
17
|
%w[Marshal load] => "Marshal.load can instantiate arbitrary Ruby objects, including ones " \
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
module Scryer
|
|
2
|
+
module Rules
|
|
3
|
+
# Flags `config.log_level = :debug` specifically in
|
|
4
|
+
# config/environments/production.rb. Debug-level Rails logging can write
|
|
5
|
+
# full request parameters (including anything not filtered by
|
|
6
|
+
# `config.filter_parameters`) and raw SQL bind values to the production
|
|
7
|
+
# log — wherever that log ends up (a shared file, a log-aggregation
|
|
8
|
+
# service), that's a broader-than-intended audience for potentially
|
|
9
|
+
# sensitive data. `:debug` is Rails' own default in development.rb, so
|
|
10
|
+
# it's only flagged in production.rb; other levels (`:info`, `:warn`)
|
|
11
|
+
# aren't flagged at all — those don't carry this risk.
|
|
12
|
+
class VerboseProductionLogLevelRule < Rule
|
|
13
|
+
self.rule_id = "verbose_production_log_level"
|
|
14
|
+
self.category = "security"
|
|
15
|
+
self.default_severity = "warning"
|
|
16
|
+
self.title = "Debug-level logging enabled in production"
|
|
17
|
+
self.cwe = "CWE-532"
|
|
18
|
+
self.owasp_category = "A09:2021-Security Logging and Monitoring Failures"
|
|
19
|
+
self.confidence = "medium"
|
|
20
|
+
|
|
21
|
+
PRODUCTION_ENV_FILE = "config/environments/production.rb"
|
|
22
|
+
|
|
23
|
+
def scan
|
|
24
|
+
return [] unless file.to_s.end_with?(PRODUCTION_ENV_FILE)
|
|
25
|
+
|
|
26
|
+
findings = []
|
|
27
|
+
|
|
28
|
+
Ast.each_node(sexp) do |node|
|
|
29
|
+
next unless Ast.tagged?(node, :assign)
|
|
30
|
+
|
|
31
|
+
target = node[1]
|
|
32
|
+
next unless Ast.tagged?(target, :field)
|
|
33
|
+
next unless Ast.ident_text(target[3]) == "log_level"
|
|
34
|
+
next unless Ast.literal_text(node[2]) == "debug"
|
|
35
|
+
|
|
36
|
+
findings << finding(
|
|
37
|
+
line: Ast.line_of(node),
|
|
38
|
+
message: "`config.log_level = :debug` in #{PRODUCTION_ENV_FILE} logs full request " \
|
|
39
|
+
"parameters and raw SQL bind values in production — anything not covered " \
|
|
40
|
+
"by `config.filter_parameters` (e.g. a param name added after that list was " \
|
|
41
|
+
"last updated) ends up in the log verbatim.",
|
|
42
|
+
suggested_fix: "Use `:info` (Rails' own production default) or higher in production, " \
|
|
43
|
+
"and confirm `config.filter_parameters` covers every sensitive param " \
|
|
44
|
+
"name this app actually receives if verbose logging is genuinely needed " \
|
|
45
|
+
"for debugging."
|
|
46
|
+
)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
findings
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -11,9 +11,21 @@ module Scryer
|
|
|
11
11
|
self.category = "security"
|
|
12
12
|
self.default_severity = "warning"
|
|
13
13
|
self.title = "Weak hash algorithm used for password/credential hashing"
|
|
14
|
+
self.cwe = "CWE-327"
|
|
15
|
+
self.owasp_category = "A02:2021-Cryptographic Failures"
|
|
16
|
+
self.confidence = "medium"
|
|
14
17
|
|
|
15
18
|
WEAK_DIGESTS = %w[MD5 SHA1].freeze
|
|
16
|
-
|
|
19
|
+
|
|
20
|
+
# `(?!less)` after "password" excludes "passwordless" (and
|
|
21
|
+
# "passwordlessly") specifically — a real identifier that means the
|
|
22
|
+
# *opposite* of what this heuristic is looking for (e.g. a magic-link
|
|
23
|
+
# or passwordless-auth token, `Digest::SHA1.hexdigest(passwordless_token)`),
|
|
24
|
+
# not a credential. It doesn't affect any legitimate match: every other
|
|
25
|
+
# "password"-containing identifier this heuristic cares about
|
|
26
|
+
# (`password`, `user_password`, `password_reset_token`, `hashed_password`,
|
|
27
|
+
# ...) is never immediately followed by the literal substring "less".
|
|
28
|
+
PASSWORD_HINT = /password(?!less)|passwd|credential/i.freeze
|
|
17
29
|
|
|
18
30
|
def scan
|
|
19
31
|
findings = []
|
|
@@ -29,7 +41,7 @@ module Scryer
|
|
|
29
41
|
# non-credential uses (cache keys, ETags, checksums) that shouldn't
|
|
30
42
|
# be flagged as a crypto weakness.
|
|
31
43
|
line = Ast.line_of(node)
|
|
32
|
-
context_line = Ast.source_line(source, line).to_s
|
|
44
|
+
context_line = strip_trailing_comment(Ast.source_line(source, line).to_s)
|
|
33
45
|
next unless PASSWORD_HINT.match?(context_line)
|
|
34
46
|
|
|
35
47
|
findings << finding(
|
|
@@ -49,6 +61,29 @@ module Scryer
|
|
|
49
61
|
|
|
50
62
|
private
|
|
51
63
|
|
|
64
|
+
# Strips a trailing `# ...` line comment before running PASSWORD_HINT
|
|
65
|
+
# against the raw source line. Without this, a defensive comment that
|
|
66
|
+
# *disclaims* password use (`Digest::SHA1.hexdigest(file_content) #
|
|
67
|
+
# cache key, not a password hash` — a natural thing to write specifically
|
|
68
|
+
# to preempt this exact kind of static-analysis false positive) still
|
|
69
|
+
# contains the substring "password" and would trip the heuristic just
|
|
70
|
+
# as hard as a genuine mention. This does give up the (much rarer) case
|
|
71
|
+
# where a comment is the *only* signal — e.g. an unnamed argument whose
|
|
72
|
+
# only password-ish hint is a comment describing it — but real
|
|
73
|
+
# password-hashing call sites almost always also have a password-ish
|
|
74
|
+
# identifier in the actual code (`password`, `user.password`, ...),
|
|
75
|
+
# which stays visible on the line after stripping the comment.
|
|
76
|
+
#
|
|
77
|
+
# Deliberately simple rather than a full tokenizer: finds the first `#`
|
|
78
|
+
# that isn't the start of a `#{` string interpolation and treats
|
|
79
|
+
# everything from there as comment. This means a literal `#` inside a
|
|
80
|
+
# string argument on the same line (e.g. `hexdigest("score: #1")`) would
|
|
81
|
+
# be mistaken for a comment start too — an accepted limitation of a
|
|
82
|
+
# same-line text heuristic, not a full parse.
|
|
83
|
+
def strip_trailing_comment(line)
|
|
84
|
+
line.sub(/#(?!\{).*\z/, "")
|
|
85
|
+
end
|
|
86
|
+
|
|
52
87
|
# Matches `Digest::MD5` / `Digest::SHA1` referenced as a constant path.
|
|
53
88
|
def digest_algorithm_name(node)
|
|
54
89
|
text = Ast.each_node(node)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
module Scryer
|
|
2
|
+
module Rules
|
|
3
|
+
# Flags `config.session_store :cookie_store, ...` with no `secure: true`
|
|
4
|
+
# in its options — without it, the session cookie can be sent over plain
|
|
5
|
+
# HTTP, not just HTTPS. Only `:cookie_store` is checked (the default and
|
|
6
|
+
# most common store); other stores (`:redis_session_store`, ...) don't
|
|
7
|
+
# carry the same cookie-content risk and are out of scope here.
|
|
8
|
+
class WeakSessionCookieRule < Rule
|
|
9
|
+
self.rule_id = "weak_session_cookie"
|
|
10
|
+
self.category = "security"
|
|
11
|
+
self.default_severity = "warning"
|
|
12
|
+
self.title = "Session cookie missing the secure flag"
|
|
13
|
+
self.cwe = "CWE-614"
|
|
14
|
+
self.owasp_category = "A05:2021-Security Misconfiguration"
|
|
15
|
+
self.confidence = "high"
|
|
16
|
+
|
|
17
|
+
def scan
|
|
18
|
+
findings = []
|
|
19
|
+
|
|
20
|
+
Ast.each_node(sexp) do |node|
|
|
21
|
+
next unless Ast.tagged?(node, :command, :command_call, :method_add_arg)
|
|
22
|
+
|
|
23
|
+
inner = Ast.tagged?(node, :method_add_arg) ? node[1] : node
|
|
24
|
+
receiver_and_name = Ast.call_name(inner)
|
|
25
|
+
next unless receiver_and_name
|
|
26
|
+
|
|
27
|
+
_receiver, method_name = receiver_and_name
|
|
28
|
+
next unless method_name == "session_store"
|
|
29
|
+
|
|
30
|
+
args = Ast.call_arguments(node)
|
|
31
|
+
next unless args.any? { |a| Ast.literal_text(a) == "cookie_store" }
|
|
32
|
+
next if Ast.true_literal?(Ast.keyword_arg(args, "secure"))
|
|
33
|
+
|
|
34
|
+
line = Ast.line_of(node)
|
|
35
|
+
findings << finding(
|
|
36
|
+
line: line,
|
|
37
|
+
message: "`session_store :cookie_store` has no `secure: true` — the session cookie " \
|
|
38
|
+
"can be transmitted over plain HTTP, where it's exposed to network " \
|
|
39
|
+
"eavesdropping (e.g. on shared/untrusted Wi-Fi).",
|
|
40
|
+
suggested_fix: "Add `secure: true` to the session_store options (typically only in " \
|
|
41
|
+
"production, since local HTTP development doesn't have TLS): " \
|
|
42
|
+
"`config.session_store :cookie_store, key: '_app_session', secure: " \
|
|
43
|
+
"Rails.env.production?`."
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
findings
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -10,6 +10,33 @@ module Scryer
|
|
|
10
10
|
self.category = "security"
|
|
11
11
|
self.default_severity = "warning"
|
|
12
12
|
self.title = "Unescaped HTML output (possible XSS)"
|
|
13
|
+
self.cwe = "CWE-79"
|
|
14
|
+
self.owasp_category = "A03:2021-Injection"
|
|
15
|
+
self.confidence = "high"
|
|
16
|
+
|
|
17
|
+
# Rails helpers whose whole job is to hand back HTML that's already
|
|
18
|
+
# safe to render unescaped, so a `.html_safe` immediately wrapped
|
|
19
|
+
# around a call to one of these isn't the same risk as calling it on
|
|
20
|
+
# raw user input:
|
|
21
|
+
# - `sanitize(x)` strips to an explicit allow-list of tags/attrs —
|
|
22
|
+
# it's the standard sanitize-then-mark-safe idiom this rule exists
|
|
23
|
+
# to steer people *toward*, so flagging it would contradict our
|
|
24
|
+
# own suggested fix.
|
|
25
|
+
# - `strip_tags(x)` removes all markup, so there's no HTML left to
|
|
26
|
+
# inject.
|
|
27
|
+
# - `simple_format(x)` runs the text through `sanitize` internally by
|
|
28
|
+
# default (it only skips that when called with an explicit
|
|
29
|
+
# `sanitize: false` option, which we don't special-case here).
|
|
30
|
+
# - `t(...)`/`translate(...)` pulls from the app's own locale files,
|
|
31
|
+
# not attacker-controlled request data — translators, not users,
|
|
32
|
+
# write that content, so this is Rails' own common "trusted copy"
|
|
33
|
+
# idiom rather than a raw-input passthrough.
|
|
34
|
+
# This says nothing about the *argument* passed to these methods being
|
|
35
|
+
# safe on its own — it's specifically the combination of "wrapped in
|
|
36
|
+
# one of these calls, then marked html_safe" that's the recognized
|
|
37
|
+
# pattern. A bare `params[:bio].html_safe` or an interpolated string
|
|
38
|
+
# marked safe still flags, since neither goes through any of these.
|
|
39
|
+
SANITIZING_METHODS = %w[sanitize strip_tags simple_format t translate].freeze
|
|
13
40
|
|
|
14
41
|
def scan
|
|
15
42
|
findings = []
|
|
@@ -62,9 +89,23 @@ module Scryer
|
|
|
62
89
|
def safe_literal?(node)
|
|
63
90
|
return true if node.nil?
|
|
64
91
|
return true if Ast.tagged?(node, :string_literal) && !Ast.string_literal_has_interpolation?(node)
|
|
92
|
+
return true if sanitizing_call?(node)
|
|
65
93
|
|
|
66
94
|
false
|
|
67
95
|
end
|
|
96
|
+
|
|
97
|
+
# True if `node` is a call (parenthesized or bare) to one of
|
|
98
|
+
# SANITIZING_METHODS — see that constant's comment for why those
|
|
99
|
+
# specific methods are exempt.
|
|
100
|
+
def sanitizing_call?(node)
|
|
101
|
+
inner = Ast.tagged?(node, :method_add_arg) ? node[1] : node
|
|
102
|
+
return false unless Ast.tagged?(inner, :call, :command_call, :vcall, :fcall, :command)
|
|
103
|
+
|
|
104
|
+
name_pair = Ast.call_name(inner)
|
|
105
|
+
return false unless name_pair
|
|
106
|
+
|
|
107
|
+
SANITIZING_METHODS.include?(name_pair[1])
|
|
108
|
+
end
|
|
68
109
|
end
|
|
69
110
|
end
|
|
70
111
|
end
|
data/lib/scryer/version.rb
CHANGED
data/lib/scryer.rb
CHANGED
|
@@ -10,7 +10,9 @@ require "scryer/duplicate_detector"
|
|
|
10
10
|
require "scryer/scanner"
|
|
11
11
|
require "scryer/report_renderer"
|
|
12
12
|
require "scryer/dependency_audit"
|
|
13
|
+
require "scryer/baseline"
|
|
13
14
|
require "scryer/ai_client"
|
|
15
|
+
require "scryer/fix_verifier"
|
|
14
16
|
require "scryer/ai_fix_suggester"
|
|
15
17
|
|
|
16
18
|
Dir[File.join(__dir__, "scryer", "rules", "*.rb")].sort.each { |f| require f }
|
|
@@ -53,6 +55,19 @@ module Scryer
|
|
|
53
55
|
def configuration
|
|
54
56
|
@configuration ||= Configuration.new
|
|
55
57
|
end
|
|
58
|
+
|
|
59
|
+
# Runs the static scan with the current configuration (or explicit
|
|
60
|
+
# overrides) applied, without needing to know Scanner's own constructor
|
|
61
|
+
# shape. Exists mainly so the RSpec/Minitest test helpers (see
|
|
62
|
+
# lib/scryer/rspec.rb / lib/scryer/minitest.rb) — and any other future
|
|
63
|
+
# caller that just wants "the result of a normal scan" — don't each
|
|
64
|
+
# duplicate `Scanner.new(root:, dirs:, skip_rules:).call`. The CLI/rake
|
|
65
|
+
# task aren't changed to use this (they also handle dependency auditing,
|
|
66
|
+
# baselines, and report writing inline) — this is for callers that only
|
|
67
|
+
# need the static-scan Result itself.
|
|
68
|
+
def scan(root:, dirs: configuration.dirs, skip_rules: configuration.skip_rules)
|
|
69
|
+
Scryer::Scanner.new(root: root, dirs: dirs, skip_rules: skip_rules).call
|
|
70
|
+
end
|
|
56
71
|
end
|
|
57
72
|
end
|
|
58
73
|
|