mail-access.test.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. import assert from 'node:assert/strict';
  2. import { mkdtempSync } from 'node:fs';
  3. import net from 'node:net';
  4. import { tmpdir } from 'node:os';
  5. import path from 'node:path';
  6. import { test } from 'node:test';
  7. import {
  8. createDomain,
  9. createInboundFolder,
  10. createInboundMailbox,
  11. createInboundMessage,
  12. createImportedInboundMessage,
  13. createUser,
  14. getInboundMessage,
  15. inboundFolderExists,
  16. initDatabase,
  17. listInboundMessages
  18. } from '../src/db.js';
  19. import { startMailboxAccessServers } from '../src/mail-access.js';
  20. test('IMAP SELECT keeps message bodies lazy and FETCH hydrates one message', async () => {
  21. const database = initDatabase(mkdtempSync(path.join(tmpdir(), 'mailhub-imap-test-')), 'mail-access-secret');
  22. const { user, mailbox } = createMailboxFixture('imap.example', 'imap-user');
  23. const storedMessage = createInboundMessage(mailbox, {
  24. sender: 'alice@example.net',
  25. recipients: ['admin@imap.example'],
  26. subject: 'IMAP hello',
  27. messageId: '<imap-hello@example.net>',
  28. rawMessage: [
  29. 'From: Alice <alice@example.net>',
  30. 'To: admin@imap.example',
  31. 'Subject: IMAP hello',
  32. 'Message-ID: <imap-hello@example.net>',
  33. '',
  34. 'Hello through IMAP.'
  35. ].join('\r\n'),
  36. textBody: 'Hello through IMAP.'
  37. });
  38. const [server] = startMailboxAccessServers({
  39. hostname: 'mail.imap.example',
  40. imapEnabled: true,
  41. imapListeners: [{ port: 0, protocol: 'imap' }],
  42. pop3Enabled: false,
  43. pop3Listeners: [],
  44. allowInsecureAuth: true
  45. });
  46. await waitForListening(server);
  47. try {
  48. const port = server.address().port;
  49. const client = await connectClient(port);
  50. await client.readUntil(/\* OK .* IMAP ready\r\n/);
  51. assert.match(await client.command('A1 LOGIN "admin@imap.example" "mailbox-pass-123"', /A1 OK/), /LOGIN completed/);
  52. const selected = await client.command('A2 SELECT INBOX', /A2 OK/);
  53. assert.match(selected, /\* 1 EXISTS/);
  54. database
  55. .prepare('UPDATE inbound_messages SET raw_message_bytes = ? WHERE id = ?')
  56. .run(Buffer.from(storedMessage.rawMessage.replace('Hello through IMAP.', 'Hallo through IMAP.'), 'utf8'), storedMessage.id);
  57. const fetched = await client.command('A3 UID FETCH 1:* (UID FLAGS RFC822.SIZE BODY.PEEK[])', /A3 OK/);
  58. assert.match(fetched, /\* 1 FETCH/);
  59. assert.match(fetched, /UID 1/);
  60. assert.match(fetched, /Subject: IMAP hello/);
  61. assert.match(fetched, /Hallo through IMAP\./);
  62. assert.doesNotMatch(fetched, /Hello through IMAP\./);
  63. await client.command('A4 LOGOUT', /A4 OK/);
  64. client.close();
  65. assert.equal(listInboundMessages(user.id).length, 1);
  66. } finally {
  67. await closeServer(server);
  68. }
  69. });
  70. test('IMAP exposes imported Maildir flags and Dovecot keywords', async () => {
  71. initDatabase(mkdtempSync(path.join(tmpdir(), 'mailhub-imap-flags-test-')), 'mail-access-secret');
  72. const { mailbox } = createMailboxFixture('flags.example', 'flags-user');
  73. createImportedInboundMessage(mailbox, {
  74. importSource: 'vesta:flags',
  75. sourceKey: 'message-1',
  76. sender: 'sender@example.net',
  77. recipients: ['admin@flags.example'],
  78. subject: 'Imported flags',
  79. messageId: '<flags@example.net>',
  80. rawMessageBytes: Buffer.from('From: sender@example.net\r\nTo: admin@flags.example\r\nSubject: Imported flags\r\n\r\nBody', 'utf8'),
  81. flags: ['\\Answered', '\\Flagged', '\\Draft', '\\Seen'],
  82. keywords: ['$Label1', 'custom-keyword'],
  83. receivedAt: '2024-01-02T03:04:05.000Z'
  84. });
  85. const [server] = startMailboxAccessServers({
  86. hostname: 'mail.flags.example',
  87. imapEnabled: true,
  88. imapListeners: [{ port: 0, protocol: 'imap' }],
  89. pop3Enabled: false,
  90. pop3Listeners: [],
  91. allowInsecureAuth: true
  92. });
  93. await waitForListening(server);
  94. let client;
  95. try {
  96. client = await connectClient(server.address().port);
  97. await client.readUntil(/\* OK .* IMAP ready\r\n/);
  98. await client.command('A1 LOGIN "admin@flags.example" "mailbox-pass-123"', /A1 OK/);
  99. const selected = await client.command('A2 SELECT INBOX', /A2 OK/);
  100. assert.match(selected, /\* FLAGS \([^\r\n]*\\Answered/);
  101. assert.match(selected, /\* FLAGS \([^\r\n]*\$Label1/);
  102. assert.match(selected, /\* FLAGS \([^\r\n]*custom-keyword/);
  103. const fetched = await client.command('A3 UID FETCH 1:* (UID FLAGS)', /A3 OK/);
  104. for (const flag of ['\\Answered', '\\Flagged', '\\Draft', '\\Seen', '$Label1', 'custom-keyword']) {
  105. assert.ok(fetched.includes(flag));
  106. }
  107. await client.command('A4 LOGOUT', /A4 OK/);
  108. } finally {
  109. client?.close();
  110. await closeServer(server);
  111. }
  112. });
  113. test('IMAP exposes MIME body structures and individual parts for Roundcube', async () => {
  114. initDatabase(mkdtempSync(path.join(tmpdir(), 'mailhub-imap-mime-test-')), 'mail-access-secret');
  115. const { mailbox } = createMailboxFixture('mime.example', 'mime-user');
  116. createInboundMessage(mailbox, {
  117. sender: 'alice@example.net',
  118. recipients: ['admin@mime.example'],
  119. subject: 'MIME message',
  120. messageId: '<mime-message@example.net>',
  121. rawMessage: [
  122. 'From: Alice <alice@example.net>',
  123. 'To: admin@mime.example',
  124. 'Subject: MIME message',
  125. 'MIME-Version: 1.0',
  126. 'Content-Type: multipart/alternative; boundary="mailhub-boundary"',
  127. '',
  128. '--mailhub-boundary',
  129. 'Content-Type: text/plain; charset=UTF-8',
  130. 'Content-Transfer-Encoding: quoted-printable',
  131. '',
  132. 'Plain message body.',
  133. '--mailhub-boundary',
  134. 'Content-Type: text/html; charset=UTF-8',
  135. '',
  136. '<p>HTML message body.</p>',
  137. '--mailhub-boundary--',
  138. ''
  139. ].join('\r\n'),
  140. textBody: 'Plain message body.',
  141. htmlBody: '<p>HTML message body.</p>'
  142. });
  143. const [server] = startMailboxAccessServers({
  144. hostname: 'mail.mime.example',
  145. imapEnabled: true,
  146. imapListeners: [{ port: 0, protocol: 'imap' }],
  147. pop3Enabled: false,
  148. pop3Listeners: [],
  149. allowInsecureAuth: true
  150. });
  151. await waitForListening(server);
  152. let client;
  153. try {
  154. client = await connectClient(server.address().port);
  155. await client.readUntil(/\* OK .* IMAP ready\r\n/);
  156. assert.match(await client.command('A1 LOGIN "admin@mime.example" "mailbox-pass-123"', /A1 OK/), /LOGIN completed/);
  157. await client.command('A2 SELECT INBOX', /A2 OK/);
  158. const structure = await client.command('A3 UID FETCH 1 (UID BODYSTRUCTURE)', /A3 OK/);
  159. assert.match(structure, /BODYSTRUCTURE \(\("TEXT" "PLAIN" \("CHARSET" "UTF-8"\).*\) \("TEXT" "HTML" \("CHARSET" "UTF-8"\).*\) "ALTERNATIVE" \("BOUNDARY" "mailhub-boundary"\)\)/);
  160. const textPart = await client.command('A4 UID FETCH 1 (BODY.PEEK[1])', /A4 OK/);
  161. assert.match(textPart, /BODY\[1\] \{\d+\}\r\nPlain message body\./);
  162. assert.doesNotMatch(textPart, /Content-Type: text\/plain/);
  163. const htmlPart = await client.command('A5 UID FETCH 1 (BODY.PEEK[2])', /A5 OK/);
  164. assert.match(htmlPart, /BODY\[2\] \{\d+\}\r\n<p>HTML message body\.<\/p>/);
  165. const mimeHeaders = await client.command('A6 UID FETCH 1 (BODY.PEEK[1.MIME])', /A6 OK/);
  166. assert.match(mimeHeaders, /BODY\[1\.MIME\] \{\d+\}\r\nContent-Type: text\/plain; charset=UTF-8/);
  167. await client.command('A7 LOGOUT', /A7 OK/);
  168. client.close();
  169. } finally {
  170. client?.close();
  171. await closeServer(server);
  172. }
  173. });
  174. test('IMAP exposes LF-only Maildir headers and text sections to Roundcube', async () => {
  175. initDatabase(mkdtempSync(path.join(tmpdir(), 'mailhub-imap-lf-test-')), 'mail-access-secret');
  176. const { mailbox } = createMailboxFixture('lf.example', 'lf-user');
  177. const rawMessageBytes = Buffer.from([
  178. 'From: Alice <alice@example.net>',
  179. 'To: admin@lf.example',
  180. 'Subject: LF-only imported',
  181. ' continuation',
  182. 'Message-ID: <lf-only@example.net>',
  183. 'Content-Type: text/plain; charset=UTF-8',
  184. 'X-Not-Selected: private metadata',
  185. '',
  186. 'LF-only body.',
  187. 'Second line.'
  188. ].join('\n'), 'utf8');
  189. createImportedInboundMessage(mailbox, {
  190. importSource: 'vesta:lf-only',
  191. sourceKey: 'lf-only-message',
  192. sender: 'alice@example.net',
  193. recipients: ['admin@lf.example'],
  194. subject: 'LF-only imported continuation',
  195. messageId: '<lf-only@example.net>',
  196. rawMessageBytes,
  197. receivedAt: '2026-07-14T06:19:40.000Z'
  198. });
  199. const [server] = startMailboxAccessServers({
  200. hostname: 'mail.lf.example',
  201. imapEnabled: true,
  202. imapListeners: [{ port: 0, protocol: 'imap' }],
  203. pop3Enabled: false,
  204. pop3Listeners: [],
  205. allowInsecureAuth: true
  206. });
  207. await waitForListening(server);
  208. let client;
  209. try {
  210. client = await connectClient(server.address().port);
  211. await client.readUntil(/\* OK .* IMAP ready\r\n/);
  212. await client.command('A1 LOGIN "admin@lf.example" "mailbox-pass-123"', /A1 OK/);
  213. await client.command('A2 SELECT INBOX', /A2 OK/);
  214. const headerFieldsLabel = 'BODY[HEADER.FIELDS (DATE FROM TO CC REPLY-TO SUBJECT MESSAGE-ID REFERENCES CONTENT-TYPE X-PRIORITY X-MSMMAIL-PRIORITY IMPORTANCE)]';
  215. const headerFieldsResponse = await client.commandBytes(
  216. `A3 UID FETCH 1 (UID FLAGS RFC822.SIZE INTERNALDATE BODY.PEEK[HEADER.FIELDS (DATE FROM TO CC REPLY-TO SUBJECT MESSAGE-ID REFERENCES CONTENT-TYPE X-PRIORITY X-MSMMAIL-PRIORITY IMPORTANCE)])`,
  217. /A3 OK FETCH completed\r\n$/
  218. );
  219. assert.deepEqual(extractFetchLiteral(headerFieldsResponse, headerFieldsLabel), Buffer.from([
  220. 'From: Alice <alice@example.net>',
  221. 'To: admin@lf.example',
  222. 'Subject: LF-only imported',
  223. ' continuation',
  224. 'Message-ID: <lf-only@example.net>',
  225. 'Content-Type: text/plain; charset=UTF-8',
  226. '',
  227. ''
  228. ].join('\r\n'), 'utf8'));
  229. const fullHeaderResponse = await client.commandBytes(
  230. 'A4 UID FETCH 1 (RFC822.HEADER)',
  231. /A4 OK FETCH completed\r\n$/
  232. );
  233. assert.deepEqual(extractFetchLiteral(fullHeaderResponse, 'RFC822.HEADER'), Buffer.from([
  234. 'From: Alice <alice@example.net>',
  235. 'To: admin@lf.example',
  236. 'Subject: LF-only imported',
  237. ' continuation',
  238. 'Message-ID: <lf-only@example.net>',
  239. 'Content-Type: text/plain; charset=UTF-8',
  240. 'X-Not-Selected: private metadata',
  241. '',
  242. ''
  243. ].join('\r\n'), 'utf8'));
  244. const expectedBody = Buffer.from('LF-only body.\nSecond line.', 'utf8');
  245. const rfc822TextResponse = await client.commandBytes(
  246. 'A5 UID FETCH 1 (RFC822.TEXT)',
  247. /A5 OK FETCH completed\r\n$/
  248. );
  249. assert.deepEqual(extractFetchLiteral(rfc822TextResponse, 'RFC822.TEXT'), expectedBody);
  250. const bodyTextResponse = await client.commandBytes(
  251. 'A6 UID FETCH 1 (BODY.PEEK[TEXT])',
  252. /A6 OK FETCH completed\r\n$/
  253. );
  254. assert.deepEqual(extractFetchLiteral(bodyTextResponse, 'BODY[TEXT]'), expectedBody);
  255. const fullMessageResponse = await client.commandBytes(
  256. 'A7 UID FETCH 1 (BODY.PEEK[])',
  257. /A7 OK FETCH completed\r\n$/
  258. );
  259. assert.deepEqual(extractFetchLiteral(fullMessageResponse, 'BODY[]'), rawMessageBytes);
  260. await client.command('A8 LOGOUT', /A8 OK/);
  261. } finally {
  262. client?.close();
  263. await closeServer(server);
  264. }
  265. });
  266. test('IMAP exposes standard folders expected by mainstream clients', async () => {
  267. initDatabase(mkdtempSync(path.join(tmpdir(), 'mailhub-imap-folders-test-')), 'mail-access-secret');
  268. createMailboxFixture('folders.example', 'folders-user');
  269. const [server] = startMailboxAccessServers({
  270. hostname: 'mail.folders.example',
  271. imapEnabled: true,
  272. imapListeners: [{ port: 0, protocol: 'imap' }],
  273. pop3Enabled: false,
  274. pop3Listeners: [],
  275. allowInsecureAuth: true
  276. });
  277. await waitForListening(server);
  278. let client;
  279. try {
  280. client = await connectClient(server.address().port);
  281. await client.readUntil(/\* OK .* IMAP ready\r\n/);
  282. assert.match(await client.command('A1 LOGIN "admin@folders.example" "mailbox-pass-123"', /A1 OK/), /LOGIN completed/);
  283. const listed = await client.command('A2 LIST "" "*"', /A2 OK/);
  284. assert.match(listed, /\* LIST .* "INBOX"/);
  285. assert.match(listed, /\* LIST .*\\Sent.* "Sent"/);
  286. assert.match(listed, /\* LIST .*\\Drafts.* "Drafts"/);
  287. assert.match(listed, /\* LIST .*\\Trash.* "Trash"/);
  288. assert.match(listed, /\* LIST .*\\Junk.* "Junk"/);
  289. assert.match(listed, /\* LIST .*\\Archive.* "Archive"/);
  290. const selected = await client.command('A3 SELECT Sent', /A3 OK/);
  291. assert.match(selected, /\* 0 EXISTS/);
  292. await client.command('A4 LOGOUT', /A4 OK/);
  293. client.close();
  294. } finally {
  295. client?.close();
  296. await closeServer(server);
  297. }
  298. });
  299. test('IMAP uses Modified UTF-7 on the wire while storing Unicode folder names', async () => {
  300. initDatabase(mkdtempSync(path.join(tmpdir(), 'mailhub-imap-utf7-test-')), 'mail-access-secret');
  301. const { user, mailbox } = createMailboxFixture('utf7.example', 'utf7-user');
  302. createInboundFolder(mailbox, '中文 & 项目');
  303. const [server] = startMailboxAccessServers({
  304. hostname: 'mail.utf7.example',
  305. imapEnabled: true,
  306. imapListeners: [{ port: 0, protocol: 'imap' }],
  307. pop3Enabled: false,
  308. pop3Listeners: [],
  309. allowInsecureAuth: true
  310. });
  311. await waitForListening(server);
  312. let client;
  313. try {
  314. client = await connectClient(server.address().port);
  315. await client.readUntil(/\* OK .* IMAP ready\r\n/);
  316. await client.command('A1 LOGIN "admin@utf7.example" "mailbox-pass-123"', /A1 OK/);
  317. const listed = await client.command('A2 LIST "" "*"', /A2 OK/);
  318. assert.match(listed, /"&Ti1lhw- &- &mHl27g-"/);
  319. assert.doesNotMatch(listed, /中文|项目/);
  320. const subscribed = await client.command('A2L LSUB "" "*"', /A2L OK/);
  321. assert.match(subscribed, /"&Ti1lhw- &- &mHl27g-"/);
  322. const selected = await client.command('A3 SELECT "&Ti1lhw- &- &mHl27g-"', /A3 OK/);
  323. assert.match(selected, /\* 0 EXISTS/);
  324. const status = await client.command('A4 STATUS "&Ti1lhw- &- &mHl27g-" \(MESSAGES UNSEEN\)', /A4 OK/);
  325. assert.match(status, /\* STATUS "&Ti1lhw- &- &mHl27g-" \(MESSAGES 0 UNSEEN 0/);
  326. await client.command('A5 CREATE "&ZeVnLIqe-"', /A5 OK/);
  327. assert.equal(inboundFolderExists(mailbox, '日本語'), true);
  328. const rawMessage = [
  329. 'From: Bob <bob@example.net>',
  330. 'To: admin@utf7.example',
  331. 'Subject: UTF-7 folder append',
  332. '',
  333. 'Imported into a Unicode folder.'
  334. ].join('\r\n');
  335. await client.append(
  336. `A6 APPEND "&ZeVnLIqe-" {${Buffer.byteLength(rawMessage, 'utf8')}}`,
  337. rawMessage,
  338. /A6 OK/
  339. );
  340. assert.equal(listInboundMessages(user.id, { folder: '日本語' }).length, 1);
  341. await client.command('A7 LOGOUT', /A7 OK/);
  342. client.close();
  343. } finally {
  344. client?.close();
  345. await closeServer(server);
  346. }
  347. });
  348. test('IMAP APPEND stores sent messages in the Sent folder', async () => {
  349. initDatabase(mkdtempSync(path.join(tmpdir(), 'mailhub-imap-append-test-')), 'mail-access-secret');
  350. const { user } = createMailboxFixture('append.example', 'append-user');
  351. const [server] = startMailboxAccessServers({
  352. hostname: 'mail.append.example',
  353. imapEnabled: true,
  354. imapListeners: [{ port: 0, protocol: 'imap' }],
  355. pop3Enabled: false,
  356. pop3Listeners: [],
  357. allowInsecureAuth: true
  358. });
  359. await waitForListening(server);
  360. let client;
  361. try {
  362. const sentMessage = [
  363. 'From: Admin <admin@append.example>',
  364. 'To: Bob <bob@example.net>',
  365. 'Subject: =?UTF-8?Q?=E6=A0=B8=E4=BA=91?=',
  366. ' =?UTF-8?Q?=E8=AE=A1=E7=AE=97?=',
  367. 'Message-ID: <sent-copy@append.example>',
  368. 'MIME-Version: 1.0',
  369. 'Content-Type: multipart/alternative; boundary="sent-boundary"',
  370. '',
  371. '--sent-boundary',
  372. 'Content-Type: text/plain; charset=UTF-8',
  373. 'Content-Transfer-Encoding: base64',
  374. '',
  375. Buffer.from('工单正文', 'utf8').toString('base64'),
  376. '--sent-boundary',
  377. 'Content-Type: text/html; charset=UTF-8',
  378. 'Content-Transfer-Encoding: quoted-printable',
  379. '',
  380. '<p>Sent HTML body.</p>',
  381. '--sent-boundary--',
  382. ''
  383. ].join('\r\n');
  384. client = await connectClient(server.address().port);
  385. await client.readUntil(/\* OK .* IMAP ready\r\n/);
  386. assert.match(await client.command('A1 LOGIN "admin@append.example" "mailbox-pass-123"', /A1 OK/), /LOGIN completed/);
  387. await client.append(`A2 APPEND Sent (\\Seen) {${Buffer.byteLength(sentMessage, 'utf8')}}`, sentMessage, /A2 OK/);
  388. const selectedSent = await client.command('A3 SELECT Sent', /A3 OK/);
  389. assert.match(selectedSent, /\* 1 EXISTS/);
  390. const fetchedSent = await client.command('A4 UID FETCH 1:* (UID FLAGS BODY.PEEK[])', /A4 OK/);
  391. assert.match(fetchedSent, /FLAGS \(\\Seen\)/);
  392. assert.match(fetchedSent, /Subject: =\?UTF-8\?Q\?/);
  393. assert.match(fetchedSent, /--sent-boundary/);
  394. const [storedSummary] = listInboundMessages(user.id, { folder: 'Sent' });
  395. const storedMessage = getInboundMessage(user.id, storedSummary.id);
  396. assert.equal(storedMessage.subject, '核云计算');
  397. assert.equal(storedMessage.textBody, '工单正文');
  398. assert.match(storedMessage.htmlBody, /Sent HTML body/);
  399. assert.equal(storedMessage.preview, '工单正文');
  400. assert.match(storedMessage.rawMessage, /--sent-boundary/);
  401. const latin1Message = Buffer.concat([
  402. Buffer.from([
  403. 'From: Admin <admin@append.example>',
  404. 'To: Bob <bob@example.net>',
  405. 'Subject: Latin1 copy',
  406. 'Content-Type: text/plain; charset=ISO-8859-1',
  407. 'Content-Transfer-Encoding: 8bit',
  408. '',
  409. 'caf'
  410. ].join('\r\n'), 'ascii'),
  411. Buffer.from([0xe9])
  412. ]);
  413. await client.append(`A5 APPEND Sent {${latin1Message.length}}`, latin1Message, /A5 OK/);
  414. const latin1Summary = listInboundMessages(user.id, { folder: 'Sent' })
  415. .find((message) => message.subject === 'Latin1 copy');
  416. assert.equal(getInboundMessage(user.id, latin1Summary.id).textBody, 'café');
  417. await client.command('A6 SELECT Sent', /A6 OK/);
  418. const latin1Fetch = await client.commandBytes('A7 UID FETCH 1:* (UID BODY.PEEK[])', /A7 OK/);
  419. assert.equal(latin1Fetch.includes(latin1Message), true);
  420. const selectedInbox = await client.command('A8 SELECT INBOX', /A8 OK/);
  421. assert.match(selectedInbox, /\* 0 EXISTS/);
  422. await client.command('A9 LOGOUT', /A9 OK/);
  423. client.close();
  424. } finally {
  425. client?.close();
  426. await closeServer(server);
  427. }
  428. });
  429. test('POP3 clients can retrieve and delete messages on quit', async () => {
  430. const database = initDatabase(mkdtempSync(path.join(tmpdir(), 'mailhub-pop3-test-')), 'mail-access-secret');
  431. const { user, mailbox } = createMailboxFixture('pop3.example', 'pop3-user');
  432. const firstRawMessage = [
  433. 'From: Bob <bob@example.net>',
  434. 'To: admin@pop3.example',
  435. 'Subject: POP3 hello',
  436. 'Message-ID: <pop3-hello@example.net>',
  437. '',
  438. 'Hello through POP3.'
  439. ].join('\r\n');
  440. const firstMessage = createInboundMessage(mailbox, {
  441. sender: 'bob@example.net',
  442. recipients: ['admin@pop3.example'],
  443. subject: 'POP3 hello',
  444. messageId: '<pop3-hello@example.net>',
  445. rawMessage: firstRawMessage,
  446. textBody: 'Hello through POP3.'
  447. });
  448. const latin1RawMessage = Buffer.concat([
  449. Buffer.from([
  450. 'From: Alice <alice@example.net>',
  451. 'To: admin@pop3.example',
  452. 'Subject: Latin1 POP3',
  453. 'Content-Type: text/plain; charset=ISO-8859-1',
  454. 'Content-Transfer-Encoding: 8bit',
  455. '',
  456. 'caf'
  457. ].join('\n'), 'ascii'),
  458. Buffer.from([0xe9])
  459. ]);
  460. createInboundMessage(mailbox, {
  461. sender: 'alice@example.net',
  462. recipients: ['admin@pop3.example'],
  463. subject: 'Latin1 POP3',
  464. rawMessage: latin1RawMessage.toString('latin1'),
  465. rawMessageBytes: latin1RawMessage,
  466. textBody: 'café'
  467. });
  468. const firstPop3Message = Buffer.from(`${firstRawMessage}\r\n`, 'utf8');
  469. const latin1Pop3Message = Buffer.concat([
  470. Buffer.from(latin1RawMessage.toString('latin1').replace(/\n/g, '\r\n'), 'latin1'),
  471. Buffer.from('\r\n')
  472. ]);
  473. const totalOctets = firstPop3Message.length + latin1Pop3Message.length;
  474. const [server] = startMailboxAccessServers({
  475. hostname: 'mail.pop3.example',
  476. imapEnabled: false,
  477. imapListeners: [],
  478. pop3Enabled: true,
  479. pop3Listeners: [{ port: 0, protocol: 'pop3' }],
  480. allowInsecureAuth: true
  481. });
  482. await waitForListening(server);
  483. try {
  484. const client = await connectClient(server.address().port);
  485. await client.readUntil(/\+OK .* POP3 ready\r\n/);
  486. assert.match(await client.command('USER admin@pop3.example', /\+OK/), /User accepted/);
  487. assert.match(await client.command('PASS mailbox-pass-123', /\+OK/), /ready/);
  488. assert.match(await client.command('STAT', /\+OK \d+ \d+/), new RegExp(`\\+OK 2 ${totalOctets}`));
  489. const listed = await client.command('LIST', /\r\n\.\r\n/);
  490. assert.match(listed, new RegExp(`1 ${firstPop3Message.length}\\r\\n`));
  491. assert.match(listed, new RegExp(`2 ${latin1Pop3Message.length}\\r\\n`));
  492. assert.match(await client.command('UIDL 1', /\+OK 1 mh-1/), /\+OK 1 mh-1/);
  493. database
  494. .prepare('UPDATE inbound_messages SET raw_message_bytes = ? WHERE id = ?')
  495. .run(Buffer.from(firstRawMessage.replace('Hello through POP3.', 'Hallo through POP3.'), 'utf8'), firstMessage.id);
  496. const retrieved = await client.command('RETR 1', /\r\n\.\r\n/);
  497. assert.match(retrieved, /Subject: POP3 hello/);
  498. assert.match(retrieved, /Hallo through POP3\./);
  499. assert.doesNotMatch(retrieved, /Hello through POP3\./);
  500. const latin1Retrieved = await client.commandBytes('RETR 2', /\r\n\.\r\n/);
  501. assert.deepEqual(latin1Retrieved, Buffer.concat([
  502. Buffer.from(`+OK ${latin1Pop3Message.length} octets\r\n`),
  503. latin1Pop3Message,
  504. Buffer.from('.\r\n')
  505. ]));
  506. assert.match(await client.command('DELE 1', /\+OK/), /deleted/);
  507. assert.match(await client.command('DELE 2', /\+OK/), /deleted/);
  508. await client.command('QUIT', /\+OK Bye/);
  509. client.close();
  510. assert.equal(listInboundMessages(user.id).length, 0);
  511. } finally {
  512. await closeServer(server);
  513. }
  514. });
  515. test('POP3 AUTH PLAIN requires TLS when insecure authentication is disabled', async () => {
  516. const [server] = startMailboxAccessServers({
  517. hostname: 'mail.secure-pop3.example',
  518. imapEnabled: false,
  519. imapListeners: [],
  520. pop3Enabled: true,
  521. pop3Listeners: [{ port: 0, protocol: 'pop3' }],
  522. allowInsecureAuth: false
  523. });
  524. await waitForListening(server);
  525. let client;
  526. try {
  527. client = await connectClient(server.address().port);
  528. await client.readUntil(/\+OK .* POP3 ready\r\n/);
  529. const credentials = Buffer.from('\u0000user@example.com\u0000password').toString('base64');
  530. assert.equal(
  531. await client.command(`AUTH PLAIN ${credentials}`, /\+OK|\-ERR/),
  532. '-ERR Encryption required for authentication\r\n'
  533. );
  534. } finally {
  535. client?.close();
  536. await closeServer(server);
  537. }
  538. });
  539. function createMailboxFixture(domainName, username) {
  540. const user = createUser({ username, email: `${username}@example.com`, password: 'password123' });
  541. createDomain(user.id, {
  542. domain: domainName,
  543. selector: 'mh',
  544. verificationToken: 'verify',
  545. dkimPublic: 'public',
  546. dkimPrivate: 'private',
  547. senderHost: `mail.${domainName}`,
  548. sendingIp: '192.0.2.30',
  549. spfExtra: '',
  550. dmarcPolicy: 'none',
  551. dmarcRua: ''
  552. });
  553. const mailbox = createInboundMailbox(user.id, {
  554. address: `admin@${domainName}`,
  555. password: 'mailbox-pass-123'
  556. });
  557. return { user, mailbox };
  558. }
  559. function connectClient(port) {
  560. return new Promise((resolve, reject) => {
  561. const socket = net.createConnection({ host: '127.0.0.1', port });
  562. socket.setTimeout(5000);
  563. let buffer = '';
  564. let rawBuffer = Buffer.alloc(0);
  565. const waiters = [];
  566. socket.on('data', (chunk) => {
  567. const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
  568. rawBuffer = Buffer.concat([rawBuffer, bytes]);
  569. buffer += bytes.toString('utf8');
  570. for (const waiter of [...waiters]) {
  571. if (waiter.pattern.test(buffer)) {
  572. waiters.splice(waiters.indexOf(waiter), 1);
  573. const output = buffer;
  574. const rawOutput = rawBuffer;
  575. buffer = '';
  576. rawBuffer = Buffer.alloc(0);
  577. waiter.resolve(waiter.raw ? rawOutput : output);
  578. }
  579. }
  580. });
  581. socket.once('connect', () => resolve({
  582. command(command, pattern) {
  583. socket.write(`${command}\r\n`);
  584. return this.readUntil(pattern);
  585. },
  586. commandBytes(command, pattern) {
  587. socket.write(`${command}\r\n`);
  588. return this.readUntil(pattern, true);
  589. },
  590. async append(command, literal, pattern) {
  591. socket.write(`${command}\r\n`);
  592. await this.readUntil(/^\+ /m);
  593. socket.write(literal);
  594. socket.write('\r\n');
  595. return this.readUntil(pattern);
  596. },
  597. readUntil(pattern, raw = false) {
  598. if (pattern.test(buffer)) {
  599. const output = buffer;
  600. const rawOutput = rawBuffer;
  601. buffer = '';
  602. rawBuffer = Buffer.alloc(0);
  603. return Promise.resolve(raw ? rawOutput : output);
  604. }
  605. return new Promise((waitResolve, waitReject) => {
  606. const waiter = {
  607. pattern,
  608. raw,
  609. resolve(output) {
  610. clearTimeout(waiter.timer);
  611. waitResolve(output);
  612. },
  613. reject(error) {
  614. clearTimeout(waiter.timer);
  615. waitReject(error);
  616. },
  617. timer: null
  618. };
  619. waiter.timer = setTimeout(() => {
  620. waiters.splice(waiters.indexOf(waiter), 1);
  621. waitReject(new Error(`Timed out waiting for ${pattern}; buffered response: ${buffer}`));
  622. }, 5000);
  623. waiters.push(waiter);
  624. });
  625. },
  626. close() {
  627. socket.destroy();
  628. }
  629. }));
  630. socket.once('error', reject);
  631. socket.once('timeout', () => reject(new Error('Mail access client timed out')));
  632. });
  633. }
  634. function extractFetchLiteral(response, label) {
  635. const bytes = Buffer.isBuffer(response) ? response : Buffer.from(response || '');
  636. const marker = Buffer.from(`${label} {`, 'ascii');
  637. const markerIndex = bytes.indexOf(marker);
  638. assert.notEqual(markerIndex, -1, `Missing ${label} literal marker`);
  639. const sizeStart = markerIndex + marker.length;
  640. const sizeEndMarker = Buffer.from('}\r\n', 'ascii');
  641. const sizeEnd = bytes.indexOf(sizeEndMarker, sizeStart);
  642. assert.notEqual(sizeEnd, -1, `Missing ${label} literal size terminator`);
  643. const size = Number(bytes.subarray(sizeStart, sizeEnd).toString('ascii'));
  644. assert.equal(Number.isInteger(size) && size >= 0, true, `Invalid ${label} literal size`);
  645. const literalStart = sizeEnd + sizeEndMarker.length;
  646. const literalEnd = literalStart + size;
  647. assert.ok(literalEnd <= bytes.length, `Truncated ${label} literal`);
  648. assert.deepEqual(bytes.subarray(literalEnd, literalEnd + 5), Buffer.from('\r\n)\r\n', 'ascii'));
  649. return bytes.subarray(literalStart, literalEnd);
  650. }
  651. function waitForListening(server) {
  652. if (server.listening) return Promise.resolve();
  653. return new Promise((resolve) => server.once('listening', resolve));
  654. }
  655. function closeServer(server) {
  656. return new Promise((resolve, reject) => {
  657. server.close((error) => error ? reject(error) : resolve());
  658. });
  659. }