Files
wekan/tests/inboundEmailReplyToken.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

97 lines
3.9 KiB
JavaScript

'use strict';
// Plain-Node unit test (no Meteor) for #2414 reply-by-email's HMAC reply
// token: server/lib/inboundEmailReplyToken.js.
// Run: node tests/inboundEmailReplyToken.test.cjs
//
// The token is the ONLY guard on the unauthenticated inbound-email webhook
// (server/routes/inboundEmail.js), so this pins: a valid token round-trips to
// the right cardId, and a forged/tampered/malformed/wrong-secret token is
// rejected outright (the negative tests).
const assert = require('assert');
const {
buildReplyToLocalPart,
buildReplyToAddress,
verifyReplyToken,
computeCardReplyHmac,
} = require('../server/lib/inboundEmailReplyToken.js');
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
const SECRET = 'test-secret-do-not-use-in-production';
const CARD_ID = 'abc123CardId';
test('a valid token round-trips to the same cardId', () => {
const local = buildReplyToLocalPart(CARD_ID, SECRET);
assert.ok(local.startsWith('reply+'));
const result = verifyReplyToken(local, SECRET);
assert.strictEqual(result.valid, true);
assert.strictEqual(result.cardId, CARD_ID);
});
test('a full address (local@domain) also verifies', () => {
const address = buildReplyToAddress(CARD_ID, SECRET, 'reply.example.com');
assert.strictEqual(address, `${buildReplyToLocalPart(CARD_ID, SECRET)}@reply.example.com`);
const result = verifyReplyToken(address, SECRET);
assert.strictEqual(result.valid, true);
assert.strictEqual(result.cardId, CARD_ID);
});
test('a different cardId produces a different token', () => {
const t1 = buildReplyToLocalPart('card-one', SECRET);
const t2 = buildReplyToLocalPart('card-two', SECRET);
assert.notStrictEqual(t1, t2);
});
// --- negative tests: a forged/tampered token must be rejected ---
test('NEGATIVE: a tampered HMAC is rejected', () => {
const local = buildReplyToLocalPart(CARD_ID, SECRET);
const tampered = local.slice(0, -1) + (local.slice(-1) === '0' ? '1' : '0');
const result = verifyReplyToken(tampered, SECRET);
assert.strictEqual(result.valid, false);
});
test('NEGATIVE: a token for one cardId cannot be reused for another', () => {
// Forging by swapping the cardId in an otherwise-valid token, keeping the
// original (now-mismatched) HMAC - the classic "change the id, keep the
// signature" forgery attempt.
const local = buildReplyToLocalPart('card-one', SECRET);
const mac = local.slice(local.lastIndexOf('-') + 1);
const forged = `reply+card-two-${mac}`;
const result = verifyReplyToken(forged, SECRET);
assert.strictEqual(result.valid, false);
});
test('NEGATIVE: a token verified against the wrong secret is rejected', () => {
const local = buildReplyToLocalPart(CARD_ID, SECRET);
const result = verifyReplyToken(local, 'a-completely-different-secret');
assert.strictEqual(result.valid, false);
});
test('NEGATIVE: a guessed/made-up token (no real HMAC) is rejected', () => {
const result = verifyReplyToken(`reply+${CARD_ID}-0000000000000000`, SECRET);
assert.strictEqual(result.valid, false);
});
test('NEGATIVE: malformed input (no prefix, empty, non-string) is rejected', () => {
assert.strictEqual(verifyReplyToken('not-a-reply-token@x.com', SECRET).valid, false);
assert.strictEqual(verifyReplyToken('', SECRET).valid, false);
assert.strictEqual(verifyReplyToken(null, SECRET).valid, false);
assert.strictEqual(verifyReplyToken(undefined, SECRET).valid, false);
assert.strictEqual(verifyReplyToken('reply+', SECRET).valid, false);
assert.strictEqual(verifyReplyToken('reply+noHyphenHere', SECRET).valid, false);
});
test('NEGATIVE: no secret configured never validates (feature stays off)', () => {
const local = buildReplyToLocalPart(CARD_ID, SECRET);
assert.strictEqual(verifyReplyToken(local, '').valid, false);
assert.strictEqual(buildReplyToLocalPart(CARD_ID, ''), '');
assert.strictEqual(computeCardReplyHmac(CARD_ID, ''), '');
});
console.log(`inboundEmailReplyToken: ${passed} passed`);