fix(auth): stop flipLastChar's tamper tests from flaking

Corrupting the true last character of a JWT's base64url-encoded
HMAC-SHA256 signature is unreliable: that character encodes only 4
real bits plus 2 unused padding bits, and Go's encoding/base64
ignores those padding bits by default -- about 1 in 4 replacement
characters decode to byte-identical signature bytes, so the
"tampered" cookie still verifies and the test spuriously passes.
Corrupting the second-to-last character instead is deterministic,
since HMAC-SHA256's fixed 32-byte digest length means that position
is always fully significant.

Confirmed via 20 repeated runs (previously ~1/3 failure rate).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-27 10:59:49 +02:00
parent f9b1d454bc
commit 0e3295b746
2 changed files with 25 additions and 12 deletions

View File

@@ -63,7 +63,7 @@ func TestRequireSession_TamperedCookie(t *testing.T) {
if err != nil {
t.Fatalf("mint: %v", err)
}
cookie.Value = flipLastChar(cookie.Value)
cookie.Value = flipSignatureChar(cookie.Value)
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.AddCookie(cookie)
rec := httptest.NewRecorder()