From 45ca0ea745a6824ea4607da296d3afb6b7971725 Mon Sep 17 00:00:00 2001 From: Nick Szaunig Date: Sun, 27 Sep 2026 05:39:09 +0200 Subject: [PATCH] test(session): tamper inside the signature, not its last character The last base64url character of a 32-byte HMAC signature partly carries padding bits that decoders ignore, so the tampered token was sometimes still valid and the test failed at random. Changing a character in the middle of the signature always alters the signature bytes. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/lib/session.test.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/lib/session.test.ts b/src/lib/session.test.ts index 183fdd1..8512fa9 100644 --- a/src/lib/session.test.ts +++ b/src/lib/session.test.ts @@ -70,8 +70,14 @@ describe("Session-Token", () => { role: "KUNDE", sessionVersion: 0, }); - // Letztes Zeichen der Signatur veraendern. - const tampered = token.slice(0, -1) + (token.endsWith("A") ? "B" : "A"); + // Ein Zeichen MITTEN in der Signatur veraendern. Das letzte Zeichen taugt + // nicht: bei 32 Signaturbytes traegt es in Base64url teils nur Fuellbits, + // die der Decoder ignoriert — der Test schlug dann zufaellig fehl, obwohl + // die Signatur korrekt geprueft wurde. + const signatureStart = token.lastIndexOf(".") + 1; + const position = signatureStart + 10; + const replacement = token[position] === "A" ? "B" : "A"; + const tampered = token.slice(0, position) + replacement + token.slice(position + 1); await expect(verifySessionToken(tampered)).resolves.toBeNull(); });