Files
wekan/server/lib/inboundEmailReplyToken.js
T
Lauri Ojansivu 0c7524db05 Add reply-by-email: replying to a notification email comments on the card.
WeKan only ever sent email; there was no way to receive it, so replying to a
notification email went nowhere. This adds the webhook-based half of #2414 -
not an IMAP-polling mail client, which would need a running mailbox,
credentials and a polling loop the self-hoster would have to operate. Instead
a self-hoster points a mail provider's inbound-parse webhook (Mailgun Routes,
SendGrid Inbound Parse, Postmark inbound - documented in
docs/Features/Email/Reply-By-Email.md) at a new POST /api/inbound-email.

Every outbound notification email's Reply-To now carries
reply+<cardId>-<hmac>@<domain> (server/lib/inboundEmailReplyToken.js), the
HMAC keyed by INBOUND_EMAIL_HMAC_SECRET so it cannot be forged or guessed.
The webhook verifies that token, resolves the card, strips the reply's quoted
text (server/lib/inboundEmailQuoteStrip.js), matches the sender's address to
an existing WeKan user (server/lib/inboundEmailUserMatch.js - no match is
rejected, never an anonymous comment), and inserts a CardComments document.

Unauthenticated by design, so every rejection (bad/forged token, unmatched
sender) is logged via securityLog under the new authn.inbound-email catalog
key, wrapped so logging can never break the guard.

Both INBOUND_EMAIL_HMAC_SECRET and INBOUND_EMAIL_DOMAIN are opt-in; unset
(the default), no Reply-To is added and existing installs are unchanged.

Thanks to vasyugan and xet7 !

Closes #2414,
2026-09-10 20:51:52 +03:00

99 lines
3.8 KiB
JavaScript

'use strict';
// Reply-by-email token: encode/verify a card-identifying token used as the
// local part of a notification email's Reply-To address, so a reply sent
// through a mail provider's inbound-parse webhook (server/routes/inboundEmail.js)
// can be matched back to the right card - see docs/Features/Reply-By-Email.md
// and https://github.com/wekan/wekan/issues/2414.
//
// Scope: this is the WEBHOOK-based approach (a mail provider such as Mailgun
// Routes or SendGrid Inbound Parse POSTs parsed inbound mail to a WeKan HTTP
// endpoint), not an IMAP-polling mail client - see the doc for why.
//
// Token shape: `reply+<cardId>-<hex-hmac>` (the local part of
// `reply+<cardId>-<hex-hmac>@<domain>`). The HMAC is keyed by a server-only
// secret so the token cannot be forged or guessed from a card id alone - a
// forged/tampered token must be REJECTED (see the negative test in
// tests/inboundEmailReplyToken.test.cjs). Plain Node `crypto`, no new
// dependency, the same approach as models/lib/oauth2ClientSecretJwt.js.
const crypto = require('crypto');
const TOKEN_PREFIX = 'reply+';
// crypto.timingSafeEqual requires equal-length buffers; a length mismatch is
// itself a "not equal" rather than a thrown error.
function safeEqualHex(a, b) {
if (typeof a !== 'string' || typeof b !== 'string') return false;
const bufA = Buffer.from(a, 'hex');
const bufB = Buffer.from(b, 'hex');
if (bufA.length === 0 || bufA.length !== bufB.length) return false;
return crypto.timingSafeEqual(bufA, bufB);
}
// Pure: compute the hex HMAC for a cardId under a given secret. No I/O.
function computeCardReplyHmac(cardId, secret) {
if (!cardId || !secret) return '';
return crypto
.createHmac('sha256', secret)
.update(String(cardId))
.digest('hex')
.slice(0, 32); // 128 bits is plenty for this purpose and keeps addresses short
}
// Pure: build the local part of the Reply-To address for a card, e.g.
// "reply+abc123-9f8e...". Does not include "@domain" - the caller appends
// the configured inbound-mail domain.
function buildReplyToLocalPart(cardId, secret) {
const mac = computeCardReplyHmac(cardId, secret);
if (!mac) return '';
return `${TOKEN_PREFIX}${cardId}-${mac}`;
}
// Pure: build the full Reply-To address.
function buildReplyToAddress(cardId, secret, domain) {
const local = buildReplyToLocalPart(cardId, secret);
if (!local || !domain) return '';
return `${local}@${domain}`;
}
// Pure: parse and verify a reply token (either the bare local part, e.g.
// "reply+abc123-9f8e...", or a full "local@domain" address - callers may pass
// either the raw `to`/recipient field a mail provider sends, or an address
// already split down to its local part).
//
// Returns { valid: true, cardId } when the token verifies, or
// { valid: false, reason } otherwise. Never throws.
function verifyReplyToken(token, secret) {
try {
if (!token || typeof token !== 'string' || !secret) {
return { valid: false, reason: 'missing-token-or-secret' };
}
const local = token.includes('@') ? token.split('@')[0] : token;
if (!local.startsWith(TOKEN_PREFIX)) {
return { valid: false, reason: 'bad-prefix' };
}
const rest = local.slice(TOKEN_PREFIX.length);
const lastDash = rest.lastIndexOf('-');
if (lastDash <= 0 || lastDash === rest.length - 1) {
return { valid: false, reason: 'malformed' };
}
const cardId = rest.slice(0, lastDash);
const providedMac = rest.slice(lastDash + 1);
const expectedMac = computeCardReplyHmac(cardId, secret);
if (!expectedMac || !safeEqualHex(providedMac, expectedMac)) {
return { valid: false, reason: 'bad-hmac' };
}
return { valid: true, cardId };
} catch (e) {
return { valid: false, reason: 'exception' };
}
}
module.exports = {
TOKEN_PREFIX,
computeCardReplyHmac,
buildReplyToLocalPart,
buildReplyToAddress,
verifyReplyToken,
};