test(auth): stop TestJWT_TamperedToken passing a valid signature
The test overwrote the last character of the signature with "X". An HMAC-SHA256 signature is 32 bytes, so its base64url encoding is 43 characters and the final one carries only four significant bits — U, V, W and X all decode to the same trailing byte. Roughly one token in sixteen was therefore left byte-identical and validly signed, and the test failed because ValidateToken correctly accepted it. Measured at 3098/50000 (6.2%) over distinct signatures; it just failed the Go job on this branch for reasons unrelated to the branch. Flipping a character in the middle of the signature is 0/50000. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -181,8 +181,20 @@ func TestJWT_TamperedToken(t *testing.T) {
|
||||
t.Fatalf("GenerateAccessToken() error: %v", err)
|
||||
}
|
||||
|
||||
// Tamper with the token by modifying the last character of the signature.
|
||||
tampered := token[:len(token)-1] + "X"
|
||||
// Tamper with the token by flipping a bit in the middle of the signature.
|
||||
//
|
||||
// Not the last character: an HMAC-SHA256 signature is 32 bytes, so its
|
||||
// base64url encoding is 43 characters and the last one carries only four
|
||||
// significant bits. U, V, W and X all decode to the same trailing byte, so
|
||||
// overwriting the last character with "X" left roughly one token in
|
||||
// sixteen byte-identical and validly signed — a real 6% flake, measured
|
||||
// over 50k distinct signatures.
|
||||
middle := len(token) - 20
|
||||
flipped := byte('A')
|
||||
if token[middle] == flipped {
|
||||
flipped = 'B'
|
||||
}
|
||||
tampered := token[:middle] + string(flipped) + token[middle+1:]
|
||||
|
||||
_, err = svc.ValidateToken(tampered)
|
||||
if err == nil {
|
||||
|
||||
Reference in New Issue
Block a user