From 129091d8fd98b18fd01f86088165b57f3fe1c36f Mon Sep 17 00:00:00 2001 From: Scott Kirkland Date: Mon, 16 Nov 2026 10:04:34 -0800 Subject: [PATCH 1/2] fix: preserve encrypted response fallback with multiple certificates --- lib/validateSignature.ts | 5 ++++- test/lib/response.spec.ts | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/validateSignature.ts b/lib/validateSignature.ts index fb9a6ab4..bc02cb8c 100644 --- a/lib/validateSignature.ts +++ b/lib/validateSignature.ts @@ -219,7 +219,10 @@ const hasValidSignature = ( break; } } - if (!valid) { + // A digest mismatch must reach validate()'s original encrypted-response + // fallback, just as it does with a single certificate. Still throw if + // every verification attempt threw instead of returning false. + if (!valid && valid !== false) { throw new Error('invalid signature: Failed to verify signature against all the certificates provided.'); } } else { diff --git a/test/lib/response.spec.ts b/test/lib/response.spec.ts index b3eb2b9e..232ab1bc 100644 --- a/test/lib/response.spec.ts +++ b/test/lib/response.spec.ts @@ -596,6 +596,46 @@ describe('validate() with an assertion encrypted before the Response was signed' await assert.rejects(validate(signedEncrypted, options({ publicKey: certificate })), /Invalid assertion/); }); + for (const [label, publicKey] of [ + ['active certificate first', [testIdpCert, certificate].join(',')], + ['active certificate last', [certificate, testIdpCert].join(',')], + ['duplicate certificates', [testIdpCert, testIdpCert].join(',')], + ]) { + it(`accepts the encrypted Response with ${label}`, async function () { + const profile = await validate(signedEncrypted, options({ publicKey })); + assert.deepStrictEqual(profile, await validate(signedEncrypted, options())); + }); + + it(`rejects a tampered encrypted Response with ${label}`, async function () { + const tampered = signedEncrypted.replace( + `Destination="${ACS}"`, + 'Destination="https://evil.example.com/acs"' + ); + assert.notStrictEqual(tampered, signedEncrypted); + await assert.rejects(validate(tampered, options({ publicKey }))); + }); + + it(`still checks the audience with ${label}`, async function () { + await assert.rejects( + validate(signedEncrypted, options({ publicKey, audience: 'https://wrong.example.com' })), + /Invalid audience/ + ); + }); + + it(`still checks request correlation with ${label}`, async function () { + await assert.rejects( + validate(signedEncrypted, options({ publicKey, inResponseTo: '_wrong-request' })), + /Invalid InResponseTo/ + ); + }); + } + + it('rejects the encrypted Response when none of the certificates is trusted', async function () { + await assert.rejects( + validate(signedEncrypted, options({ publicKey: [certificate, certificate].join(',') })) + ); + }); + it('rejects it when no decryption key is supplied', async function () { await assert.rejects(validate(signedEncrypted, options({ privateKey: undefined })), { message: 'Exception of Assertion Decryption.', From 9226fcf6d9cfd14a0c60e7060af5b2d2b5d43b92 Mon Sep 17 00:00:00 2001 From: Deepak Prabhakara Date: Sat, 21 Nov 2026 23:21:02 +0000 Subject: [PATCH 2/2] test: pin errors for the multi-certificate encrypted response fallback Assert the specific rejection for a tampered Response and an untrusted certificate list, and cover a digest mismatch with multiple certificates directly in validateSignature. Write the check as `valid === undefined`. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01EdsD5LceNhxMXQTQv7x1Dt --- lib/validateSignature.ts | 6 ++---- test/lib/response.spec.ts | 7 +++++-- test/lib/validateSignature.spec.ts | 7 +++++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/validateSignature.ts b/lib/validateSignature.ts index bc02cb8c..36dd2b6c 100644 --- a/lib/validateSignature.ts +++ b/lib/validateSignature.ts @@ -219,10 +219,8 @@ const hasValidSignature = ( break; } } - // A digest mismatch must reach validate()'s original encrypted-response - // fallback, just as it does with a single certificate. Still throw if - // every verification attempt threw instead of returning false. - if (!valid && valid !== false) { + // A digest mismatch returns null, as with a single certificate, so validate() can retry the encrypted XML. + if (valid === undefined) { throw new Error('invalid signature: Failed to verify signature against all the certificates provided.'); } } else { diff --git a/test/lib/response.spec.ts b/test/lib/response.spec.ts index 232ab1bc..e9131783 100644 --- a/test/lib/response.spec.ts +++ b/test/lib/response.spec.ts @@ -612,7 +612,9 @@ describe('validate() with an assertion encrypted before the Response was signed' 'Destination="https://evil.example.com/acs"' ); assert.notStrictEqual(tampered, signedEncrypted); - await assert.rejects(validate(tampered, options({ publicKey }))); + await assert.rejects(validate(tampered, options({ publicKey })), { + message: 'Invalid assertion signature.', + }); }); it(`still checks the audience with ${label}`, async function () { @@ -632,7 +634,8 @@ describe('validate() with an assertion encrypted before the Response was signed' it('rejects the encrypted Response when none of the certificates is trusted', async function () { await assert.rejects( - validate(signedEncrypted, options({ publicKey: [certificate, certificate].join(',') })) + validate(signedEncrypted, options({ publicKey: [certificate, certificate].join(',') })), + /Invalid assertion/ ); }); diff --git a/test/lib/validateSignature.spec.ts b/test/lib/validateSignature.spec.ts index 8c06f139..fd3fae35 100644 --- a/test/lib/validateSignature.spec.ts +++ b/test/lib/validateSignature.spec.ts @@ -416,6 +416,13 @@ describe('validateSignature() failure mode', function () { message: 'invalid signature: the signature does not verify', }); }); + + it('reports a digest mismatch the same way with multiple certificates', function () { + const tampered = signed().replace('jo@example.com', 'eve@example.com'); + assert.throws(() => validateSignature(tampered, [testIdpCert, testIdpCert].join(','), null), { + message: 'invalid signature: the signature does not verify', + }); + }); }); describe('validateSignature.ts - algorithm allowlists', function () {