Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- New `SASLMechanism.verifySuccess()` hook lets a mechanism reject `<success>`.
- 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 `<challenge>` (RFC 6120 § 6.3.10).

## Version 6.0.0 (2026-11-05)

Expand Down
44 changes: 41 additions & 3 deletions src/scram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<success>` (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.
Expand All @@ -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 `<success>`. 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
Expand All @@ -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);

Expand Down Expand Up @@ -257,20 +290,25 @@ const scram = {
* Checks that a `<success>` 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 `<success/>` is accepted if the server already sent the right
* signature in a challenge.
* @param connection
* @param elem - The `<success>` element.
* @throws {SASLError} With `SCRAM_BAD_SERVER_SIGNATURE` if no challenge
* was answered, or the signature is missing, malformed or wrong.
*/
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;
Expand Down
80 changes: 80 additions & 0 deletions tests/sasl-scram-failure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <success> 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<typeof startScram>) {
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 <success/> 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(`<success xmlns="${Strophe.NS.SASL}"/>`);
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 <success> 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']);
});
});