mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
An email to a member's personal board address, board+<token>@<domain>, becomes a card on that board, as Trello's email-to-board does (#794, #3948). The subject is the title; the body (quoted replies stripped, HTML as text) is the description, with a From line; attachments are attached (no SVG, as for uploads). The author is the address's member, never the From address, and they must still have write access when the email arrives. A reply whose In-Reply-To/References matches a card's thread becomes a comment on it. Notification Settings, at three levels: the Admin Panel turns it on and holds the domain, the provider webhook secret and the IMAP mailboxes with their passwords (never published, never sent back to a browser); a board admin turns the board on and picks the list and swimlane; board and member choose which parts of an email are used. The member sees and replaces their own address. It arrives by POST /api/inbound-email (Mailgun, SendGrid, Postmark, JSON, forms, multipart with attachments, raw MIME), by IMAP polling with imapflow and mailparser under a database lease so one replica polls, or on Sandstorm through the grain's own address: the bridge's Maildir is read, a member's board token in the subject routes it, and WeKan's outgoing email goes through HackSessionContext.send, since a grain has no network. The email and hack-session capnp schemas are generated with the other Sandstorm bindings. Unknown addresses, wrong webhook secrets and over-limit addresses are logged under authn.inbound-email. Rate limit per address, size limits, Message-ID deduplication. The new strings are translated in every locale. Thanks to xet7 !
103 lines
3.8 KiB
JavaScript
103 lines
3.8 KiB
JavaScript
'use strict';
|
|
|
|
// A member's personal email address for one board (email to card, #794):
|
|
//
|
|
// board+<token>@<domain>
|
|
//
|
|
// token 22 characters of [A-Za-z0-9], each uniform (~131 bits)
|
|
//
|
|
// The token is kept in BoardEmailAddresses, a collection no publication
|
|
// sends: the member asks for their own address through a method, so it can be
|
|
// shown again (Trello shows it every time too), and an address is revoked by
|
|
// giving the member a new one. The card an email makes is the
|
|
// address's MEMBER's card - never the From address's, which the sender
|
|
// controls (the lesson of ReplyBleed, GHSA-mc7c-cv99-64h7) - so forwarding
|
|
// somebody else's email to your own board address works, and nobody can post
|
|
// as someone else by setting From.
|
|
//
|
|
// Pure apart from crypto, so tests/emailToCard.test.cjs runs it with
|
|
// plain Node.
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const PREFIX = 'board+';
|
|
const TOKEN = /^[A-Za-z0-9]{22}$/;
|
|
const ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
|
|
|
// Rejection sampling (RandomBleed, CWE-1204): 256 is not a multiple of 62, so
|
|
// only bytes below 248 are used - each character is then exactly uniform.
|
|
function newBoardAddressToken(random = crypto.randomBytes) {
|
|
const limit = 256 - (256 % ALPHABET.length);
|
|
let out = '';
|
|
while (out.length < 22) {
|
|
const bytes = random(22 - out.length);
|
|
for (let i = 0; i < bytes.length && out.length < 22; i += 1) {
|
|
if (bytes[i] < limit) out += ALPHABET[bytes[i] % ALPHABET.length];
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function boardAddress(token, domain) {
|
|
return TOKEN.test(token || '') && domain ? `${PREFIX}${token}@${domain}` : '';
|
|
}
|
|
|
|
// The bare address from "Name <user@host>" or "user@host".
|
|
function bareAddress(value) {
|
|
if (typeof value !== 'string') return '';
|
|
const angle = value.match(/<([^>]+)>/);
|
|
return (angle ? angle[1] : value).trim();
|
|
}
|
|
|
|
// The token of a board address, or null. The local part's case is kept: the
|
|
// token is case-sensitive, and mail systems preserve a local part's case.
|
|
function parseBoardAddress(value) {
|
|
const address = bareAddress(value);
|
|
const at = address.lastIndexOf('@');
|
|
const local = at > 0 ? address.slice(0, at) : address;
|
|
if (!local.toLowerCase().startsWith(PREFIX)) return null;
|
|
const token = local.slice(PREFIX.length);
|
|
return TOKEN.test(token) ? token : null;
|
|
}
|
|
|
|
// Every recipient a message names, in the order worth trying: the envelope
|
|
// headers a mail server adds (the address the message was actually DELIVERED
|
|
// to - a board address in Bcc appears only there) before To and Cc. Each
|
|
// entry may be "a@b, Name <c@d>" or an array of such strings / { address }.
|
|
function recipientAddresses({ deliveredTo, originalTo, to, cc } = {}) {
|
|
const out = [];
|
|
const add = value => {
|
|
if (!value) return;
|
|
if (Array.isArray(value)) { value.forEach(add); return; }
|
|
if (typeof value === 'object') { add(value.address || value.value || value.text); return; }
|
|
for (const part of String(value).split(',')) {
|
|
const address = bareAddress(part);
|
|
if (address && !out.includes(address)) out.push(address);
|
|
}
|
|
};
|
|
[deliveredTo, originalTo, to, cc].forEach(add);
|
|
return out;
|
|
}
|
|
|
|
// On Sandstorm a grain has ONE address and its whole local part is the grain
|
|
// id, so a member reaches their board by putting their token in the SUBJECT:
|
|
// "board+<token> Fix the login" or "[board+<token>] ...". Returns the token
|
|
// and the subject without it.
|
|
const SUBJECT_TOKEN = /(?:^|\s|\[)board\+([A-Za-z0-9]{22})(?:\]|\s|$)/;
|
|
function subjectBoardToken(subject) {
|
|
const text = String(subject || '');
|
|
const match = text.match(SUBJECT_TOKEN);
|
|
if (!match) return { token: null, subject: text };
|
|
return { token: match[1], subject: text.replace(match[0], ' ').replace(/\s+/g, ' ').trim() };
|
|
}
|
|
|
|
module.exports = {
|
|
subjectBoardToken,
|
|
PREFIX,
|
|
newBoardAddressToken,
|
|
boardAddress,
|
|
parseBoardAddress,
|
|
recipientAddresses,
|
|
bareAddress,
|
|
};
|