Files
wekan/tests/inboundEmailUserMatch.test.cjs
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

61 lines
2.4 KiB
JavaScript

'use strict';
// Plain-Node unit test (no Meteor) for #2414 reply-by-email's sender-to-user
// matching: server/lib/inboundEmailUserMatch.js.
// Run: node tests/inboundEmailUserMatch.test.cjs
//
// The inbound webhook must NEVER create an anonymous/unauthenticated comment:
// a From address that matches no WeKan user is rejected, not attributed to
// nobody. This pins the matcher in isolation, with a plain array standing in
// for the Users collection.
const assert = require('assert');
const { normalizeEmail, matchSenderToUser } = require('../server/lib/inboundEmailUserMatch.js');
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
const USERS = [
{ _id: 'user1', emails: [{ address: 'alice@example.com' }] },
{ _id: 'user2', emails: [{ address: 'Bob@Example.com' }, { address: 'bob2@example.com' }] },
{ _id: 'user3', emails: [] },
];
test('matches a bare address, case-insensitively', () => {
assert.strictEqual(matchSenderToUser('ALICE@EXAMPLE.COM', USERS)._id, 'user1');
});
test('matches a "Name <addr>" style From header', () => {
assert.strictEqual(matchSenderToUser('Alice Example <alice@example.com>', USERS)._id, 'user1');
});
test('matches a user with multiple email addresses on any of them', () => {
assert.strictEqual(matchSenderToUser('bob2@example.com', USERS)._id, 'user2');
});
test('a user email stored mixed-case still matches a lowercase From', () => {
assert.strictEqual(matchSenderToUser('bob@example.com', USERS)._id, 'user2');
});
test('NEGATIVE: an unknown sender address matches no user (must be rejected, not anonymous)', () => {
assert.strictEqual(matchSenderToUser('mallory@evil.example', USERS), null);
});
test('NEGATIVE: empty/missing From matches nobody', () => {
assert.strictEqual(matchSenderToUser('', USERS), null);
assert.strictEqual(matchSenderToUser(null, USERS), null);
assert.strictEqual(matchSenderToUser(undefined, USERS), null);
});
test('NEGATIVE: a user with no email addresses can never match', () => {
assert.strictEqual(matchSenderToUser('user3@example.com', USERS), null);
});
test('normalizeEmail extracts and lowercases the address', () => {
assert.strictEqual(normalizeEmail('Alice <ALICE@Example.com>'), 'alice@example.com');
assert.strictEqual(normalizeEmail(' alice@example.com '), 'alice@example.com');
assert.strictEqual(normalizeEmail(42), '');
});
console.log(`inboundEmailUserMatch: ${passed} passed`);