fix: race condition in Copy(), invalid AsciiJSON Unicode, Proxy-Authorization leak#4627
Open
barry3406 wants to merge 5 commits intogin-gonic:masterfrom
Open
fix: race condition in Copy(), invalid AsciiJSON Unicode, Proxy-Authorization leak#4627barry3406 wants to merge 5 commits intogin-gonic:masterfrom
barry3406 wants to merge 5 commits intogin-gonic:masterfrom
Conversation
Copy() reads c.Keys before acquiring the read lock, then clones the stale reference under the lock. If another goroutine calls Set() between the read and the lock acquisition, the cKeys variable may reference a map being concurrently modified. This is particularly dangerous on the first Set() call where c.Keys transitions from nil to a new map allocation.
Characters above U+FFFF (emoji, math symbols, etc.) were escaped as \u1f600 (5+ hex digits) which is invalid JSON per RFC 8259. Strict JSON parsers reject this output. Per the spec, supplementary plane characters must be encoded as UTF-16 surrogate pairs (e.g. U+1F600 becomes \uD83D\uDE00).
secureRequestDump only masks the Authorization header but not Proxy-Authorization, which also carries credentials (used by gin's own BasicAuthForProxy middleware). When a panic occurs behind proxy auth, credentials are logged in plaintext.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4627 +/- ##
==========================================
- Coverage 99.21% 98.37% -0.84%
==========================================
Files 42 48 +6
Lines 3182 3143 -39
==========================================
- Hits 3157 3092 -65
- Misses 17 42 +25
- Partials 8 9 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
- Add TestRenderAsciiJSONSupplementaryUnicode to verify emoji (U+1F600) is encoded as UTF-16 surrogate pair (\uD83D\uDE00) - Add Proxy-Authorization test case to TestSecureRequestDump to verify credentials are sanitized in recovery panic logs
Nurysso
suggested changes
Apr 12, 2026
Contributor
Nurysso
left a comment
There was a problem hiding this comment.
LGTM for the most parts, but a few minor suggestions
Contributor
There was a problem hiding this comment.
I think a adding a nil check would be better
// - cKeys := c.Keys
c.mu.RLock()
if c.Keys != nil {
cp.Keys = maps.Clone(c.Keys)
}
c.mu.RUnlock()
Contributor
There was a problem hiding this comment.
Few things I noticed
- Case sensitivity issue: HTTP headers are case-insensitive, but the code only matches exact case. A header like authorization: or AUTHORIZATION: would leak. Use strings.EqualFold or convert to lowercase before comparison.
- Compared to the original implementation, this adds extra complexity (SplitN) without a clear benefit. Since we already know the header name from the prefix check, a simple hardcoded replacement (as before) seems easier to read and maintain.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three independent bug fixes found during code audit.
1. Race condition in
Context.Copy()(context.go)Copy()readsc.Keysbefore acquiring the read lock, then clones the stale reference under the lock:If
Set()is called concurrently between the read and the lock,cKeysmay reference a map being modified. This is especially dangerous on the firstSet()call wherec.Keystransitions fromnilto a new map.Fix: Move the
c.Keysread inside theRLock.2.
AsciiJSONproduces invalid JSON for supplementary Unicode (render/json.go)Characters above U+FFFF (emoji, math symbols) are escaped as
\u1f600(5+ hex digits), which is invalid JSON per RFC 8259. Strict JSON parsers reject this output.Per the spec, supplementary plane characters must be encoded as UTF-16 surrogate pairs (e.g., U+1F600 →
\uD83D\uDE00).Fix: Detect runes > 0xFFFF and encode as surrogate pairs.
3.
Proxy-Authorizationheader leaked in recovery panic logs (recovery.go)secureRequestDumponly masksAuthorizationbut notProxy-Authorization, which also carries credentials (used by gin's ownBasicAuthForProxymiddleware). When a panic occurs behind proxy auth, credentials are logged in plaintext.Fix: Also sanitize
Proxy-Authorizationin the same check.