Skip to content

Commit

Permalink
feature: Add query_string config to filter query string
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Sep 19, 2022
1 parent 3186bdf commit fa423d8
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 169 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Currently this gem provides following features
- [x] Sanitizing POST params
- [x] Sanitizing HTTP headers
- [x] Sanitizing cookies
- [x] Sanitizing query string
- [x] Sanitizing extras ([see](https://docs.sentry.io/platforms/ruby/enriching-events/context/#additional-data) `Sentry.set_extras`)

## Installation
Expand Down
22 changes: 20 additions & 2 deletions lib/sentry/sanitizer/cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(config)
@fields = config.fields || []
@http_headers = config.http_headers || DEFAULT_SENSITIVE_HEADERS
@do_cookies = config.cookies || false
@do_query_string = config.query_string || false
end

def call(event)
Expand All @@ -33,14 +34,17 @@ def sanitize_request(event, type)
event.request.data = sanitize_data(event.request.data)
event.request.headers = sanitize_headers(event.request.headers)
event.request.cookies = sanitize_cookies(event.request.cookies)
event.request.query_string = sanitize_query_string(event.request.query_string)
when :stringified_hash
event['request']['data'] = sanitize_data(event['request']['data'])
event['request']['headers'] = sanitize_headers(event['request']['headers'])
event['request']['cookies'] = sanitize_cookies(event['request']['cookies'])
event['request']['query_string'] = sanitize_query_string(event['request']['query_string'])
when :symbolized_hash
event[:request][:data] = sanitize_data(event[:request][:data])
event[:request][:headers] = sanitize_headers(event[:request][:headers])
event[:request][:cookies] = sanitize_cookies(event[:request][:cookies])
event[:request][:query_string] = sanitize_query_string(event[:request][:query_string])
end
end

Expand All @@ -53,7 +57,7 @@ def sanitize_data(hash)

private

attr_reader :fields, :http_headers, :do_cookies
attr_reader :fields, :http_headers, :do_cookies, :do_query_string

# Sanitize specified headers
def sanitize_headers(headers)
Expand All @@ -76,12 +80,26 @@ def sanitize_headers(headers)

# Sanitize all cookies
def sanitize_cookies(cookies)
return cookies unless cookies.is_a? Hash
return cookies unless do_cookies
return cookies unless cookies.is_a? Hash

cookies.transform_values { DEFAULT_MASK }
end

def sanitize_query_string(query_string)
return query_string unless do_query_string
return query_string unless query_string.is_a? String

sanitized_array = query_string.split('&').map do |kv_pair|
k, v = kv_pair.split('=')
new_v = sanitize_string(k, v)

"#{k}=#{new_v}"
end

sanitized_array.join('&')
end

def sanitize_value(value, key)
case value
when Hash
Expand Down
14 changes: 11 additions & 3 deletions lib/sentry/sanitizer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class Configuration

module Sanitizer
class Configuration
attr_accessor :fields, :http_headers, :cookies
attr_accessor :fields, :http_headers, :cookies, :query_string

def configured?
[fields, http_headers, cookies].any? { |setting| !setting.nil? }
[fields, http_headers, cookies, query_string].any? { |setting| !setting.nil? }
end

def fields=(fields)
Expand All @@ -48,11 +48,19 @@ def http_headers=(headers)

def cookies=(cookies)
unless [TrueClass, FalseClass].include?(cookies.class)
raise ArgumentError, 'sanitize_cookies must be boolean'
raise ArgumentError, 'cookies must be boolean'
end

@cookies = cookies
end

def query_string=(query_string)
unless [TrueClass, FalseClass].include?(query_string.class)
raise ArgumentError, 'query_string must be boolean'
end

@query_string = query_string
end
end
end
end
2 changes: 1 addition & 1 deletion lib/sentry/sanitizer/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Sentry
module Sanitizer
VERSION = '0.5.1'
VERSION = '0.6.0'
end
end
Loading

0 comments on commit fa423d8

Please sign in to comment.