-
Notifications
You must be signed in to change notification settings - Fork 114
/
mobile-config.js
484 lines (454 loc) · 18.9 KB
/
mobile-config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/**
* Copyright (c) Forward Email LLC
* SPDX-License-Identifier: BUSL-1.1
*/
const fs = require('node:fs');
const Boom = require('@hapi/boom');
const RE2 = require('re2');
const dashify = require('dashify');
const humanize = require('humanize-string');
const isSANB = require('is-string-and-not-blank');
const mobileconfig = require('mobileconfig');
const mongoose = require('mongoose');
const plist = require('plist');
const shortID = require('mongodb-short-id');
const titleize = require('titleize');
const { isEmail } = require('validator');
// https://github.com/danakt/uuid-by-string/issues/24
const getUuid = require('@forwardemail/uuid-by-string');
const Aliases = require('#models/aliases');
const env = require('#config/env');
const { decrypt } = require('#helpers/encrypt-decrypt');
const isValidPassword = require('#helpers/is-valid-password');
//
// (this punctuation stuff is borrowed from our work with `spamscanner`)
// punctuation characters
// (need stripped from tokenization)
// <https://github.com/regexhq/punctuation-regex>
// NOTE: we prepended a normal "-" hyphen since it was missing
const PUNCTUATION_REGEX = new RE2(
/[-‒–—―|$&~=\\/⁄@+*!?({[\]})<>‹›«».;:^‘’“”'",،、`·•†‡°″¡¿※#№÷×%‰−‱¶′‴§_‖¦]/g
);
let keys;
if (isSANB(env.WEB_SSL_CA_PATH) && env.WEB_SSL_CA_PATH && env.WEB_SSL_CA_PATH)
keys = {
key: fs.readFileSync(env.WEB_SSL_KEY_PATH),
cert: fs.readFileSync(env.WEB_SSL_CERT_PATH),
ca: fs.readFileSync(env.WEB_SSL_CA_PATH)
// `hashAlg` option defaults to "sha256"
// <https://github.com/andris9/mobileconfig?tab=readme-ov-file#signing-configuration>
};
// macOS and iOS mobileconfig template (gets signed with our website SSL keys)
//
// NOTE: for PayloadIdentifier we use dashify on the email
// which gives us reverse DNS style required
//
// > require('dashify')('[email protected]')
// 'foo-bar-com'
//
// <https://developer.apple.com/business/documentation/Configuration-Profile-Reference.pdf>
//
function mobileConfigTemplate(name, username, password) {
return {
PayloadDescription: 'Forward Email Settings',
PayloadDisplayName: `Forward Email (${username})`,
PayloadIdentifier: `net.forwardemail.mobileconfig.${dashify(username)}`,
PayloadOrganization: 'Forward Email LLC',
PayloadType: 'Configuration',
PayloadUUID: getUuid(username),
PayloadVersion: 1,
PayloadContent: [
{
EmailAccountDescription: name,
EmailAccountName: username,
EmailAccountType: 'EmailTypeIMAP',
EmailAddress: username,
IncomingMailServerAuthentication: 'EmailAuthPassword',
IncomingMailServerHostName: 'imap.forwardemail.net',
IncomingMailServerPortNumber: 993,
IncomingMailServerUseSSL: true,
IncomingMailServerUsername: username,
IncomingPassword: password,
OutgoingMailServerAuthentication: 'EmailAuthPassword',
OutgoingMailServerHostName: 'smtp.forwardemail.net',
OutgoingMailServerPortNumber: 465,
OutgoingMailServerUseSSL: true,
OutgoingMailServerUsername: username,
OutgoingPassword: password,
OutgoingPasswordSameAsIncomingPassword: true,
PayloadDisplayName: `${name}${name.endsWith('s') ? "'" : "'s"} Email`,
PayloadIdentifier: `net.forwardemail.mobileconfig.email.${dashify(
username
)}`,
PayloadType: 'com.apple.mail.managed',
PayloadUUID: getUuid('email', getUuid(username)),
PayloadVersion: 1
},
{
CalDAVAccountDescription: `${name}${
name.endsWith('s') ? "'" : "'s"
} Calendars`,
CalDAVHostName: 'caldav.forwardemail.net',
CalDAVUsername: username,
CalDAVPassword: password,
CalDAVUseSSL: true,
PayloadDisplayName: `${name}${
name.endsWith('s') ? "'" : "'s"
} Calendars`,
PayloadIdentifier: `net.forwardemail.mobileconfig.caldav.${dashify(
username
)}`,
PayloadType: 'com.apple.caldav.account',
PayloadUUID: getUuid('caldav', getUuid(username)),
PayloadVersion: 1
}
/*
// TODO: CardDAV
{
CardDAVAccountDescription: `${name}${name.endsWith('s') ? "'" : "'s"} Contacts`,
CardDAVHostName: 'carddav.forwardemail.net',
CardDAVPassword: password,
CardDAVUseSSL: true,
CardDAVUsername: username,
PayloadDisplayName: `${name}${name.endsWith('s') ? "'" : "'s"} Contacts`,
PayloadIdentifier: `net.forwardemail.mobileconfig.carddav.${dashify(username)}`,
PayloadType: 'com.apple.carddav.account',
PayloadUUID: getUuid('carddav', getUuid(username)),
PayloadVersion: 1
}
*/
]
};
}
//
// NOTE: that we use an input/seed for generating account uuid
//
// here's how K-9 generates UUID's
// <https://github.com/thunderbird/thunderbird-android/blob/a8523dc743a87ae60a56a44a7478889a13e0091a/legacy/core/src/main/java/com/fsck/k9/preferences/AccountSettingsWriter.kt#L118-L119>
// (they use v4 by default)
//
// <https://stackoverflow.com/a/69958290>
//
function k9s(name, username) {
return `<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<k9settings version="90" format="1">
<global>
<value key="animations">false</value>
<value key="backgroundOperations">ALWAYS</value>
<value key="changeRegisteredNameColor">false</value>
<value key="confirmDelete">false</value>
<value key="confirmDeleteStarred">false</value>
<value key="confirmSpam">false</value>
<value key="confirmMarkAllRead">true</value>
<value key="enableDebugLogging">false</value>
<value key="enableSensitiveLogging">false</value>
<value key="fontSizeMessageComposeInput" />
<value key="fontSizeMessageListDate" />
<value key="fontSizeMessageListPreview" />
<value key="fontSizeMessageListSender" />
<value key="fontSizeMessageListSubject" />
<value key="fontSizeMessageViewAdditionalHeaders" />
<value key="fontSizeMessageViewCC" />
<value key="fontSizeMessageViewDate" />
<value key="fontSizeMessageViewSender" />
<value key="fontSizeMessageViewSubject" />
<value key="fontSizeMessageViewTime" />
<value key="fontSizeMessageViewTo" />
<value key="language">default</value>
<value key="messageListPreviewLines">2</value>
<value key="messageListStars">true</value>
<value key="messageViewFixedWidthFont">false</value>
<value key="quietTimeEnabled">false</value>
<value key="quietTimeEnds">7:00</value>
<value key="quietTimeStarts">21:00</value>
<value key="registeredNameColor">#1093f5</value>
<value key="showContactName">false</value>
<value key="showCorrespondentNames">true</value>
<value key="showUnifiedInbox">true</value>
<value key="sortTypeEnum">SORT_DATE</value>
<value key="sortAscending">false</value>
<value key="theme">follow_system</value>
<value key="messageViewTheme">use_global</value>
<value key="useVolumeKeysForNavigation">false</value>
<value key="useBackgroundAsUnreadIndicator">false</value>
<value key="threadedView">true</value>
<value key="splitViewMode">NEVER</value>
<value key="messageComposeTheme">use_global</value>
<value key="fixedMessageViewTheme">true</value>
<value key="showContactPicture">true</value>
<value key="autofitWidth">true</value>
<value key="colorizeMissingContactPictures">true</value>
<value key="messageViewDeleteActionVisible">true</value>
<value key="messageViewArchiveActionVisible">false</value>
<value key="messageViewMoveActionVisible">false</value>
<value key="messageViewCopyActionVisible">false</value>
<value key="messageViewSpamActionVisible">false</value>
<value key="fontSizeMessageViewContentPercent">100</value>
<value key="hideUserAgent">false</value>
<value key="hideTimeZone">false</value>
<value key="lockScreenNotificationVisibility">MESSAGE_COUNT</value>
<value key="confirmDeleteFromNotification">true</value>
<value key="messageListSenderAboveSubject">false</value>
<value key="notificationQuickDelete">ALWAYS</value>
<value key="notificationDuringQuietTimeEnabled">true</value>
<value key="confirmDiscardMessage">true</value>
<value key="pgpInlineDialogCounter">0</value>
<value key="pgpSignOnlyDialogCounter">0</value>
<value key="fontSizeMessageViewBCC" />
<value key="showRecentChanges">true</value>
<value key="showStarredCount">false</value>
<value key="swipeRightAction">ToggleSelection</value>
<value key="swipeLeftAction">ToggleRead</value>
<value key="showComposeButtonOnMessageList">true</value>
<value key="messageListDensity">Default</value>
<value key="fontSizeMessageViewAccountName" />
<value key="messageViewPostDeleteAction">ReturnToMessageList</value>
<value key="messageViewPostMarkAsUnreadAction">ReturnToMessageList</value>
</global>
<accounts>
<account uuid="${getUuid(username)}">
<name>${username}</name>
<incoming-server type="IMAP">
<host>imap.forwardemail.net</host>
<port>993</port>
<connection-security>SSL_TLS_REQUIRED</connection-security>
<authentication-type>PLAIN</authentication-type>
<username>${username}</username>
<extra>
<value key="autoDetectNamespace">true</value>
<value key="pathPrefix" />
</extra>
</incoming-server>
<outgoing-server type="SMTP">
<host>smtp.forwardemail.net</host>
<port>465</port>
<connection-security>SSL_TLS_REQUIRED</connection-security>
<authentication-type>PLAIN</authentication-type>
<username>${username}</username>
</outgoing-server>
<settings>
<value key="alwaysShowCcBcc">false</value>
<value key="archiveFolderSelection">AUTOMATIC</value>
<value key="autocryptMutualMode">false</value>
<value key="automaticCheckIntervalMinutes">15</value>
<value key="chipColor">#1976d2</value>
<value key="defaultQuotedTextShown">true</value>
<value key="deletePolicy">DELETE</value>
<value key="displayCount">25</value>
<value key="draftsFolderSelection">AUTOMATIC</value>
<value key="expungePolicy">EXPUNGE_IMMEDIATELY</value>
<value key="folderDisplayMode">NOT_SECOND_CLASS</value>
<value key="folderNotifyNewMailMode">ALL</value>
<value key="folderPushMode">NONE</value>
<value key="folderSyncMode">FIRST_CLASS</value>
<value key="folderTargetMode">NOT_SECOND_CLASS</value>
<value key="idleRefreshMinutes">24</value>
<value key="ignoreChatMessages">false</value>
<value key="localStorageProvider">InternalStorage</value>
<value key="markMessageAsReadOnDelete">true</value>
<value key="markMessageAsReadOnView">true</value>
<value key="maxPushFolders">10</value>
<value key="maximumAutoDownloadMessageSize">32768</value>
<value key="maximumPolledMessageAge">-1</value>
<value key="messageFormat">HTML</value>
<value key="messageFormatAuto">false</value>
<value key="messageReadReceipt">false</value>
<value key="notificationLight">Disabled</value>
<value key="notifyContactsMailOnly">false</value>
<value key="notifyMailCheck">false</value>
<value key="notifyNewMail">true</value>
<value key="notifySelfNewMail">true</value>
<value key="openPgpEncryptAllDrafts">false</value>
<value key="openPgpEncryptSubject">false</value>
<value key="openPgpHideSignOnly">false</value>
<value key="quotePrefix">></value>
<value key="quoteStyle">PREFIX</value>
<value key="remoteSearchFullText">false</value>
<value key="remoteSearchNumResults">25</value>
<value key="replyAfterQuote">false</value>
<value key="ring">true</value>
<value key="ringtone">content://settings/system/notification_sound</value>
<value key="searchableFolders">ALL</value>
<value key="sendClientId">true</value>
<value key="sentFolderSelection">AUTOMATIC</value>
<value key="showPicturesEnum">NEVER</value>
<value key="signatureBeforeQuotedText">false</value>
<value key="sortAscending">false</value>
<value key="sortTypeEnum">SORT_DATE</value>
<value key="spamFolderSelection">AUTOMATIC</value>
<value key="stripSignature">true</value>
<value key="subscribedFoldersOnly">false</value>
<value key="syncRemoteDeletions">true</value>
<value key="trashFolderSelection">AUTOMATIC</value>
<value key="uploadSentMessages">true</value>
<value key="useCompression">true</value>
<value key="vibrate">false</value>
<value key="vibratePattern">0</value>
<value key="vibrateTimes">5</value>
<value key="autoExpandFolderName">INBOX</value>
<value key="archiveFolderName">Archive</value>
<value key="draftsFolderName">Drafts</value>
<value key="sentFolderName">Sent Mail</value>
<value key="spamFolderName">Spam</value>
<value key="trashFolderName">Trash</value>
</settings>
<identities>
<identity>
<name>${name}</name>
<email>${username}</email>
<description>Initial identity</description>
<settings>
<value key="signature">${name}</value>
<value key="signatureUse">true</value>
</settings>
</identity>
</identities>
<folders>
<folder name="INBOX">
<value key="integrate">true</value>
<value key="inTopGroup">false</value>
<value key="syncMode">FIRST_CLASS</value>
<value key="displayMode">FIRST_CLASS</value>
<value key="notifyMode">FIRST_CLASS</value>
<value key="pushMode">FIRST_CLASS</value>
</folder>
<folder name="Archive">
<value key="integrate">false</value>
<value key="inTopGroup">false</value>
<value key="syncMode">NO_CLASS</value>
<value key="displayMode">FIRST_CLASS</value>
<value key="notifyMode">INHERITED</value>
<value key="pushMode">SECOND_CLASS</value>
</folder>
<folder name="Drafts">
<value key="integrate">false</value>
<value key="inTopGroup">false</value>
<value key="syncMode">NO_CLASS</value>
<value key="displayMode">FIRST_CLASS</value>
<value key="notifyMode">INHERITED</value>
<value key="pushMode">SECOND_CLASS</value>
</folder>
<folder name="Sent Mail">
<value key="integrate">false</value>
<value key="inTopGroup">false</value>
<value key="syncMode">NO_CLASS</value>
<value key="displayMode">FIRST_CLASS</value>
<value key="notifyMode">INHERITED</value>
<value key="pushMode">SECOND_CLASS</value>
</folder>
<folder name="Spam">
<value key="integrate">false</value>
<value key="inTopGroup">false</value>
<value key="syncMode">NO_CLASS</value>
<value key="displayMode">FIRST_CLASS</value>
<value key="notifyMode">INHERITED</value>
<value key="pushMode">SECOND_CLASS</value>
</folder>
<folder name="Trash">
<value key="integrate">false</value>
<value key="inTopGroup">false</value>
<value key="syncMode">NO_CLASS</value>
<value key="displayMode">FIRST_CLASS</value>
<value key="notifyMode">INHERITED</value>
<value key="pushMode">SECOND_CLASS</value>
</folder>
</folders>
</account>
</accounts>
</k9settings>
`.trim();
}
//
// NOTE: for security reasons (so users can't guess that their inputs are accurate) we 404
// (which is similar to what we do for the forgot password stuff, e.g. so users can't guess valid emails)
//
// eslint-disable-next-line complexity
async function mobileConfig(ctx, next) {
try {
// must end with `.mobileconfig` or `.k9s`
if (!ctx.path.endsWith('.mobileconfig') && !ctx.path.endsWith('.k9s'))
return next(); // 404
// ctx.params.username must be a valid email address
if (
!isSANB(ctx.params.username) ||
!isEmail(ctx.params.username, { ignore_max_length: true })
)
return next(); // 404
// ?a= alias ID
if (!isSANB(ctx.query.a)) return next(); // 404
try {
ctx.query.a = shortID.shortToLong(ctx.query.a);
} catch (err) {
ctx.logger.debug(err);
return next(); // 404
}
// safeguard
if (!mongoose.isObjectIdOrHexString(ctx.query.a)) return next(); // 404
// ?p = encrypted password
if (!isSANB(ctx.query.p)) return next(); // 404
const alias = await Aliases.findById(ctx.query.a)
.select('+tokens.hash +tokens.salt')
.populate('domain', 'name')
.lean()
.exec();
if (!alias) return next(); // 404
// cannot be catch-all nor regex
if (alias.name === '*' || alias.name.startsWith('/')) return next(); // 404
if (!Array.isArray(alias.tokens) || alias.tokens.length === 0)
return next(); // 404
// validate password
// ensure that the token is valid
const isValid = await isValidPassword(alias.tokens, decrypt(ctx.query.p));
if (!isValid) return next(); // 404
const username = `${alias.name}@${alias.domain.name}`;
// safeguard
if (ctx.params.username !== username) return next(); // 404
// name is our best guess of a human-friendly version of alias.name
// support -> Support
// foo.bar -> Foo Bar
// foo_bar -> Foo Bar
const name = titleize(humanize(alias.name.replace(PUNCTUATION_REGEX, ' ')));
// macOS and iOS mobile config (signed)
if (ctx.path.endsWith('.mobileconfig')) {
// .mobileconfig is normally "text/xml" but if signed
// it is then "application/octet-stream"
ctx.type = 'application/octet-stream';
ctx.set(
'Content-Disposition',
`attachment; filename="${username}.mobileconfig"`
);
const plistData = mobileConfigTemplate(
name,
username,
decrypt(ctx.query.p)
);
// development and test envs usually don't have SSL keys
if (!keys) {
ctx.body = plist.build(plistData);
return;
}
ctx.body = await new Promise((resolve, reject) => {
mobileconfig.getSignedConfig(plistData, keys, function (err, data) {
if (err) return reject(err);
resolve(data);
});
});
return;
}
// K-9 mail config (unsigned; does not contain password because they don't support that yet)
// (ty SW)
if (ctx.path.endsWith('.k9s')) {
ctx.type = 'text/xml';
ctx.set('Content-Disposition', `attachment; filename="${username}.k9s"`);
ctx.body = k9s(name, username);
return;
}
// safeguard to 404
return next();
} catch (err) {
ctx.logger.error(err);
if (err.isBoom) return ctx.throw(err);
ctx.throw(Boom.clientTimeout(ctx.translate('WEBSITE_OUTAGE')));
}
}
module.exports = mobileConfig;