File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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]
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments