|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | +require 'stringio' |
| 5 | + |
| 6 | +require_relative '../../../app/web/request/request_context' |
| 7 | +require_relative '../../../app/web/security/security_logger' |
| 8 | +require_relative '../../../app/web/telemetry/app_logger' |
| 9 | +require_relative '../../../app/web/telemetry/log_sanitizer' |
| 10 | +require_relative '../../../app/web/telemetry/observability' |
| 11 | + |
| 12 | +RSpec.describe Html2rss::Web::LogSanitizer do |
| 13 | + let(:io) { StringIO.new } |
| 14 | + let(:logger) { Logger.new(io).tap { |log| log.formatter = Html2rss::Web::AppLogger.send(:method, :format_entry) } } |
| 15 | + let(:context) do |
| 16 | + Html2rss::Web::RequestContext::Context.new( |
| 17 | + request_id: 'req-123', |
| 18 | + path: '/api/v1/feeds/[REDACTED]', |
| 19 | + http_method: 'GET', |
| 20 | + route_group: 'api_v1', |
| 21 | + actor: nil, |
| 22 | + strategy: 'faraday', |
| 23 | + started_at: '2026-03-21T00:00:00Z' |
| 24 | + ) |
| 25 | + end |
| 26 | + |
| 27 | + before do |
| 28 | + Html2rss::Web::RequestContext.set!(context) |
| 29 | + Html2rss::Web::AppLogger.reset_logger! |
| 30 | + Html2rss::Web::SecurityLogger.reset_logger! |
| 31 | + allow(Html2rss::Web::AppLogger).to receive(:logger).and_return(logger) |
| 32 | + allow(Html2rss::Web::SecurityLogger).to receive(:logger).and_return(logger) |
| 33 | + allow(Html2rss::Web::Observability).to receive(:logger).and_return(logger) |
| 34 | + end |
| 35 | + |
| 36 | + after do |
| 37 | + Html2rss::Web::RequestContext.clear! |
| 38 | + end |
| 39 | + |
| 40 | + it 'redacts feed tokens from token feed request paths' do |
| 41 | + expect(described_class.sanitize_path('/api/v1/feeds/token-value-123')).to eq('/api/v1/feeds/[REDACTED]') |
| 42 | + expect(described_class.sanitize_path('/api/v1/feeds/token-value-123.json')).to eq('/api/v1/feeds/[REDACTED].json') |
| 43 | + end |
| 44 | + |
| 45 | + it 'replaces logged urls with hashed host metadata' do |
| 46 | + expected_url = { |
| 47 | + host: 'news.ycombinator.com', |
| 48 | + scheme: 'https', |
| 49 | + hash: Digest::SHA256.hexdigest('https://news.ycombinator.com')[0..11] |
| 50 | + } |
| 51 | + |
| 52 | + expect(described_class.sanitize_details(url: 'https://news.ycombinator.com')).to eq(url: expected_url) |
| 53 | + end |
| 54 | + |
| 55 | + it 'sanitizes security logger token usage fields' do |
| 56 | + Html2rss::Web::SecurityLogger.log_token_usage('very-secret-token', 'https://news.ycombinator.com', true) |
| 57 | + payload = JSON.parse(io.string.lines.last, symbolize_names: true) |
| 58 | + |
| 59 | + expect(payload.slice(:path, :url, :token_hash)).to eq( |
| 60 | + path: '/api/v1/feeds/[REDACTED]', |
| 61 | + url: { |
| 62 | + host: 'news.ycombinator.com', |
| 63 | + scheme: 'https', |
| 64 | + hash: Digest::SHA256.hexdigest('https://news.ycombinator.com')[0..11] |
| 65 | + }, |
| 66 | + token_hash: Digest::SHA256.hexdigest('very-secret-token')[0..7] |
| 67 | + ) |
| 68 | + end |
| 69 | + |
| 70 | + it 'sanitizes observability details' do |
| 71 | + Html2rss::Web::Observability.emit( |
| 72 | + event_name: 'feed.render', |
| 73 | + outcome: 'success', |
| 74 | + details: { url: 'https://news.ycombinator.com', strategy: 'faraday' } |
| 75 | + ) |
| 76 | + |
| 77 | + lines = io.string.lines.map { |line| JSON.parse(line, symbolize_names: true) } |
| 78 | + observability_payload = lines.first |
| 79 | + |
| 80 | + expect(observability_payload.dig(:details, :url)).to eq( |
| 81 | + host: 'news.ycombinator.com', |
| 82 | + scheme: 'https', |
| 83 | + hash: Digest::SHA256.hexdigest('https://news.ycombinator.com')[0..11] |
| 84 | + ) |
| 85 | + end |
| 86 | + |
| 87 | + it 'formats rack-timeout logfmt as json' do |
| 88 | + logger.info('source=rack-timeout id=req-123 timeout=15000ms state=completed') |
| 89 | + |
| 90 | + payload = JSON.parse(io.string.lines.last, symbolize_names: true) |
| 91 | + expect(payload).to include( |
| 92 | + source: 'rack-timeout', |
| 93 | + id: 'req-123', |
| 94 | + timeout: '15000ms', |
| 95 | + state: 'completed' |
| 96 | + ) |
| 97 | + end |
| 98 | +end |
0 commit comments