-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlog_sanitizer.rb
More file actions
82 lines (68 loc) · 2.23 KB
/
log_sanitizer.rb
File metadata and controls
82 lines (68 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true
require 'digest'
require 'html2rss/url'
module Html2rss
module Web
##
# Sanitizes request paths and log payloads before they are emitted.
module LogSanitizer
FEED_TOKEN_ROUTE = %r{\A(/api/v1/feeds/)([^/?]+)\z}
class << self
# @param path [String, nil]
# @return [String, nil]
def sanitize_path(path)
return if path.nil?
path_string = path.to_s
suffix = feed_suffix(path_string)
token_path = suffix ? path_string.delete_suffix(suffix) : path_string
token_path.gsub(FEED_TOKEN_ROUTE, "\\1[REDACTED]#{suffix}")
end
# @param details [Hash]
# @return [Hash]
def sanitize_details(details)
details.each_with_object({}) do |(key, value), sanitized|
sanitized[key] = sanitize_value(key, value)
end
end
private
# @param path [String]
# @return [String, nil]
def feed_suffix(path)
return '.json' if path.end_with?('.json')
return '.xml' if path.end_with?('.xml')
return '.rss' if path.end_with?('.rss')
nil
end
# @param key [Object]
# @param value [Object]
# @return [Object]
def sanitize_value(key, value)
return sanitize_details(value) if value.is_a?(Hash)
return value.map { |entry| sanitize_value(key, entry) } if value.is_a?(Array)
return sanitize_url(value) if url_key?(key)
value
end
# @param key [Object]
# @return [Boolean]
def url_key?(key)
key_name = key.to_s
key_name == 'url' || key_name.end_with?('_url', '_urls')
end
# @param value [Object]
# @return [Hash{Symbol=>Object}, Object]
def sanitize_url(value)
url = value.to_s
return value if url.empty?
normalized_url = Html2rss::Url.for_channel(url)
{
host: normalized_url.host,
scheme: normalized_url.scheme,
hash: Digest::SHA256.hexdigest(url)[0..11]
}.compact
rescue StandardError
{ hash: Digest::SHA256.hexdigest(url)[0..11] }
end
end
end
end
end