-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: redact Authorization header from HTTP client debug output
- Loading branch information
Showing
3 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,32 @@ | ||
require 'delegate' | ||
|
||
module Pact | ||
module Hal | ||
class AuthorizationHeaderRedactor < SimpleDelegator | ||
def puts(*args) | ||
__getobj__().puts(*redact_args(args)) | ||
end | ||
|
||
def print(*args) | ||
__getobj__().puts(*redact_args(args)) | ||
end | ||
|
||
def <<(*args) | ||
__getobj__().send(:<<, *redact_args(args)) | ||
end | ||
|
||
private | ||
|
||
attr_reader :redactions | ||
|
||
def redact_args(args) | ||
args.collect{ | s| redact(s) } | ||
end | ||
|
||
def redact(string) | ||
return string unless string.is_a?(String) | ||
string.gsub(/Authorization: .*\\r\\n/, "Authorization: [redacted]\\r\\n") | ||
end | ||
end | ||
end | ||
end |
This file contains 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 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,15 @@ | ||
require 'pact/hal/authorization_header_redactor' | ||
|
||
module Pact | ||
module Hal | ||
describe AuthorizationHeaderRedactor do | ||
let(:stream) { StringIO.new } | ||
let(:stream_redactor) { AuthorizationHeaderRedactor.new(stream) } | ||
|
||
it "redacts the authorizaton header" do | ||
stream_redactor << "\\r\\nAuthorization: Bearer TOKEN\\r\\n" | ||
expect(stream.string).to eq "\\r\\nAuthorization: [redacted]\\r\\n" | ||
end | ||
end | ||
end | ||
end |