mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
GHSA-rg85-qx5c-qx67 reported the InviteBleed brute force again, now as a GitHub Security Advisory. The 128-bit code of the first fix was still in place, but two things it left were open: - An invitation made before the fix, and not used yet, kept its six-digit Math.random() code and was still accepted at sign-up; a re-invite even mailed the same code again. A code shorter than the 22 base64url characters of a 128-bit code is now refused at sign-up, in both places it is looked up, and hasUsableCode() treats it as missing, so a re-invite replaces it with a strong one (models/lib/invitationCodeEmail.js isStrongInvitationCode). - The DDPRateLimiter rule was on createUser, which a client is refused anyway, while the sign-up form creates the account through useraccounts' ATCreateUserServer, which had no limit. It now takes at most 10 attempts a minute per address, and each refusal is an InviteBleed row in Admin Panel -> Problems (brute.invite). The other two advisories of the same report were checked against the current code and are already fixed: GHSA-6944-p22h-3qpx (OIDC shared serviceData race) since #4897 on 2026-07-17, whose per-login objects are locals of the callback, and GHSA-j4x8-689m-h4hv (stale impersonation record allows export), where every export route authorizes through canExportBoardData() only and the impersonation record is just the audit row written after it. Thanks to meifukun and xet7 !
221 lines
9.4 KiB
JavaScript
221 lines
9.4 KiB
JavaScript
'use strict';
|
|
|
|
// Plain-Node unit test (no Meteor) for the invitation-code email rules.
|
|
// Run: node tests/invitationCodeEmail.test.cjs
|
|
//
|
|
// Regression guard for #4043 ("Sometimes user get board invitation by mail
|
|
// without invitation code"): recipients received an invitation email whose
|
|
// code the sign-up form rejected with "The invitation code doesn't exist".
|
|
// Sign-up only accepts { code: <string from the form>, email, valid: true }
|
|
// (server/models/users.js onCreateUser), so the sending side
|
|
// (server/models/settings.js sendInvitation / sendInvitationEmail) must:
|
|
// - store the invitee email lowercase (Mongo matching is case sensitive);
|
|
// - on RE-INVITE, overwrite a stale/invalid code with a fresh valid one
|
|
// instead of re-mailing a code that can never validate;
|
|
// - refuse (loudly) to send an email for a missing/invalid code;
|
|
// - on SMTP failure, only delete a code created by this very invite — never
|
|
// a pre-existing one whose code was already delivered in an earlier email.
|
|
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const {
|
|
normalizeInviteEmail,
|
|
hasUsableCode,
|
|
isInvitationCodeSendable,
|
|
buildReinviteModifier,
|
|
shouldRemoveInvitationOnEmailFailure,
|
|
} = require('../models/lib/invitationCodeEmail');
|
|
|
|
let passed = 0;
|
|
function test(name, fn) {
|
|
fn();
|
|
passed += 1;
|
|
console.log(' ok -', name);
|
|
}
|
|
|
|
// Deterministic stand-in for the code generator (only its String coercion is
|
|
// tested here).
|
|
const generateCode = () => 654321;
|
|
// A code as generateInvitationCode makes it since InviteBleed: 128 bits,
|
|
// base64url. A six-digit code is no longer usable (models/lib/invitationCodeEmail.js
|
|
// isStrongInvitationCode), so the tests of a usable code use this one.
|
|
const STRONG = 'AbCdEfGhIjKlMnOpQrStUv';
|
|
|
|
// Mimic the sign-up lookup selector { code, email, valid: true } from
|
|
// server/models/users.js onCreateUser, with Mongo's exact-match semantics
|
|
// (strict type and case sensitivity).
|
|
function signupLookupMatches(doc, typedCode, typedEmail) {
|
|
return (
|
|
doc.code === typedCode && doc.email === typedEmail && doc.valid === true
|
|
);
|
|
}
|
|
|
|
// --- POSITIVE ----------------------------------------------------------------
|
|
|
|
test('normalizeInviteEmail lowercases and trims the address', () => {
|
|
assert.strictEqual(
|
|
normalizeInviteEmail(' User@Example.COM \n'),
|
|
'user@example.com',
|
|
);
|
|
assert.strictEqual(normalizeInviteEmail('plain@example.com'), 'plain@example.com');
|
|
});
|
|
|
|
test('a valid pending invitation is sendable', () => {
|
|
assert.strictEqual(
|
|
isInvitationCodeSendable({ code: STRONG, valid: true }),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test('a legacy invitation without a valid field is still sendable', () => {
|
|
// Documents predating the `valid` defaultValue must keep working.
|
|
assert.strictEqual(isInvitationCodeSendable({ code: STRONG }), true);
|
|
});
|
|
|
|
test('re-invite of a STILL-VALID invitation keeps its code (earlier email stays usable)', () => {
|
|
const invitation = { _id: 'i1', code: STRONG, valid: true, email: 'a@b.c' };
|
|
const modifier = buildReinviteModifier(invitation, ['board1'], generateCode);
|
|
assert.deepStrictEqual(modifier, { $set: { boardsToBeInvited: ['board1'] } });
|
|
// No code/valid churn: the code delivered in the first email still validates.
|
|
assert.strictEqual('code' in modifier.$set, false);
|
|
assert.strictEqual('valid' in modifier.$set, false);
|
|
});
|
|
|
|
test('#4043: re-invite of an INVALIDATED invitation overwrites it with a fresh valid code', () => {
|
|
const invitation = { _id: 'i1', code: '111111', valid: false, email: 'a@b.c' };
|
|
const modifier = buildReinviteModifier(invitation, ['board1', 'board2'], generateCode);
|
|
assert.deepStrictEqual(modifier.$set.boardsToBeInvited, ['board1', 'board2']);
|
|
assert.strictEqual(modifier.$set.code, '654321'); // String, not Number
|
|
assert.strictEqual(typeof modifier.$set.code, 'string');
|
|
assert.strictEqual(modifier.$set.valid, true);
|
|
|
|
// The document after the update matches what sign-up will look for.
|
|
const updatedDoc = { ...invitation, ...modifier.$set };
|
|
assert.strictEqual(signupLookupMatches(updatedDoc, '654321', 'a@b.c'), true);
|
|
});
|
|
|
|
test('SMTP failure on a NEW invitation rolls the code back (nothing was ever delivered)', () => {
|
|
assert.strictEqual(
|
|
shouldRemoveInvitationOnEmailFailure({ isNewInvitation: true }),
|
|
true,
|
|
);
|
|
});
|
|
|
|
// --- NEGATIVE ----------------------------------------------------------------
|
|
|
|
test('normalizeInviteEmail returns "" for non-string input (caller skips it)', () => {
|
|
assert.strictEqual(normalizeInviteEmail(null), '');
|
|
assert.strictEqual(normalizeInviteEmail(undefined), '');
|
|
assert.strictEqual(normalizeInviteEmail(123456), '');
|
|
assert.strictEqual(normalizeInviteEmail({}), '');
|
|
});
|
|
|
|
test('#4043: an invalidated invitation is NOT sendable', () => {
|
|
assert.strictEqual(
|
|
isInvitationCodeSendable({ code: '123456', valid: false }),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test('#4043: a missing/empty/non-string code is NOT sendable', () => {
|
|
assert.strictEqual(isInvitationCodeSendable(null), false);
|
|
assert.strictEqual(isInvitationCodeSendable(undefined), false);
|
|
assert.strictEqual(isInvitationCodeSendable({ valid: true }), false);
|
|
assert.strictEqual(isInvitationCodeSendable({ code: '', valid: true }), false);
|
|
// A Number code can never match the string typed into the sign-up form.
|
|
assert.strictEqual(isInvitationCodeSendable({ code: 123456, valid: true }), false);
|
|
assert.strictEqual(hasUsableCode({ code: 123456 }), false);
|
|
});
|
|
|
|
test('re-invite regenerates when the stored code is a Number or missing', () => {
|
|
for (const invitation of [
|
|
{ _id: 'i1', code: 111111, valid: true }, // legacy Number code
|
|
{ _id: 'i1', valid: true }, // no code at all
|
|
{ _id: 'i1', code: '', valid: true }, // empty code
|
|
null, // no document
|
|
]) {
|
|
const modifier = buildReinviteModifier(invitation, ['b'], generateCode);
|
|
assert.strictEqual(modifier.$set.code, '654321');
|
|
assert.strictEqual(modifier.$set.valid, true);
|
|
}
|
|
});
|
|
|
|
test('InviteBleed: a six-digit code from before v9.96 is not usable, and a re-invite replaces it', () => {
|
|
const { isStrongInvitationCode } = require('../models/lib/invitationCodeEmail');
|
|
assert.strictEqual(isStrongInvitationCode(STRONG), true);
|
|
assert.strictEqual(isStrongInvitationCode(require('crypto').randomBytes(16).toString('base64url')), true);
|
|
for (const weak of ['123456', '999999', 'abcdefghij', '', null, 123456, `${STRONG}!`, 'x'.repeat(129)]) {
|
|
assert.strictEqual(isStrongInvitationCode(weak), false, String(weak));
|
|
}
|
|
assert.strictEqual(isInvitationCodeSendable({ code: '123456', valid: true }), false, 'a guessable code is never mailed again');
|
|
const modifier = buildReinviteModifier({ _id: 'i1', code: '123456', valid: true }, ['b'], () => STRONG);
|
|
assert.strictEqual(modifier.$set.code, STRONG);
|
|
assert.strictEqual(modifier.$set.valid, true);
|
|
});
|
|
|
|
test('a Number code in the document never matches the sign-up (string) lookup', () => {
|
|
// This is exactly why buildReinviteModifier/hasUsableCode require a string.
|
|
const doc = { code: 123456, email: 'a@b.c', valid: true };
|
|
assert.strictEqual(signupLookupMatches(doc, '123456', 'a@b.c'), false);
|
|
});
|
|
|
|
test('a mixed-case stored email never matches the lowercased sign-up address', () => {
|
|
const doc = { code: '123456', email: 'User@Example.com', valid: true };
|
|
assert.strictEqual(signupLookupMatches(doc, '123456', 'user@example.com'), false);
|
|
// ...which is why sendInvitation must store normalizeInviteEmail(rawEmail).
|
|
const stored = { ...doc, email: normalizeInviteEmail('User@Example.com') };
|
|
assert.strictEqual(signupLookupMatches(stored, '123456', 'user@example.com'), true);
|
|
});
|
|
|
|
test('#4043: SMTP failure on a RE-INVITE must NOT delete the pre-existing code', () => {
|
|
// Deleting it would invalidate the code already delivered in an earlier email.
|
|
assert.strictEqual(
|
|
shouldRemoveInvitationOnEmailFailure({ isNewInvitation: false }),
|
|
false,
|
|
);
|
|
assert.strictEqual(shouldRemoveInvitationOnEmailFailure({}), false);
|
|
assert.strictEqual(shouldRemoveInvitationOnEmailFailure(), false);
|
|
});
|
|
|
|
// --- WIRING ------------------------------------------------------------------
|
|
// The helpers only protect #4043 if server/models/settings.js actually uses
|
|
// them; guard the wiring so a refactor cannot silently regress.
|
|
|
|
test('server/models/settings.js is wired to the #4043 helpers', () => {
|
|
const src = fs.readFileSync(
|
|
path.join(__dirname, '..', 'server', 'models', 'settings.js'),
|
|
'utf8',
|
|
);
|
|
assert.ok(
|
|
src.includes("require('/models/lib/invitationCodeEmail')"),
|
|
'settings.js must require models/lib/invitationCodeEmail',
|
|
);
|
|
assert.ok(
|
|
src.includes('normalizeInviteEmail(rawEmail)'),
|
|
'sendInvitation must normalize the invitee email',
|
|
);
|
|
assert.ok(
|
|
src.includes('buildReinviteModifier(invitation, boards'),
|
|
'the re-invite branch must build its modifier via buildReinviteModifier',
|
|
);
|
|
assert.ok(
|
|
src.includes('isInvitationCodeSendable(icode)'),
|
|
'sendInvitationEmail must guard with isInvitationCodeSendable',
|
|
);
|
|
assert.ok(
|
|
src.includes('shouldRemoveInvitationOnEmailFailure({ isNewInvitation })'),
|
|
'the SMTP-failure rollback must be gated on isNewInvitation',
|
|
);
|
|
assert.ok(
|
|
src.includes("sendInvitationEmail(invitation._id, { isNewInvitation: false })"),
|
|
're-invites must be flagged as pre-existing invitations',
|
|
);
|
|
assert.ok(
|
|
!/catch \(e\) \{\s*await InvitationCodes\.removeAsync\(_id\);/.test(src),
|
|
'the unconditional remove-on-email-failure must not come back',
|
|
);
|
|
});
|
|
|
|
console.log(`\n${passed} tests passed`);
|