-
-
Notifications
You must be signed in to change notification settings - Fork 16
feat: redact sensitive feed data in structured logs #903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c70861a
Redact sensitive feed data from logs
gildesmarais 1f316d2
Consolidate structured log emission
gildesmarais 77b3358
Fix log sanitizer loading and cleanup
gildesmarais 40144e2
Tighten log sanitization coverage
gildesmarais 996fa53
Align structured logging grammar
gildesmarais fb6e123
Fix logging fallback sanitization
gildesmarais 2dc9b7b
feat: redaction only when path matches
gildesmarais File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'json' | ||
| require 'logger' | ||
| require 'time' | ||
| require 'uri' | ||
|
gildesmarais marked this conversation as resolved.
Outdated
|
||
|
|
||
| module Html2rss | ||
| module Web | ||
| ## | ||
| # Shared structured logger for application and middleware runtime events. | ||
| module AppLogger | ||
| class << self | ||
| # @return [Logger] | ||
| def logger | ||
| Thread.current[:app_logger] ||= build_logger | ||
| end | ||
|
|
||
| # @return [void] | ||
| def reset_logger! | ||
| Thread.current[:app_logger] = nil | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # @return [Logger] | ||
| def build_logger | ||
| Logger.new($stdout).tap do |log| | ||
| log.formatter = method(:format_entry) | ||
| end | ||
| end | ||
|
|
||
| # @param severity [String] | ||
| # @param datetime [Time] | ||
| # @param _progname [String, nil] | ||
| # @param message [String] | ||
| # @return [String] | ||
| def format_entry(severity, datetime, _progname, message) | ||
| "#{base_payload(severity, datetime).merge(normalize_message(message)).to_json}\n" | ||
| end | ||
|
|
||
| # @param severity [String] | ||
| # @param datetime [Time] | ||
| # @return [Hash{Symbol=>Object}] | ||
| def base_payload(severity, datetime) | ||
| { | ||
| timestamp: datetime.iso8601, | ||
| level: severity, | ||
| service: 'html2rss-web' | ||
| } | ||
| end | ||
|
|
||
| # @param message [Object] | ||
| # @return [Hash{Symbol=>Object}] | ||
| def normalize_message(message) | ||
| parsed_json(message) || parse_logfmt(message.to_s) || { message: message.to_s } | ||
| end | ||
|
|
||
| # @param message [Object] | ||
| # @return [Hash{Symbol=>Object}, nil] | ||
| def parsed_json(message) | ||
| JSON.parse(message.to_s, symbolize_names: true) | ||
| rescue JSON::ParserError, TypeError | ||
| nil | ||
| end | ||
|
|
||
| # @param message [String] | ||
| # @return [Hash{Symbol=>Object}, nil] | ||
| def parse_logfmt(message) | ||
| pairs = message.scan(/([a-zA-Z0-9_.-]+)=("[^"]*"|\S+)/) | ||
| return nil if pairs.empty? | ||
|
|
||
| pairs.to_h do |key, raw_value| | ||
| [key.to_sym, normalize_logfmt_value(raw_value)] | ||
| end | ||
| end | ||
|
|
||
| # @param raw_value [String] | ||
| # @return [String, Integer, Float, TrueClass, FalseClass] | ||
| def normalize_logfmt_value(raw_value) | ||
| value = raw_value.delete_prefix('"').delete_suffix('"') | ||
| return true if value == 'true' | ||
| return false if value == 'false' | ||
| return value.to_i if value.match?(/\A-?\d+\z/) | ||
| return value.to_f if value.match?(/\A-?\d+\.\d+\z/) | ||
|
|
||
| value | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Html2rss | ||
| module Web | ||
| ## | ||
| # Shared structured log emitter for request-scoped application events. | ||
| module LogEvent | ||
| class << self | ||
| # @param payload [Hash{Symbol=>Object}] | ||
| # @param level [Symbol] | ||
| # @return [void] | ||
| def emit(payload:, level: :info) | ||
| logger.public_send(level, build_payload(payload).to_json) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # @return [Logger] | ||
| def logger | ||
| AppLogger.logger | ||
| end | ||
|
|
||
| # @param payload [Hash{Symbol=>Object}] | ||
| # @return [Hash{Symbol=>Object}] | ||
| def build_payload(payload) | ||
| RequestContext.current_h.merge(LogSanitizer.sanitize_details(payload)) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'digest' | ||
| require 'uri' | ||
|
|
||
| module Html2rss | ||
| module Web | ||
| ## | ||
| # Sanitizes request and detail payloads before structured logging. | ||
| module LogSanitizer | ||
| FEED_TOKEN_ROUTE = %r{\A(/api/v1/feeds/)([^/.?]+)(\.(?:json|xml|rss))?\z} | ||
|
|
||
| class << self | ||
| # @param path [String, nil] | ||
| # @return [String, nil] | ||
| def sanitize_path(path) | ||
| return if path.nil? | ||
|
|
||
| path.to_s.gsub(FEED_TOKEN_ROUTE, '\1[REDACTED]\3') | ||
| 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 key [Object] | ||
| # @param value [Object] | ||
| # @return [Object] | ||
| def sanitize_value(key, value) | ||
| return sanitize_url(value) if key.to_sym == :url | ||
| return sanitize_details(value) if value.is_a?(Hash) | ||
| return value.map { |entry| sanitize_value(key, entry) } if value.is_a?(Array) | ||
|
|
||
| value | ||
| end | ||
|
|
||
| # @param value [Object] | ||
| # @return [Hash{Symbol=>Object}, Object] | ||
| def sanitize_url(value) | ||
| url = value.to_s | ||
| return value if url.empty? | ||
|
|
||
| uri = URI.parse(url) | ||
| { | ||
| host: uri.host, | ||
| scheme: uri.scheme, | ||
| hash: Digest::SHA256.hexdigest(url)[0..11] | ||
| }.compact | ||
| rescue URI::InvalidURIError | ||
| { hash: Digest::SHA256.hexdigest(url)[0..11] } | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.