Skip to content

Commit

Permalink
Add filter_metadata config option
Browse files Browse the repository at this point in the history
We had a report of an app that contains sensitive information in the
request path and the desire to filter this out. We have no system in
place to filter metadata like path and request method, as set by the
Sinatra middleware.

This change allow apps to filter out some metadata that's set by
default, like `path`, to avoid sending PII or other sensitive data,
using the `filter_metadata` config option.

Filtering is done with String based keys, like all the other `filter_*`
config options are, so the keys need to be transformed to keys
beforehand to make sure they're filtered out.
  • Loading branch information
tombruijn committed Jun 26, 2023
1 parent e19bad1 commit c7d09bd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changesets/add-filter_metadata-config-option.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: "patch"
type: "add"
---

Add `filter_metadata` config option to filter metadata set on Transactions set by default. Metadata like `path`, (request) `method`, `request_id`, `hostname`, etc. This can be useful if there's PII or other sensitive data in any of the app's metadata.
3 changes: 3 additions & 0 deletions lib/appsignal/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Config
:enable_rails_error_reporter => true,
:endpoint => "https://push.appsignal.com",
:files_world_accessible => true,
:filter_metadata => [],
:filter_parameters => [],
:filter_session_data => [],
:ignore_actions => [],
Expand Down Expand Up @@ -77,6 +78,7 @@ class Config
"APPSIGNAL_ENABLE_GVL_WAITING_THREADS" => :enable_gvl_waiting_threads,
"APPSIGNAL_ENABLE_RAILS_ERROR_REPORTER" => :enable_rails_error_reporter,
"APPSIGNAL_FILES_WORLD_ACCESSIBLE" => :files_world_accessible,
"APPSIGNAL_FILTER_METADATA" => :filter_metadata,
"APPSIGNAL_FILTER_PARAMETERS" => :filter_parameters,
"APPSIGNAL_FILTER_SESSION_DATA" => :filter_session_data,
"APPSIGNAL_HOSTNAME" => :hostname,
Expand Down Expand Up @@ -150,6 +152,7 @@ class Config
# @api private
ENV_ARRAY_KEYS = %w[
APPSIGNAL_DNS_SERVERS
APPSIGNAL_FILTER_METADATA
APPSIGNAL_FILTER_PARAMETERS
APPSIGNAL_FILTER_SESSION_DATA
APPSIGNAL_IGNORE_ACTIONS
Expand Down
13 changes: 8 additions & 5 deletions lib/appsignal/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ def set_http_or_background_queue_start

def set_metadata(key, value)
return unless key && value
return if Appsignal.config[:filter_metadata].include?(key.to_s)

@ext.set_metadata(key, value)
end
Expand Down Expand Up @@ -337,7 +338,7 @@ def sample_data
:params => sanitized_params,
:environment => sanitized_environment,
:session_data => sanitized_session_data,
:metadata => metadata,
:metadata => sanitized_metadata,
:tags => sanitized_tags,
:breadcrumbs => breadcrumbs
}.each do |key, data|
Expand Down Expand Up @@ -522,12 +523,14 @@ def sanitized_session_data
)
end

# Returns metadata from the environment.
# Returns sanitized metadata set by {#set_metadata} and from the
# {#environment}.
#
# @return [nil] if no `:metadata` key is present in the {#environment}.
# @return [Hash<String, Object>]
def metadata
environment[:metadata]
def sanitized_metadata
(environment[:metadata] || {})
.transform_keys(&:to_s)
.except(*Appsignal.config[:filter_metadata])
end

# Returns the environment for a transaction.
Expand Down
1 change: 1 addition & 0 deletions spec/lib/appsignal/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
:enable_rails_error_reporter => true,
:endpoint => "https://push.appsignal.com",
:files_world_accessible => true,
:filter_metadata => [],
:filter_parameters => [],
:filter_session_data => [],
:ignore_actions => [],
Expand Down
33 changes: 27 additions & 6 deletions spec/lib/appsignal/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,18 @@ def current_transaction
expect(transaction.to_h["metadata"]).to eq("request_method" => "GET")
end

context "when filter_metadata includes metadata key" do
before { Appsignal.config[:filter_metadata] = ["filter_key"] }
after { Appsignal.config[:filter_metadata] = [] }

it "does not set the metadata on the transaction" do
transaction.set_metadata(:filter_key, "filtered value")
transaction.set_metadata("filter_key", "filtered value")

expect(transaction.to_h["metadata"].keys).to_not include("filter_key")
end
end

context "when the key is nil" do
it "does not update the metadata on the transaction" do
transaction.set_metadata(nil, "GET")
Expand Down Expand Up @@ -1275,25 +1287,34 @@ def session_exists?(_env)
end
end

describe "#metadata" do
subject { transaction.send(:metadata) }
describe "#sanitized_metadata" do
subject { transaction.send(:sanitized_metadata) }

context "when request is nil" do
let(:request) { nil }

it { is_expected.to be_nil }
it { is_expected.to eq({}) }
end

context "when env is nil" do
before { expect(transaction.request).to receive(:env).and_return(nil) }

it { is_expected.to be_nil }
it { is_expected.to eq({}) }
end

context "when env is present" do
let(:env) { { :metadata => { :key => "value" } } }
let(:env) { { "key" => "value" } }

it { is_expected.to eq("key" => "value") }

it { is_expected.to eq env[:metadata] }
context "with filter_metadata option set" do
before { Appsignal.config[:filter_metadata] = ["key"] }
after { Appsignal.config[:filter_metadata] = [] }

it "filters out keys listed in the filter_metadata option" do
expect(subject.keys).to_not include("key")
end
end
end
end

Expand Down

0 comments on commit c7d09bd

Please sign in to comment.