11# frozen_string_literal: true
22
33require 'digest'
4- require 'uri '
4+ require 'html2rss/url '
55
66module Html2rss
77 module Web
88 ##
9- # Sanitizes request and detail payloads before structured logging .
9+ # Sanitizes request paths and log payloads before they are emitted .
1010 module LogSanitizer
11- FEED_TOKEN_ROUTE = %r{\A (/api/v1/feeds/)([^/. ?]+)( \. (?:json|xml|rss))? \z }
11+ FEED_TOKEN_ROUTE = %r{\A (/api/v1/feeds/)([^/?]+)\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 . to_s . gsub ( FEED_TOKEN_ROUTE , '\1[REDACTED]\3' )
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 } " )
2024 end
2125
2226 # @param details [Hash]
@@ -29,6 +33,16 @@ def sanitize_details(details)
2933
3034 private
3135
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+
3246 # @param key [Object]
3347 # @param value [Object]
3448 # @return [Object]
@@ -46,13 +60,13 @@ def sanitize_url(value)
4660 url = value . to_s
4761 return value if url . empty?
4862
49- uri = URI . parse ( url )
63+ normalized_url = Html2rss :: Url . for_channel ( url )
5064 {
51- host : uri . host ,
52- scheme : uri . scheme ,
65+ host : normalized_url . host ,
66+ scheme : normalized_url . scheme ,
5367 hash : Digest ::SHA256 . hexdigest ( url ) [ 0 ..11 ]
5468 } . compact
55- rescue URI :: InvalidURIError
69+ rescue StandardError
5670 { hash : Digest ::SHA256 . hexdigest ( url ) [ 0 ..11 ] }
5771 end
5872 end
0 commit comments