You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: Mask sensitive credentials in authorization header toString() methods (#238)
- [x] Understand the security issue: Bearer tokens exposed in toString()
methods
- [x] Modify BearerAuthorizationHeader.toString() to mask the token
value (show only first/last few characters)
- [x] Add toStringInsecure() method to BearerAuthorizationHeader that
prints the full token
- [x] Apply same changes for BasicAuthorizationHeader (mask password)
- [x] Apply same changes for DigestAuthorizationHeader (mask nonce,
response, cnonce, opaque)
- [x] Add comprehensive tests for the new toString() and
toStringInsecure() methods
- [x] Run all existing tests to ensure no regressions (3187 tests pass)
- [x] Run static analysis and formatting checks (both pass)
- [x] Manually verify the changes work as expected
- [x] Run CodeQL security checker (no issues detected)
- [x] Address PR review feedback: increase minimum token length to 16
characters
## Summary
This PR addresses a security vulnerability where sensitive
authentication credentials were being exposed in full when toString()
was called on authorization header instances.
### Changes Made:
**BearerAuthorizationHeader:**
- `toString()` now masks the token, showing only first 4 and last 4
characters (e.g., `1234****3456`)
- For tokens <16 chars, shows only `****` (ensures at least 8 characters
are masked)
- Added `toStringInsecure()` method for debugging that exposes the full
token
**BasicAuthorizationHeader:**
- `toString()` now masks the password as `****`
- Added `toStringInsecure()` method for debugging
**DigestAuthorizationHeader:**
- `toString()` now masks sensitive fields: nonce, response, cnonce, and
opaque
- Added `toStringInsecure()` method for debugging
### Testing:
- Added 11 new test cases covering all masking scenarios
- All 3187 existing tests pass
- Manual verification confirms proper masking behavior
This prevents accidental credential leaks in logs while still providing
developers with a way to debug authentication issues in secure
environments using `toStringInsecure()`.
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>Security: Bearer tokens exposed in toString()
methods</issue_title>
> <issue_description>Bearer token values are currently exposed in full
when toString() is called on BearerAuthorizationHeader instances. This
could lead to sensitive authentication tokens being leaked in logs or
debug output.
>
> The toString() method should mask or redact the token value to prevent
accidental exposure of sensitive credentials.
>
> **Related Discussion:**
> - PR: #146
> - Comment:
#146 (comment)
>
> **Reporter:** @coderabbitai</issue_description>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> <comment_new><author>@SandPod</author><body>
> Should obfuscate the token in `toString` but then introduce a
`toStringInsecure` that prints out the full token.</body></comment_new>
> </comments>
>
</details>
Fixes#154
<!-- START COPILOT CODING AGENT TIPS -->
---
✨ Let Copilot coding agent [set things up for
you](https://github.com/serverpod/relic/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Added debug methods to reveal full sensitive authentication data when
needed.
* **Bug Fixes**
* Authorization header strings now mask sensitive credentials (tokens,
passwords, nonces) for security, displaying only essential fields in
logs and output.
* **Tests**
* Added comprehensive tests verifying masking behavior across
authorization header types.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: SandPod <[email protected]>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
0 commit comments