Skip to content

Commit

Permalink
Redact short and known private tokens
Browse files Browse the repository at this point in the history
Prior this commit very short private tokens (< 4 chars) triggered:
ArgumentError: negative argument

Also, if private token was a term which was part of the inspected string
only the first occurrence was the term was redacted.
  • Loading branch information
splattael committed Jul 4, 2024
1 parent 0766442 commit 41bfc86
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/gitlab/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Client < API
# @return [String]
def inspect
inspected = super
inspected.sub! @private_token, only_show_last_four_chars(@private_token) if @private_token
inspected = redact_private_token(inspected, @private_token) if @private_token
inspected
end

Expand All @@ -91,7 +91,14 @@ def url_encode(url)

private

def redact_private_token(inspected, private_token)
redacted = only_show_last_four_chars(private_token)
inspected.sub %{@private_token="#{private_token}"}, %{@private_token="#{redacted}"}
end

def only_show_last_four_chars(token)
return "****" if token.size <= 4

"#{'*' * (token.size - 4)}#{token[-4..]}"
end
end
Expand Down

0 comments on commit 41bfc86

Please sign in to comment.