|
@@ -0,0 +1,203 @@
|
|
|
|
|
+// Exercises ctx.mail through the DEPLOYED runner against a real mail provider.
|
|
|
|
|
+//
|
|
|
|
|
+// Credentials are NEVER in this source: userSecret/passwordSecret name project
|
|
|
|
|
+// secrets that the host resolves, so nothing here or in an echoed options
|
|
|
|
|
+// object can leak them.
|
|
|
|
|
+//
|
|
|
|
|
+// timeoutMs is raised to the HTTP ceiling because the default wall budget is
|
|
|
|
|
+// 2 s and a TLS IMAP session plus a LOGIN does not fit in that.
|
|
|
|
|
+export const on = {
|
|
|
|
|
+ id: 'mailprobe',
|
|
|
|
|
+ type: 'http',
|
|
|
|
|
+ route: '/mail/:op',
|
|
|
|
|
+ method: ['GET', 'POST'],
|
|
|
|
|
+ timeoutMs: 15000,
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+const IMAP = {
|
|
|
|
|
+ host: 'mail.onmail.hu',
|
|
|
|
|
+ port: 993,
|
|
|
|
|
+ tls: true,
|
|
|
|
|
+ user: 'nbot@fsociety.hu',
|
|
|
|
|
+ passwordSecret: 'MAIL_IMAP_PASSWORD',
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+// 2525 with STARTTLS. NOT 587: DigitalOcean blocks outbound 25, 465 and 587
|
|
|
|
|
+// from this droplet (confirmed as the host's policy, since gmail and sendgrid
|
|
|
|
|
+// are blocked on 587 too), while 2525 is open. tls and startTls are mutually
|
|
|
|
|
+// exclusive, so tls must be false here; plaintext is still refused off
|
|
|
|
|
+// loopback, which startTls satisfies.
|
|
|
|
|
+const SMTP = {
|
|
|
|
|
+ host: 'mail.onmail.hu',
|
|
|
|
|
+ port: 2525,
|
|
|
|
|
+ tls: false,
|
|
|
|
|
+ startTls: true,
|
|
|
|
|
+ user: 'nbot@fsociety.hu',
|
|
|
|
|
+ passwordSecret: 'MAIL_SMTP_PASSWORD',
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+export default async function (ctx) {
|
|
|
|
|
+ const op = (ctx.params && ctx.params.op) || 'mailboxes';
|
|
|
|
|
+ const started = Date.now();
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (op === 'mailboxes') {
|
|
|
|
|
+ const boxes = await ctx.mail.mailboxes(IMAP);
|
|
|
|
|
+ return {
|
|
|
|
|
+ op, ok: true, ms: Date.now() - started,
|
|
|
|
|
+ count: boxes.length,
|
|
|
|
|
+ // nameEncoding proves the modified-UTF-7 decode ran.
|
|
|
|
|
+ boxes: boxes.map(b => ({ name: b.name, nameEncoding: b.nameEncoding, delimiter: b.delimiter })),
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (op === 'read') {
|
|
|
|
|
+ const r = await ctx.mail.read({ ...IMAP, mailbox: 'INBOX', criteria: 'ALL', limit: 3 });
|
|
|
|
|
+ return {
|
|
|
|
|
+ op, ok: true, ms: Date.now() - started,
|
|
|
|
|
+ matched: r.matched,
|
|
|
|
|
+ returned: r.messages.length,
|
|
|
|
|
+ messages: r.messages.map(m => ({
|
|
|
|
|
+ uid: m.uid, flags: m.flags, size: m.size,
|
|
|
|
|
+ internal_date: m.internal_date,
|
|
|
|
|
+ headerEncoding: m.headerEncoding, textEncoding: m.textEncoding,
|
|
|
|
|
+ body_sections: m.body_sections,
|
|
|
|
|
+ // A SLICE only: the point is that bytes arrived, not to echo mail.
|
|
|
|
|
+ header_head: (m.header || '').slice(0, 120),
|
|
|
|
|
+ text_head: (m.text || '').slice(0, 80),
|
|
|
|
|
+ })),
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (op === 'small') {
|
|
|
|
|
+ // The documented remedy for mail_too_large: ask for less. A plain
|
|
|
|
|
+ // read of this mailbox exceeds the 4 MiB per-run cap, so bound it by
|
|
|
|
|
+ // message size rather than by count - limit alone cannot help, since
|
|
|
|
|
+ // one big message is enough to blow the cap.
|
|
|
|
|
+ const r = await ctx.mail.read({
|
|
|
|
|
+ ...IMAP, criteria: { smallerThanBytes: 60000 }, limit: 3,
|
|
|
|
|
+ });
|
|
|
|
|
+ return {
|
|
|
|
|
+ op, ok: true, ms: Date.now() - started,
|
|
|
|
|
+ matched: r.matched, returned: r.messages.length,
|
|
|
|
|
+ messages: r.messages.map(m => ({
|
|
|
|
|
+ uid: m.uid, flags: m.flags, size: m.size,
|
|
|
|
|
+ headerEncoding: m.headerEncoding, textEncoding: m.textEncoding,
|
|
|
|
|
+ body_sections: m.body_sections,
|
|
|
|
|
+ subject: ((m.header || '').match(/^Subject:\s*(.*)$/mi) || [])[1] || null,
|
|
|
|
|
+ text_head: (m.text || '').slice(0, 60),
|
|
|
|
|
+ })),
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (op === 'unseen') {
|
|
|
|
|
+ // A structured criterion, which is the only form accepted.
|
|
|
|
|
+ const r = await ctx.mail.read({ ...IMAP, criteria: { unseen: true }, limit: 5 });
|
|
|
|
|
+ return { op, ok: true, ms: Date.now() - started, matched: r.matched, uids: r.messages.map(m => m.uid) };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (op === 'accents') {
|
|
|
|
|
+ // MUST be refused by field name rather than sent. A pass here is a
|
|
|
|
|
+ // silently wrong answer, not a feature.
|
|
|
|
|
+ const r = await ctx.mail.read({ ...IMAP, criteria: { subject: 'árvíztűrő' }, limit: 1 });
|
|
|
|
|
+ return { op, ok: true, unexpected: true, matched: r.matched };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (op === 'send') {
|
|
|
|
|
+ const stamp = String(Date.now());
|
|
|
|
|
+ const r = await ctx.mail.send({
|
|
|
|
|
+ ...SMTP,
|
|
|
|
|
+ from: 'Kővágó Örs <nbot@fsociety.hu>',
|
|
|
|
|
+ to: 'test001@fsociety.hu',
|
|
|
|
|
+ subject: 'ctx.mail prod proba ' + stamp + ' - árvíztűrő tükörfúrógép',
|
|
|
|
|
+ text: 'Ez a level a telepitett runnerbol ment. stamp=' + stamp +
|
|
|
|
|
+ '\n.\nEgy maganyos pont a fenti sorban.\n',
|
|
|
|
|
+ html: '<p>Ez a levél a <b>telepített runnerből</b> ment.</p>',
|
|
|
|
|
+ attachments: [{ filename: 'árvíztűrő.txt', contentType: 'text/plain', base64: 'w6FydsOtenT9csWR' }],
|
|
|
|
|
+ });
|
|
|
|
|
+ return { op, ok: true, ms: Date.now() - started, accepted: r.accepted };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (op === 'roundtrip') {
|
|
|
|
|
+ // The strongest end-to-end check available: send to the mailbox we can
|
|
|
|
|
+ // READ, then fetch it back and assert on the bytes. Sent to nbot@ rather
|
|
|
|
|
+ // than test001@ precisely because test001@ routes to a human's box we
|
|
|
|
|
+ // cannot open.
|
|
|
|
|
+ const stamp = String(Date.now());
|
|
|
|
|
+ const subject = 'svapi roundtrip ' + stamp + ' - árvíztűrő tükörfúrógép';
|
|
|
|
|
+ const sent = await ctx.mail.send({
|
|
|
|
|
+ ...SMTP,
|
|
|
|
|
+ from: 'Kővágó Örs <nbot@fsociety.hu>',
|
|
|
|
|
+ to: 'nbot@fsociety.hu',
|
|
|
|
|
+ subject,
|
|
|
|
|
+ // A lone '.' on its own line: dot-stuffing is curl's job, and if we
|
|
|
|
|
+ // also stuffed it the body would be corrupt; if neither did, the
|
|
|
|
|
+ // message would be TRUNCATED here.
|
|
|
|
|
+ text: 'stamp=' + stamp + '\n.\nA fenti sorban egy magányos pont áll.\n',
|
|
|
|
|
+ html: '<p>stamp=' + stamp + ' <b>árvíztűrő</b></p>',
|
|
|
|
|
+ attachments: [{ filename: 'árvíztűrő.txt', contentType: 'text/plain', base64: 'w6FydsOtenT9csWR' }],
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // Give the server a moment to file it, then find it by an ASCII-safe
|
|
|
|
|
+ // criterion - the accented subject CANNOT be searched for, by design.
|
|
|
|
|
+ let found = null;
|
|
|
|
|
+ for (let attempt = 0; attempt < 6 && !found; attempt++) {
|
|
|
|
|
+ const r = await ctx.mail.read({
|
|
|
|
|
+ ...IMAP, criteria: { subject: 'svapi roundtrip ' + stamp }, limit: 1,
|
|
|
|
|
+ });
|
|
|
|
|
+ if (r.messages.length > 0) found = r.messages[0];
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!found) {
|
|
|
|
|
+ return { op, ok: false, sent: sent.accepted, stamp, error: 'sent but not found on read-back' };
|
|
|
|
|
+ }
|
|
|
|
|
+ const header = found.header || '';
|
|
|
|
|
+ const text = found.text || '';
|
|
|
|
|
+ return {
|
|
|
|
|
+ op, ok: true, ms: Date.now() - started, stamp,
|
|
|
|
|
+ sent: sent.accepted,
|
|
|
|
|
+ uid: found.uid,
|
|
|
|
|
+ flags: found.flags,
|
|
|
|
|
+ checks: {
|
|
|
|
|
+ // The subject must arrive as RFC 2047 encoded words, not raw bytes.
|
|
|
|
|
+ subject_is_encoded_word: /Subject:.*=\?UTF-8\?/i.test(header),
|
|
|
|
|
+ subject_raw: (header.match(/^Subject:\s*(.*)$/mi) || [])[1] || null,
|
|
|
|
|
+ // The lone dot survived and was not doubled.
|
|
|
|
|
+ lone_dot_intact: /\n\.\r?\n|\r\n\.\r\n/.test(text),
|
|
|
|
|
+ double_stuffed: text.indexOf('\n..') !== -1,
|
|
|
|
|
+ // RFC 2231 for the accented filename.
|
|
|
|
|
+ rfc2231_filename: /filename\*=|filename\*0\*=/i.test(text),
|
|
|
|
|
+ quoted_printable: /Content-Transfer-Encoding:\s*quoted-printable/i.test(text),
|
|
|
|
|
+ multipart_alternative: /multipart\/alternative/i.test(text),
|
|
|
|
|
+ multipart_mixed: /multipart\/mixed/i.test(text),
|
|
|
|
|
+ // Quoted-printable encodes '=' as '=3D', so the literal 'stamp='
|
|
|
|
|
+ // is NOT in the transferred body - looking for it was a naive check.
|
|
|
|
|
+ stamp_in_body_qp: text.indexOf('stamp=3D' + stamp) !== -1,
|
|
|
|
|
+ // multipart/mixed is declared in the HEADER (it is the top-level
|
|
|
|
|
+ // Content-Type); only the inner alternative boundary shows in TEXT.
|
|
|
|
|
+ mixed_in_header: /multipart\/mixed/i.test(header),
|
|
|
|
|
+ },
|
|
|
|
|
+ headerEncoding: found.headerEncoding,
|
|
|
|
|
+ // The exact bytes around the Subject, escaped, so a fold is visible.
|
|
|
|
|
+ subject_region: (function () {
|
|
|
|
|
+ const i = header.search(/^Subject:/mi);
|
|
|
|
|
+ if (i < 0) return null;
|
|
|
|
|
+ return header.slice(i, i + 120).replace(/\r/g, '<CR>').replace(/\n/g, '<LF>');
|
|
|
|
|
+ })(),
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return { op, ok: false, error: 'unknown op; use mailboxes|read|small|unseen|accents|send' };
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ // The shape a consumer branches on: slug, status and retryable.
|
|
|
|
|
+ // NOT a key called `status`: a returned object carrying status, headers or
|
|
|
|
|
+ // body is read as a response ENVELOPE, and an envelope may hold nothing
|
|
|
|
|
+ // else - so a diagnostic field named `status` turns into a 500
|
|
|
|
|
+ // invalid_return and hides the very error it was meant to report.
|
|
|
|
|
+ return {
|
|
|
|
|
+ op, ok: false, ms: Date.now() - started,
|
|
|
|
|
+ code: e.code || null,
|
|
|
|
|
+ httpStatus: e.status || null,
|
|
|
|
|
+ retryable: (e.details && e.details.retryable) ?? null,
|
|
|
|
|
+ message: String(e.message || e).slice(0, 400),
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+}
|