Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/validateSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ const hasValidSignature = (
break;
}
}
if (!valid) {
// 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 {
Expand Down
43 changes: 43 additions & 0 deletions test/lib/response.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,49 @@ 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 })), {
message: 'Invalid assertion signature.',
});
});

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(',') })),
/Invalid assertion/
);
});

it('rejects it when no decryption key is supplied', async function () {
await assert.rejects(validate(signedEncrypted, options({ privateKey: undefined })), {
message: 'Exception of Assertion Decryption.',
Expand Down
7 changes: 7 additions & 0 deletions test/lib/validateSignature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down