scryer 1.0.0 → 1.2.0
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 +414 -0
- data/README.md +114 -649
- data/docs/architecture.md +268 -0
- data/docs/contributing.md +42 -0
- data/docs/fix-mode.md +364 -0
- data/docs/rails-integration.md +162 -0
- data/docs/rules.md +310 -0
- data/docs/usage.md +301 -0
- data/lib/generators/scryer/USAGE +10 -2
- data/lib/generators/scryer/templates/scryer_initializer.rb +15 -0
- data/lib/scryer/ai_fix_suggester.rb +37 -11
- data/lib/scryer/ast.rb +26 -0
- data/lib/scryer/authorization_watcher.rb +156 -0
- data/lib/scryer/baseline.rb +75 -0
- data/lib/scryer/cli.rb +688 -14
- data/lib/scryer/colorizer.rb +56 -0
- data/lib/scryer/dependency_fixer.rb +96 -0
- data/lib/scryer/finding.rb +6 -0
- data/lib/scryer/fix_runner.rb +161 -0
- data/lib/scryer/fix_verifier.rb +169 -0
- data/lib/scryer/mechanical_fixer.rb +288 -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 +539 -46
- data/lib/scryer/rspec.rb +55 -0
- data/lib/scryer/rule.rb +22 -2
- data/lib/scryer/rules/action_cable_forgery_protection_rule.rb +3 -0
- data/lib/scryer/rules/active_storage_inline_disposition_rule.rb +3 -0
- data/lib/scryer/rules/active_storage_missing_content_type_validation_rule.rb +3 -0
- data/lib/scryer/rules/authentication_bypass_rule.rb +30 -7
- 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 +51 -20
- data/lib/scryer/rules/csrf_protection_rule.rb +60 -11
- data/lib/scryer/rules/force_ssl_rule.rb +3 -0
- data/lib/scryer/rules/graphql_missing_query_limits_rule.rb +31 -0
- data/lib/scryer/rules/hardcoded_basic_auth_rule.rb +3 -0
- data/lib/scryer/rules/hardcoded_secret_key_base_rule.rb +3 -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 +63 -9
- data/lib/scryer/rules/insecure_cookie_serializer_rule.rb +3 -0
- data/lib/scryer/rules/job_raw_params_rule.rb +40 -7
- data/lib/scryer/rules/jwt_insecure_rule.rb +3 -0
- data/lib/scryer/rules/mass_assignment_rule.rb +32 -5
- 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 +22 -1
- data/lib/scryer/rules/security_headers_rule.rb +3 -0
- data/lib/scryer/rules/sql_injection_rule.rb +3 -0
- data/lib/scryer/rules/ssrf_rule.rb +67 -13
- 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 +3 -0
- data/lib/scryer/rules/xss_unsafe_html_rule.rb +41 -0
- data/lib/scryer/scanner.rb +25 -12
- data/lib/scryer/style_rules/frozen_string_literal_rule.rb +1 -0
- data/lib/scryer/version.rb +1 -1
- data/lib/scryer.rb +30 -1
- data/lib/tasks/scryer.rake +447 -20
- metadata +52 -12
|
@@ -11,6 +11,9 @@ module Scryer
|
|
|
11
11
|
self.category = "security"
|
|
12
12
|
self.default_severity = "critical"
|
|
13
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"
|
|
14
17
|
|
|
15
18
|
DANGEROUS_CALLS = {
|
|
16
19
|
"Net::HTTP" => %w[get get_response post post_form],
|
|
@@ -34,21 +37,40 @@ module Scryer
|
|
|
34
37
|
next unless dangerous_call?(receiver, method_name)
|
|
35
38
|
|
|
36
39
|
args = Ast.call_arguments(node)
|
|
37
|
-
|
|
40
|
+
matched_arg = args.find { |a| Ast.references_params?(a) }
|
|
41
|
+
next unless matched_arg
|
|
38
42
|
|
|
39
43
|
line = Ast.line_of(node)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
|
52
74
|
end
|
|
53
75
|
|
|
54
76
|
findings
|
|
@@ -67,6 +89,38 @@ module Scryer
|
|
|
67
89
|
name ? "#{name}.#{method_name}" : method_name
|
|
68
90
|
end
|
|
69
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
|
+
|
|
70
124
|
# Textual name of a (possibly namespaced) constant receiver —
|
|
71
125
|
# "URI" for `[:var_ref, [:@const, "URI", pos]]`, "Net::HTTP" for the
|
|
72
126
|
# `[:const_path_ref, ...]` chain `Net::HTTP` parses into. nil for
|
|
@@ -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)
|
|
@@ -10,6 +10,9 @@ module Scryer
|
|
|
10
10
|
self.category = "security"
|
|
11
11
|
self.default_severity = "warning"
|
|
12
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"
|
|
13
16
|
|
|
14
17
|
def scan
|
|
15
18
|
findings = []
|
|
@@ -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/scanner.rb
CHANGED
|
@@ -31,10 +31,19 @@ module Scryer
|
|
|
31
31
|
# `skip_rules` silences specific checks by rule_id (e.g. a known false
|
|
32
32
|
# positive on this codebase) without editing/removing the rule itself —
|
|
33
33
|
# accepts strings or symbols, matched against Rule.rule_id.
|
|
34
|
-
|
|
34
|
+
#
|
|
35
|
+
# `detect_duplicates: false` skips duplicate-code detection entirely
|
|
36
|
+
# (method/query/cache-key extraction and the DuplicateDetector passes
|
|
37
|
+
# below) — unlike the security/performance/style rules, duplicate
|
|
38
|
+
# detection isn't a `Scryer::Rule` with its own rule_id, so `skip_rules`
|
|
39
|
+
# has no way to address it; this is its equivalent off switch. See
|
|
40
|
+
# `Scryer::Configuration#detect_duplicates` for the config-driven default
|
|
41
|
+
# every CLI/rake entry point reads before constructing a Scanner.
|
|
42
|
+
def initialize(root:, dirs: DEFAULT_GLOB_DIRS, skip_rules: [], detect_duplicates: true)
|
|
35
43
|
@root = File.expand_path(root)
|
|
36
44
|
@dirs = dirs
|
|
37
45
|
@skip_rules = Set.new(skip_rules.map(&:to_s))
|
|
46
|
+
@detect_duplicates = detect_duplicates
|
|
38
47
|
end
|
|
39
48
|
|
|
40
49
|
def call
|
|
@@ -77,24 +86,28 @@ module Scryer
|
|
|
77
86
|
bucket.concat(rule_class.new(file: rel_path, source: source, sexp: sexp).scan)
|
|
78
87
|
end
|
|
79
88
|
|
|
80
|
-
if duplicate_detection_target?(rel_path)
|
|
89
|
+
if @detect_duplicates && duplicate_detection_target?(rel_path)
|
|
81
90
|
all_methods.concat(MethodExtractor.extract(file: rel_path, source: source, sexp: sexp))
|
|
82
91
|
all_queries.concat(QueryExtractor.extract(file: rel_path, source: source, sexp: sexp))
|
|
83
92
|
all_cache_calls.concat(CacheExtractor.extract(file: rel_path, source: source, sexp: sexp))
|
|
84
93
|
end
|
|
85
94
|
end
|
|
86
95
|
|
|
87
|
-
# Same computed value cached under the same key from multiple call
|
|
88
|
-
# sites is normal (just reusing the cache). Only flag it when the
|
|
89
|
-
# *keys* differ too — that's either a redundant cache entry or a key
|
|
90
|
-
# that drifted out of sync with a copy-pasted sibling.
|
|
91
|
-
cache_groups = DuplicateDetector.call(all_cache_calls, threshold: CACHE_SIMILARITY_THRESHOLD, kind: "cache_duplicate")
|
|
92
|
-
.select { |g| g.members.map(&:cache_key).uniq.size > 1 }
|
|
93
|
-
|
|
94
96
|
duplicate_groups =
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
97
|
+
if @detect_duplicates
|
|
98
|
+
# Same computed value cached under the same key from multiple call
|
|
99
|
+
# sites is normal (just reusing the cache). Only flag it when the
|
|
100
|
+
# *keys* differ too — that's either a redundant cache entry or a
|
|
101
|
+
# key that drifted out of sync with a copy-pasted sibling.
|
|
102
|
+
cache_groups = DuplicateDetector.call(all_cache_calls, threshold: CACHE_SIMILARITY_THRESHOLD, kind: "cache_duplicate")
|
|
103
|
+
.select { |g| g.members.map(&:cache_key).uniq.size > 1 }
|
|
104
|
+
|
|
105
|
+
DuplicateDetector.call(all_methods, kind: "method_duplicate") +
|
|
106
|
+
DuplicateDetector.call(all_queries, threshold: QUERY_SIMILARITY_THRESHOLD, kind: "query_duplicate") +
|
|
107
|
+
cache_groups
|
|
108
|
+
else
|
|
109
|
+
[]
|
|
110
|
+
end
|
|
98
111
|
|
|
99
112
|
Result.new(
|
|
100
113
|
security_findings: security_findings,
|
data/lib/scryer/version.rb
CHANGED
data/lib/scryer.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require "scryer/version"
|
|
2
|
+
require "scryer/colorizer"
|
|
2
3
|
require "scryer/ast"
|
|
3
4
|
require "scryer/finding"
|
|
4
5
|
require "scryer/rule_set"
|
|
@@ -10,8 +11,13 @@ require "scryer/duplicate_detector"
|
|
|
10
11
|
require "scryer/scanner"
|
|
11
12
|
require "scryer/report_renderer"
|
|
12
13
|
require "scryer/dependency_audit"
|
|
14
|
+
require "scryer/baseline"
|
|
13
15
|
require "scryer/ai_client"
|
|
16
|
+
require "scryer/fix_verifier"
|
|
17
|
+
require "scryer/mechanical_fixer"
|
|
14
18
|
require "scryer/ai_fix_suggester"
|
|
19
|
+
require "scryer/fix_runner"
|
|
20
|
+
require "scryer/dependency_fixer"
|
|
15
21
|
|
|
16
22
|
Dir[File.join(__dir__, "scryer", "rules", "*.rb")].sort.each { |f| require f }
|
|
17
23
|
Dir[File.join(__dir__, "scryer", "performance_rules", "*.rb")].sort.each { |f| require f }
|
|
@@ -37,11 +43,21 @@ module Scryer
|
|
|
37
43
|
# default: every registered rule runs. The `scryer` executable's
|
|
38
44
|
# `--skip RULE_ID` flag adds to this list for a single run rather than
|
|
39
45
|
# replacing it.
|
|
40
|
-
|
|
46
|
+
#
|
|
47
|
+
# `detect_duplicates` toggles duplicate-code detection (method/query/
|
|
48
|
+
# cache-key similarity across models, controllers, helpers, and
|
|
49
|
+
# concerns — see Scryer::DuplicateDetector) on or off. `true` by
|
|
50
|
+
# default, matching this gem's existing behavior. Duplicate detection
|
|
51
|
+
# isn't a `Scryer::Rule`, so it has no `rule_id` and `skip_rules` can't
|
|
52
|
+
# address it — set this to `false` instead (or pass `--no-duplicates` /
|
|
53
|
+
# `SCRYER_NO_DUPLICATES=1` for a single run without changing the
|
|
54
|
+
# configured default) if it's too noisy or too slow for a given project.
|
|
55
|
+
attr_accessor :project_name, :dirs, :branch, :ai_client, :skip_rules, :detect_duplicates
|
|
41
56
|
|
|
42
57
|
def initialize
|
|
43
58
|
@dirs = Scryer::Scanner::DEFAULT_GLOB_DIRS
|
|
44
59
|
@skip_rules = []
|
|
60
|
+
@detect_duplicates = true
|
|
45
61
|
end
|
|
46
62
|
end
|
|
47
63
|
|
|
@@ -53,6 +69,19 @@ module Scryer
|
|
|
53
69
|
def configuration
|
|
54
70
|
@configuration ||= Configuration.new
|
|
55
71
|
end
|
|
72
|
+
|
|
73
|
+
# Runs the static scan with the current configuration (or explicit
|
|
74
|
+
# overrides) applied, without needing to know Scanner's own constructor
|
|
75
|
+
# shape. Exists mainly so the RSpec/Minitest test helpers (see
|
|
76
|
+
# lib/scryer/rspec.rb / lib/scryer/minitest.rb) — and any other future
|
|
77
|
+
# caller that just wants "the result of a normal scan" — don't each
|
|
78
|
+
# duplicate `Scanner.new(root:, dirs:, skip_rules:).call`. The CLI/rake
|
|
79
|
+
# task aren't changed to use this (they also handle dependency auditing,
|
|
80
|
+
# baselines, and report writing inline) — this is for callers that only
|
|
81
|
+
# need the static-scan Result itself.
|
|
82
|
+
def scan(root:, dirs: configuration.dirs, skip_rules: configuration.skip_rules, detect_duplicates: configuration.detect_duplicates)
|
|
83
|
+
Scryer::Scanner.new(root: root, dirs: dirs, skip_rules: skip_rules, detect_duplicates: detect_duplicates).call
|
|
84
|
+
end
|
|
56
85
|
end
|
|
57
86
|
end
|
|
58
87
|
|