|
48 | 48 | expected_url = { |
49 | 49 | host: 'news.ycombinator.com', |
50 | 50 | scheme: 'https', |
51 | | - hash: Digest::SHA256.hexdigest('https://news.ycombinator.com')[0..11] |
| 51 | + hash: url_hash('https://news.ycombinator.com') |
52 | 52 | } |
53 | 53 |
|
54 | 54 | expect(described_class.sanitize_details(url: 'https://news.ycombinator.com')).to eq(url: expected_url) |
|
60 | 60 | ) |
61 | 61 | end |
62 | 62 |
|
| 63 | + it 'sanitizes nested url fields when emitting shared log events' do |
| 64 | + Html2rss::Web::LogEvent.emit(payload: nested_url_payload) |
| 65 | + |
| 66 | + payload = JSON.parse(io.string.lines.last, symbolize_names: true) |
| 67 | + |
| 68 | + expect(payload.slice(:url, :related_urls, :details)).to eq(expected_nested_url_payload) |
| 69 | + end |
| 70 | + |
63 | 71 | it 'sanitizes security logger token usage fields' do |
64 | 72 | Html2rss::Web::SecurityLogger.log_token_usage('very-secret-token', 'https://news.ycombinator.com', true) |
65 | 73 | payload = JSON.parse(io.string.lines.last, symbolize_names: true) |
|
69 | 77 | url: { |
70 | 78 | host: 'news.ycombinator.com', |
71 | 79 | scheme: 'https', |
72 | | - hash: Digest::SHA256.hexdigest('https://news.ycombinator.com')[0..11] |
| 80 | + hash: url_hash('https://news.ycombinator.com') |
73 | 81 | }, |
74 | 82 | token_hash: Digest::SHA256.hexdigest('very-secret-token')[0..7] |
75 | 83 | ) |
|
88 | 96 | expect(observability_payload.dig(:details, :url)).to eq( |
89 | 97 | host: 'news.ycombinator.com', |
90 | 98 | scheme: 'https', |
91 | | - hash: Digest::SHA256.hexdigest('https://news.ycombinator.com')[0..11] |
| 99 | + hash: url_hash('https://news.ycombinator.com') |
92 | 100 | ) |
93 | 101 | end |
94 | 102 |
|
|
103 | 111 | state: 'completed' |
104 | 112 | ) |
105 | 113 | end |
| 114 | + |
| 115 | + private |
| 116 | + |
| 117 | + # @return [Hash{Symbol=>Object}] |
| 118 | + def nested_url_payload |
| 119 | + { |
| 120 | + url: 'https://news.ycombinator.com', |
| 121 | + related_urls: ['https://example.com/feed.xml'], |
| 122 | + details: { url: 'https://lobste.rs/s/test' } |
| 123 | + } |
| 124 | + end |
| 125 | + |
| 126 | + # @return [Hash{Symbol=>Object}] |
| 127 | + def expected_nested_url_payload |
| 128 | + { |
| 129 | + url: sanitized_url('news.ycombinator.com', 'https://news.ycombinator.com'), |
| 130 | + related_urls: [ |
| 131 | + sanitized_url('example.com', 'https://example.com/feed.xml') |
| 132 | + ], |
| 133 | + details: { |
| 134 | + url: sanitized_url('lobste.rs', 'https://lobste.rs/s/test') |
| 135 | + } |
| 136 | + } |
| 137 | + end |
| 138 | + |
| 139 | + # @param host [String] |
| 140 | + # @param url [String] |
| 141 | + # @return [Hash{Symbol=>String}] |
| 142 | + def sanitized_url(host, url) |
| 143 | + { host:, scheme: 'https', hash: url_hash(url) } |
| 144 | + end |
| 145 | + |
| 146 | + # @param url [String] |
| 147 | + # @return [String] |
| 148 | + def url_hash(url) |
| 149 | + Digest::SHA256.hexdigest(url)[0..11] |
| 150 | + end |
106 | 151 | end |
0 commit comments