Skip to content

Commit f23471a

Browse files
authored
Merge pull request #124 from github/rewinfrey/add-pattern-for-bearer-tokens
Add Authorization Bearer token pattern
2 parents 9b77750 + f7042d2 commit f23471a

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

lib/patterns/default.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ module Patterns
4747
# Vault Tokens
4848
# https://github.com/hashicorp/vault/issues/27151
4949
/[sbr]\.[a-zA-Z0-9]{24,}/, # <= 1.9.x
50-
/hv[sbr]\.[a-zA-Z0-9]{24,}/ # >= 1.10
50+
/hv[sbr]\.[a-zA-Z0-9]{24,}/, # >= 1.10
51+
52+
# Authorization bearer tokens
53+
# https://datatracker.ietf.org/doc/html/rfc6750#section-2.1
54+
/(?i)authorization:\s+bearer\s+[A-Za-z0-9\-_\.=~+\/]+/,
5155
].freeze
5256
end

spec/lib/redacting_logger_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,51 @@
160160
case: "redacts a RubyGems token",
161161
message: "using rubygems token: rubygems_0123456789abcdef0123456789abcdef0123456789abcdef",
162162
expected_message: "using rubygems token: [REDACTED]"
163+
},
164+
{
165+
case: "redacts authorization bearer token",
166+
message: '-H "Authorization: Bearer ab123456789a1abcd1~_.-+456ABCDE=" -H "Content-Type: application/json"',
167+
expected_message: '-H "[REDACTED]" -H "Content-Type: application/json"'
168+
},
169+
{
170+
case: "redacts authorization bearer token with case insensitivity",
171+
message: '-H "authorizAtion: beaRer ab123456789a1abcd1~_.-+456ABCDE=" -H "Content-Type: application/json"',
172+
expected_message: '-H "[REDACTED]" -H "Content-Type: application/json"'
173+
},
174+
{
175+
case: "redacts authorization bearer token with extra spaces and tabs",
176+
message: "authorization: bearer abcd1234",
177+
expected_message: "[REDACTED]"
178+
},
179+
{
180+
case: "redacts authorization bearer token with special characters",
181+
message: "authorization: bearer aBcD-_=~+/1234",
182+
expected_message: "[REDACTED]"
183+
},
184+
{
185+
case: "redacts authorization bearer token at start of string",
186+
message: "authorization: bearer tokenatstart",
187+
expected_message: "[REDACTED]"
188+
},
189+
{
190+
case: "redacts authorization bearer token at end of string",
191+
message: "some text authorization: bearer tokenatend",
192+
expected_message: "some text [REDACTED]"
193+
},
194+
{
195+
case: "redacts multiple authorization bearer tokens in one string",
196+
message: "authorization: bearer token1 and authorization: bearer token2",
197+
expected_message: "[REDACTED] and [REDACTED]"
198+
},
199+
{
200+
case: "redacts authorization bearer token with minimum plausible length",
201+
message: "authorization: bearer a",
202+
expected_message: "[REDACTED]"
203+
},
204+
{
205+
case: "redacts authorization bearer token with maximum plausible length",
206+
message: "authorization: bearer #{'a' * 256}",
207+
expected_message: "[REDACTED]"
163208
}
164209
].each do |test|
165210
it "redacts #{test[:case]}" do

0 commit comments

Comments
 (0)