Files
wekan/models/lib/invitationCodeEmail.js
T
Lauri Ojansivu 4fad2a9000 Fix InviteBleed's remainder: six-digit invitation codes and the unthrottled sign-up method.
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 !
2026-11-20 07:48:22 +02:00

100 lines
4.9 KiB
JavaScript

'use strict';
// Pure helpers for the "invite by email" flow (Meteor method `sendInvitation`
// in server/models/settings.js). Extracted so the rules that guarantee "an
// invitation email always carries a code that will validate at sign-up" are
// unit-testable without Meteor.
//
// #4043 ("Sometimes user get board invitation by mail without invitation
// code"): recipients received an invitation email whose code the sign-up form
// then rejected with "The invitation code doesn't exist". Sign-up only accepts
// a code whose stored document matches { code, email, valid: true }
// (server/models/users.js onCreateUser), so the SENDING side must guarantee:
// 1. the stored email is lowercase — the sign-up form lowercases the typed
// address and MongoDB string matching is case sensitive, so a mixed-case
// invitation document can never match (normalizeInviteEmail);
// 2. a RE-INVITE never re-sends a stale code: when the stored invitation is
// no longer valid (e.g. consumed and left with valid:false) or has no
// usable code, a fresh code must overwrite the old one with `valid`
// restored to true (buildReinviteModifier) — this is the fix proposed in
// the #4043 thread ("send a new invitation code that overwrites the last
// one");
// 3. an email is only sent for an invitation document that exists, has a
// non-empty string code, and is still valid (isInvitationCodeSendable) —
// otherwise fail loudly instead of mailing a dead code;
// 4. a failed SMTP send may only roll back (delete) an invitation code that
// was created by this very invite; deleting a PRE-EXISTING document would
// silently invalidate the code already delivered in an earlier email
// (shouldRemoveInvitationOnEmailFailure).
// Lowercase + trim an invitee address so the stored invitation email always
// matches the (lowercased) address the user signs up with. Non-strings
// normalize to '' (callers skip empty results).
function normalizeInviteEmail(email) {
if (typeof email !== 'string') {
return '';
}
return email.trim().toLowerCase();
}
// InviteBleed (GHSA-rg85-qx5c-qx67): an invitation code is the 128-bit value
// server/models/settings.js generateInvitationCode makes -
// crypto.randomBytes(16).toString('base64url'), 22 characters. Until v9.96 a
// code was six digits from Math.random(), and such an invitation, made before
// the upgrade and not used yet, is still in the database as valid: with a
// keyspace of 900,000 it can be guessed. So a code shorter than 22 base64url
// characters is not a code: sign-up refuses it (server/models/users.js), and a
// re-invite replaces it with a strong one instead of mailing it again.
const STRONG_INVITATION_CODE = /^[A-Za-z0-9_-]{22,128}$/;
function isStrongInvitationCode(code) {
return typeof code === 'string' && STRONG_INVITATION_CODE.test(code);
}
// True when `code` is a strong string code — the only shape the sign-up lookup
// { code: <string from the form>, valid: true } can ever match, and accept.
// Legacy or hand-inserted documents may carry a Number code, which MongoDB will
// never match against the form's string, or a six-digit one, which sign-up
// refuses (isStrongInvitationCode).
function hasUsableCode(invitation) {
return Boolean(invitation && isStrongInvitationCode(invitation.code));
}
// An invitation email may only be sent for a document that exists, carries a
// usable code, and has not been invalidated (valid === false). A missing
// `valid` field (legacy documents predating the field's defaultValue) counts
// as valid.
function isInvitationCodeSendable(invitation) {
return hasUsableCode(invitation) && invitation.valid !== false;
}
// Build the Mongo modifier for re-inviting an email that already has an
// invitation document. Always refreshes boardsToBeInvited; additionally
// overwrites the code (and restores valid:true) when the stored one could
// never validate at sign-up — stale (valid:false), missing, empty, or not a
// string. `generateCode` supplies the new code (coerced to String so it
// matches the form input type regardless of the generator).
function buildReinviteModifier(invitation, boards, generateCode) {
const $set = { boardsToBeInvited: boards };
if (!invitation || invitation.valid === false || !hasUsableCode(invitation)) {
$set.code = String(generateCode());
$set.valid = true;
}
return { $set };
}
// After an SMTP failure, only an invitation created by this very invite call
// may be deleted (nothing was ever delivered for it). A pre-existing document
// must survive, because its code was already delivered in an earlier email.
function shouldRemoveInvitationOnEmailFailure({ isNewInvitation } = {}) {
return isNewInvitation === true;
}
module.exports = {
isStrongInvitationCode,
normalizeInviteEmail,
hasUsableCode,
isInvitationCodeSendable,
buildReinviteModifier,
shouldRemoveInvitationOnEmailFailure,
};