Skip to content

Commit 2dc9b7b

Browse files
committed
feat: redaction only when path matches
1 parent fb6e123 commit 2dc9b7b

2 files changed

Lines changed: 8 additions & 16 deletions

File tree

app/web/security/log_sanitizer.rb

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,15 @@ module Web
88
##
99
# Sanitizes request paths and log payloads before they are emitted.
1010
module LogSanitizer
11-
FEED_TOKEN_ROUTE = %r{\A(/api/v1/feeds/)([^/?]+)\z}
11+
FEED_TOKEN_ROUTE = %r{\A(/api/v1/feeds/)([^/?]+?)(\.(?:json|xml|rss))?\z}
1212

1313
class << self
1414
# @param path [String, nil]
1515
# @return [String, nil]
1616
def sanitize_path(path)
1717
return if path.nil?
1818

19-
path_string = path.to_s
20-
suffix = feed_suffix(path_string)
21-
token_path = suffix ? path_string.delete_suffix(suffix) : path_string
22-
23-
token_path.gsub(FEED_TOKEN_ROUTE, "\\1[REDACTED]#{suffix}")
19+
path.to_s.gsub(FEED_TOKEN_ROUTE, '\1[REDACTED]\3')
2420
end
2521

2622
# @param details [Hash]
@@ -33,16 +29,6 @@ def sanitize_details(details)
3329

3430
private
3531

36-
# @param path [String]
37-
# @return [String, nil]
38-
def feed_suffix(path)
39-
return '.json' if path.end_with?('.json')
40-
return '.xml' if path.end_with?('.xml')
41-
return '.rss' if path.end_with?('.rss')
42-
43-
nil
44-
end
45-
4632
# @param key [Object]
4733
# @param value [Object]
4834
# @return [Object]

spec/html2rss/web/log_sanitizer_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@
5151
).to eq('/api/v1/feeds/[REDACTED].xml')
5252
end
5353

54+
it 'leaves non-feed paths unchanged when they use supported suffixes', :aggregate_failures do
55+
expect(described_class.sanitize_path('/api/v1/health.json')).to eq('/api/v1/health.json')
56+
expect(described_class.sanitize_path('/api/v1/status.xml')).to eq('/api/v1/status.xml')
57+
expect(described_class.sanitize_path('/feeds/public.rss')).to eq('/feeds/public.rss')
58+
end
59+
5460
it 'replaces logged urls with hashed host metadata' do
5561
expect(described_class.sanitize_details(url: 'https://news.ycombinator.com')).to eq(url: expected_news_url)
5662
end

0 commit comments

Comments
 (0)