Skip to content

Commit 2452efb

Browse files
Improve hardened_sites detective
Improve the detective that analyzes websites: * For 'x-frame-options' accept its CSP alternative. * Report *specifically* what fields failed and on which websites, as otherwise it can be hard to figure out what to fix. Signed-off-by: David A. Wheeler <[email protected]>
1 parent a47eb6d commit 2452efb

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

app/lib/hardened_sites_detective.rb

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class HardenedSitesDetective < Detective
2929
UNMET_MISSING =
3030
{
3131
value: 'Unmet', confidence: 5,
32-
explanation: '// One or more of the required security hardening headers ' \
33-
'is missing.'
32+
explanation: 'Required security hardening headers missing: '
3433
}.freeze
3534
UNMET_NOSNIFF =
3635
{
@@ -43,10 +42,14 @@ class HardenedSitesDetective < Detective
4342

4443
# Check the given list of header hashes to make sure that all expected
4544
# keys are present.
46-
def security_fields_present?(headers_list)
47-
result = true
45+
def missing_security_fields(headers_list)
46+
result = []
4847
headers_list.each do |headers|
49-
result &&= CHECK.reduce(true) { |acc, elem| acc & headers.key?(elem) }
48+
CHECK.each do |required_item|
49+
if !headers.key?(required_item)
50+
result.append("#{required_item}")
51+
end
52+
end
5053
end
5154
result
5255
end
@@ -85,10 +88,16 @@ def check_urls(evidence, homepage_url, repo_url)
8588
if homepage_url.present? && repo_url.present?
8689
homepage_headers = get_headers(evidence, homepage_url)
8790
repo_headers = get_headers(evidence, repo_url)
88-
hardened = security_fields_present?([homepage_headers, repo_headers])
89-
@results[:hardened_site_status] = hardened ? MET : UNMET_MISSING
90-
hardened ||= check_nosniff?([homepage_headers, repo_headers])
91-
@results[:hardened_site_status] = UNMET_NOSNIFF unless hardened
91+
missing_hardened = missing_security_fields([homepage_headers, repo_headers])
92+
@results[:hardened_site_status] =
93+
if missing_hardened.empty?
94+
MET
95+
else
96+
result = UNMET_MISSING.deep_dup # clone but result is not frozen
97+
result[:explanation] += missing_hardened.join(',')
98+
end
99+
# hardened ||= check_nosniff?([homepage_headers, repo_headers])
100+
# @results[:hardened_site_status] = UNMET_NOSNIFF unless hardened
92101
end
93102
@results
94103
end

0 commit comments

Comments
 (0)