Files
wekan/server/lib/verificationEmail.js
T
Lauri Ojansivu b1b414e850 - **Fix "Internal Server Error" when signing up despite the account being created**:
Registering a new account showed a red "Internal server error" on the sign-up form even
  though the account was created and could sign in — which typically happens when SMTP is not
  configured. Root cause: useraccounts' `ATCreateUserServer` creates the account and then
  calls `Accounts.sendVerificationEmail()` (because `sendVerificationEmail: true`); when SMTP
  is missing/misconfigured that send throws **after** the user row is inserted, and with no
  try/catch the exception leaves the createUser method as an opaque HTTP 500. The verification
  email is best-effort at sign-up, so `Accounts.sendVerificationEmail` is now wrapped to log
  and swallow a transport failure — registration completes and redirects to sign-in — while an
  "already verified" error is re-thrown so the resend-verification flow still reports it. This
  mirrors the [#5706](https://github.com/wekan/wekan/issues/5706) reset-password hardening.
  Covered by `tests/verificationEmail.test.cjs`.

Thanks to Firas-Git and xet7.

Fixes #5672
2026-07-06 11:50:28 +03:00

62 lines
2.7 KiB
JavaScript

// verificationEmail.js
//
// Pure, Meteor-free helper hardening the sign-up verification-email send.
// Extracted so it can be unit-tested in plain Node (see
// tests/verificationEmail.test.cjs).
//
// #5672: "Internal Server Error When Signing Up Despite Successful Account
// Creation". useraccounts' ATCreateUserServer method creates the account and
// THEN calls `Accounts.sendVerificationEmail(userId, email)`. When SMTP is not
// configured (no/invalid MAIL_URL / MAIL_FROM, or the mail server is
// unreachable), that send throws AFTER the user row is already inserted, and the
// exception propagates out of the createUser method as an opaque HTTP 500 — so
// the user sees "Internal server error" even though the account was created and
// can sign in.
//
// The verification email is best-effort at sign-up: the account already exists,
// so a mail failure must not fail the registration. Wrap the sender so a send
// failure is logged and swallowed (registration completes and redirects to
// sign-in). An "already verified" error is re-thrown unchanged so the separate
// "resend verification email" flow (ATResendVerificationEmail) can still report
// it.
// True when the error is accounts-password signalling that the address is
// already verified (there is nothing to send), as opposed to a mail-transport
// failure. Kept broad but conservative: only the word "verified" qualifies.
function isAlreadyVerifiedError(error) {
const message = ((error && (error.reason || error.message)) || '')
.toString()
.toLowerCase();
return message.includes('verified');
}
// Wrap an Accounts.sendVerificationEmail-style sender so a transport failure at
// sign-up is logged via `logWarning` and swallowed (resolves to undefined)
// instead of bubbling up as an HTTP 500. Already-verified errors are re-thrown.
// A non-function `originalSend` is returned unchanged.
function wrapSendVerificationEmail(originalSend, logWarning) {
if (typeof originalSend !== 'function') {
return originalSend;
}
const warn = typeof logWarning === 'function' ? logWarning : () => {};
return async function wrappedSendVerificationEmail(...args) {
try {
return await originalSend.apply(this, args);
} catch (error) {
// Preserve the "resend to an already-verified address" signal.
if (isAlreadyVerifiedError(error)) {
throw error;
}
warn(
'[sendVerificationEmail] Could not send the verification email — is ' +
'SMTP configured (MAIL_URL / MAIL_FROM)? The account was still ' +
'created and can sign in. ' +
((error && (error.message || error.reason)) || error),
);
return undefined;
}
};
}
module.exports = { isAlreadyVerifiedError, wrapSendVerificationEmail };