Skip to content

Commit 10efddb

Browse files
committed
feat(webhook status): redact authorization headers in webhook logs
1 parent 7266b1e commit 10efddb

File tree

5 files changed

+74
-16
lines changed

5 files changed

+74
-16
lines changed

lib/pact_broker/domain/webhook_request.rb

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'pact_broker/logging'
44
require 'pact_broker/messages'
55
require 'net/http'
6+
require 'pact_broker/webhooks/redact_logs'
67

78
module PactBroker
89

@@ -56,7 +57,7 @@ def execute
5657
execution_logger.info "HTTP/1.1 #{method.upcase} #{url_with_credentials}"
5758

5859
headers.each_pair do | name, value |
59-
execution_logger.info "#{name}: #{value}"
60+
execution_logger.info Webhooks::RedactLogs.call("#{name}: #{value}")
6061
req[name] = value
6162
end
6263

@@ -91,9 +92,8 @@ def execute
9192

9293
rescue StandardError => e
9394
logger.error "Error executing webhook #{uuid} #{e.class.name} - #{e.message}"
94-
execution_logger.error "Error executing webhook #{uuid} #{e.class.name} - #{e.message}"
9595
logger.error e.backtrace.join("\n")
96-
execution_logger.error e.backtrace.join("\n")
96+
execution_logger.error "Error executing webhook #{uuid} #{e.class.name} - #{e.message}"
9797
WebhookExecutionResult.new(nil, logs.string, e)
9898
end
9999
end
@@ -118,7 +118,5 @@ def url_with_credentials
118118
u
119119
end
120120
end
121-
122121
end
123-
124122
end
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module PactBroker
2+
module Webhooks
3+
class RedactLogs
4+
def self.call logs
5+
logs.gsub(/(Authorization: )(.*)/i,'\1[REDACTED]')
6+
.gsub(/(Token: )(.*)/i,'\1[REDACTED]')
7+
end
8+
end
9+
end
10+
end

spec/lib/pact_broker/domain/webhook_request_spec.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Domain
1919
WebhookRequest.new(
2020
method: 'post',
2121
url: url,
22-
headers: {'Content-Type' => 'text/plain'},
22+
headers: {'Content-Type' => 'text/plain', 'Authorization' => 'foo'},
2323
username: username,
2424
password: password,
2525
body: body)
@@ -73,9 +73,6 @@ module Domain
7373
end
7474

7575
describe "execution logs" do
76-
before do
77-
78-
end
7976

8077
let(:logs) { subject.execute.logs }
8178

@@ -87,6 +84,10 @@ module Domain
8784
expect(logs).to include "Content-Type: text/plain"
8885
end
8986

87+
it "redacts potentially sensitive headers" do
88+
expect(logs).to include "Authorization: [REDACTED]"
89+
end
90+
9091
it "logs the request body" do
9192
expect(logs).to include body
9293
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'pact_broker/webhooks/redact_logs'
2+
3+
module PactBroker
4+
module Webhooks
5+
describe RedactLogs do
6+
describe ".call" do
7+
let(:string) do
8+
"Authorization: foo\nX-Thing: bar"
9+
end
10+
11+
let(:x_auth_string) do
12+
"X-Authorization: bar foo\nX-Thing: bar"
13+
end
14+
15+
let(:x_auth_token) do
16+
"X-Auth-Token: bar foo\nX-Thing: bar"
17+
end
18+
19+
let(:x_authorization_token) do
20+
"X-Authorization-Token: bar foo\nX-Thing: bar"
21+
end
22+
23+
let(:string_lower) do
24+
"authorization: foo\nX-Thing: bar"
25+
end
26+
27+
it "hides the value of the Authorization header" do
28+
expect(RedactLogs.call(string)).to eq "Authorization: [REDACTED]\nX-Thing: bar"
29+
end
30+
31+
it "hides the value of the X-Authorization header" do
32+
expect(RedactLogs.call(x_auth_string)).to eq "X-Authorization: [REDACTED]\nX-Thing: bar"
33+
end
34+
35+
it "hides the value of the X-Auth-Token header" do
36+
expect(RedactLogs.call(x_auth_token)).to eq "X-Auth-Token: [REDACTED]\nX-Thing: bar"
37+
end
38+
39+
it "hides the value of the X-Authorization-Token header" do
40+
expect(RedactLogs.call(x_authorization_token)).to eq "X-Authorization-Token: [REDACTED]\nX-Thing: bar"
41+
end
42+
43+
it "hides the value of the authorization header" do
44+
expect(RedactLogs.call(string_lower)).to eq "authorization: [REDACTED]\nX-Thing: bar"
45+
end
46+
end
47+
end
48+
end
49+
end

spec/spec_helper.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
RACK_ENV = 'test'
66

77
$: << File.expand_path("../../", __FILE__)
8-
require 'rack/test'
8+
99
require 'db'
10-
require 'pact_broker/api'
1110
require 'tasks/database'
11+
require 'pact_broker/db'
12+
raise "Wrong environment!!! Don't run this script!! ENV['RACK_ENV'] is #{ENV['RACK_ENV']} and RACK_ENV is #{RACK_ENV}" if ENV['RACK_ENV'] != 'test' || RACK_ENV != 'test'
13+
PactBroker::DB.connection = PactBroker::Database.database = DB::PACT_BROKER_DB
14+
15+
require 'rack/test'
16+
require 'pact_broker/api'
1217
require 'rspec/its'
1318

1419
Dir.glob("./spec/support/**/*.rb") { |file| require file }
1520

1621
I18n.config.enforce_available_locales = false
1722

1823
RSpec.configure do | config |
19-
config.before :suite do
20-
raise "Wrong environment!!! Don't run this script!! ENV['RACK_ENV'] is #{ENV['RACK_ENV']} and RACK_ENV is #{RACK_ENV}" if ENV['RACK_ENV'] != 'test' || RACK_ENV != 'test'
21-
PactBroker::DB.connection = PactBroker::Database.database = DB::PACT_BROKER_DB
22-
end
23-
2424
config.before :each do
2525
PactBroker.reset_configuration
2626
end

0 commit comments

Comments
 (0)