Skip to content

Commit 4b84f49

Browse files
committed
test(session): stabilise tampered-JWT-signature test
Flipping the last base64url char of the JWT signature could canonicalise to the original bytes; flip a middle byte instead so the decoded signature is deterministically different.
1 parent daac2ee commit 4b84f49

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

tests/SessionManagerTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,13 @@ public function testAuthenticateRejectsTamperedSignature(): void
210210
'exp' => time() + 3600,
211211
]);
212212

213-
// Flip the last byte of the signature segment.
213+
// Flip a byte in the middle of the signature segment so the base64
214+
// decoder produces a clearly different signature. Avoids the trailing
215+
// padding bits that base64url can canonicalise away.
214216
$parts = explode('.', $jwt);
215217
$sig = $parts[2];
216-
$parts[2] = substr($sig, 0, -1) . ($sig[-1] === 'A' ? 'B' : 'A');
218+
$mid = intdiv(strlen($sig), 2);
219+
$parts[2] = substr($sig, 0, $mid) . ($sig[$mid] === 'A' ? 'B' : 'A') . substr($sig, $mid + 1);
217220
$tampered = implode('.', $parts);
218221

219222
$sealed = SessionManager::sealSessionFromAuthResponse(

0 commit comments

Comments
 (0)