Files
wekan/server/lib/inboundEmailUserMatch.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

37 lines
1.4 KiB
JavaScript

'use strict';
// Match an inbound reply's From address to an existing WeKan user - used by
// server/routes/inboundEmail.js (#2414). No match => the caller must reject
// the reply rather than create an anonymous/unauthenticated comment (see the
// route and its negative test).
//
// Split into a pure matcher (testable with a plain array, no database) and a
// thin server-side wrapper that reads the real Users collection.
function normalizeEmail(address) {
if (typeof address !== 'string') return '';
// A From header can be "Name <addr@x>" as well as a bare address.
const angleMatch = address.match(/<([^>]+)>/);
const raw = angleMatch ? angleMatch[1] : address;
return raw.trim().toLowerCase();
}
// Pure: given a From address and a list of { _id, emails: [{ address }] }
// user docs (the shape ReactiveCache.getUsers() / Meteor.users documents
// use), return the matching user doc or null. Never throws.
function matchSenderToUser(fromAddress, users) {
const email = normalizeEmail(fromAddress);
if (!email || !Array.isArray(users)) return null;
for (const user of users) {
const addrs = (user && Array.isArray(user.emails)) ? user.emails : [];
for (const e of addrs) {
if (e && typeof e.address === 'string' && e.address.toLowerCase() === email) {
return user;
}
}
}
return null;
}
module.exports = { normalizeEmail, matchSenderToUser };