From 1bf26153b68ec25013ee7f0bb9ae1848070be950 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Tue, 17 Nov 2026 09:26:02 +0200 Subject: [PATCH] fix: accept a SCRAM server signature sent in a challenge --- CHANGELOG.md | 1 + src/scram.ts | 44 ++++++++++++++++-- tests/sasl-scram-failure.test.ts | 80 ++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e9f959..afc11dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - New `SASLMechanism.verifySuccess()` hook lets a mechanism reject ``. - A late answer to a SASL exchange that has already ended is dropped. - A SCRAM server nonce must extend the client nonce with its own part. +- SCRAM accepts a server signature sent in a `` (RFC 6120 § 6.3.10). ## Version 6.0.0 (2026-11-05) diff --git a/src/scram.ts b/src/scram.ts index 8a7458c..35f2f27 100644 --- a/src/scram.ts +++ b/src/scram.ts @@ -147,6 +147,33 @@ function generate_cnonce(): string { return utils.arrayBufToBase64(crypto.getRandomValues(bytes).buffer); } +/** + * @param message - A SCRAM server-final message. + * @returns The server signature (the `v` attribute), if there is one. + */ +function scramServerSignature(message: string): string | undefined { + return message.match(/(?:^|,)v=([^,]+)/)?.[1]; +} + +/** + * Checks the server-final message of a server that sends it in a challenge, + * instead of as additional data with `` (RFC 6120 § 6.3.10). + * @param data - The data of the SASL exchange. + * @param challenge + * @returns The empty response that such a challenge is answered with. + */ +function scramFinalResponse(data: Connection['_sasl_data'], challenge: string): '' { + if (data['server-signature-verified']) { + throw new SASLError(ErrorCondition.SCRAM_BAD_CHALLENGE, 'The SCRAM exchange is already complete.'); + } + if (scramServerSignature(challenge) !== data['server-signature']) { + log.warn('Failing SCRAM authentication because the server signature is missing or wrong.'); + throw new SASLError(ErrorCondition.SCRAM_BAD_SERVER_SIGNATURE, 'The server signature is missing or wrong.'); + } + data['server-signature-verified'] = true; + return ''; +} + const scram = { /** * Whether the Web Crypto `SubtleCrypto` API that SCRAM relies on is available. @@ -163,6 +190,10 @@ const scram = { * * The server signature should be verified after this function completes.. * + * A later challenge is taken to be the server-final message, which a + * server may send that way instead of in ``. It is answered with + * an empty response if its signature is right. + * * On failure, throws a `SASLError` saying why, and the connection aborts * the SASL exchange. * @param connection @@ -179,6 +210,8 @@ const scram = { // Write results to this exchange's data, even if a new exchange has // started by the time the keys are derived. const data = connection._sasl_data; + if (data['server-signature']) return scramFinalResponse(data, challenge); + const cnonce = data.cnonce as string; const challengeData = scramParseChallenge(challenge); @@ -257,6 +290,9 @@ const scram = { * Checks that a `` carries the server signature computed while * answering the server's challenge, so that the server has proven it * knows the password (RFC 5802 § 3). + * + * An empty `` is accepted if the server already sent the right + * signature in a challenge. * @param connection * @param elem - The `` element. * @throws {SASLError} With `SCRAM_BAD_SERVER_SIGNATURE` if no challenge @@ -264,13 +300,15 @@ const scram = { */ verifySuccess(connection: Connection, elem: Element): true { const expected = connection._sasl_data['server-signature']; - let data: string | undefined; + const text = getText(elem) ?? ''; + let signature: string | undefined; try { - data = atob(getText(elem) ?? ''); + signature = scramServerSignature(atob(text)); } catch { // not base64, so no signature } - if (!expected || data?.match(/(?:^|,)v=([^,]+)/)?.[1] !== expected) { + const verified = text ? signature === expected : connection._sasl_data['server-signature-verified']; + if (!expected || !verified) { throw new SASLError(ErrorCondition.SCRAM_BAD_SERVER_SIGNATURE, 'The server signature is missing or wrong.'); } return true; diff --git a/tests/sasl-scram-failure.test.ts b/tests/sasl-scram-failure.test.ts index 905ea4b..eaf644f 100644 --- a/tests/sasl-scram-failure.test.ts +++ b/tests/sasl-scram-failure.test.ts @@ -330,3 +330,83 @@ describe('SCRAM failures on the client side', () => { expect(conn.scram_keys?.name).toBe('SHA-1'); }); }); + +// A server that doesn't send additional data with sends the +// server-final message as a challenge instead (RFC 6120 § 6.3.10). +describe('A SCRAM server signature sent in a challenge', () => { + /** Answers the first challenge, so that the server-final message is next. */ + async function answerFirstChallenge(scram: ReturnType) { + scram.recv(challenge(CHALLENGE)); + await vi.waitFor(() => expect(scram.sent).toHaveLength(1)); + scram.sent.length = 0; + } + + it('is answered with an empty response, and an empty then authenticates', async () => { + const scram = startScram(); + const { conn, sent, statuses, recv } = scram; + await answerFirstChallenge(scram); + + recv(challenge(SERVER_SIGNATURE)); + await vi.waitFor(() => expect(sent).toHaveLength(1)); + expect(sent[0].nodeName).toBe('response'); + expect(sent[0].textContent).toBe(''); + + recv(``); + expect(statuses).toEqual([]); + expect(conn.scram_keys?.name).toBe('SHA-1'); + }); + + it.each([ + ['is wrong', 'v=AAAAAAAAAAAAAAAAAAAAAAAAAAA='], + ['is a server error', 'e=invalid-proof'], + ])('aborts the exchange when it %s', async (_, data) => { + const scram = startScram(); + const { conn, sent, statuses, recv } = scram; + await answerFirstChallenge(scram); + + recv(challenge(data)); + await vi.waitFor(() => expect(statuses).toHaveLength(1)); + expect(statuses[0]).toEqual([Status.AUTHFAIL, ErrorCondition.SCRAM_BAD_SERVER_SIGNATURE, null]); + expect(sent.map((el) => el.nodeName)).toEqual(['abort']); + expect(conn.scram_keys).toBeFalsy(); + }); + + it('fails and disconnects when then carries another signature', async () => { + const scram = startScram(); + const { conn, sent, statuses, recv } = scram; + const close = connected(conn); + await answerFirstChallenge(scram); + recv(challenge(SERVER_SIGNATURE)); + await vi.waitFor(() => expect(sent).toHaveLength(1)); + + recv(success('v=AAAAAAAAAAAAAAAAAAAAAAAAAAA=')); + expect(statuses).toEqual([ + [Status.DISCONNECTING, ErrorCondition.SCRAM_BAD_SERVER_SIGNATURE, undefined], + [Status.AUTHFAIL, ErrorCondition.SCRAM_BAD_SERVER_SIGNATURE, null], + ]); + expect(close).toHaveBeenCalledTimes(1); + expect(conn.scram_keys).toBeFalsy(); + }); + + it('aborts with SCRAM_BAD_CHALLENGE when it comes before the first challenge', async () => { + const { sent, statuses, recv } = startScram(); + recv(challenge(SERVER_SIGNATURE)); + await vi.waitFor(() => expect(statuses).toHaveLength(1)); + + expect(statuses[0]).toEqual([Status.AUTHFAIL, ErrorCondition.SCRAM_BAD_CHALLENGE, null]); + expect(sent.map((el) => el.nodeName)).toEqual(['abort']); + }); + + it('aborts with SCRAM_BAD_CHALLENGE on a challenge after it', async () => { + const scram = startScram(); + const { sent, statuses, recv } = scram; + await answerFirstChallenge(scram); + recv(challenge(SERVER_SIGNATURE)); + await vi.waitFor(() => expect(sent).toHaveLength(1)); + + recv(challenge(SERVER_SIGNATURE)); + await vi.waitFor(() => expect(statuses).toHaveLength(1)); + expect(statuses[0]).toEqual([Status.AUTHFAIL, ErrorCondition.SCRAM_BAD_CHALLENGE, null]); + expect(sent.map((el) => el.nodeName)).toEqual(['response', 'abort']); + }); +});