Repository navigation
Expand file tree
/
Copy pathapi.js
More file actions
943 lines (824 loc) · 39.2 KB
/
Copy pathapi.js
File metadata and controls
943 lines (824 loc) · 39.2 KB
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
'use strict';
const config = require('@zone-eu/wild-config');
const fastify = require('fastify');
const fastifyCors = require('@fastify/cors');
const fastifyMultipart = require('@fastify/multipart');
const fastifyStatic = require('@fastify/static');
const fastifySwagger = require('@fastify/swagger');
const fs = require('fs');
const qs = require('qs');
const log = require('npmlog');
const UserHandler = require('./lib/user-handler');
const MailboxHandler = require('./lib/mailbox-handler');
const MessageHandler = require('./lib/message-handler');
const StorageHandler = require('./lib/storage-handler');
const AuditHandler = require('./lib/audit-handler');
const ImapNotifier = require('./lib/imap-notifier');
const db = require('./lib/db');
const certs = require('./lib/certs');
const tools = require('./lib/tools');
const consts = require('./lib/consts');
const crypto = require('crypto');
const Gelf = require('gelf');
const os = require('os');
const util = require('util');
const ObjectId = require('mongodb').ObjectId;
const tls = require('tls');
const Lock = require('ioredfour');
const Path = require('path');
const { normalizeLoggelfMessage } = require('./lib/loggelf-message');
const ApnClient = require('./lib/apn-client');
const metrics = require('./lib/metrics');
const { attachNativeRoutes } = require('./lib/fastify/routes');
const { attachResponseContractCheck, createViolationLog } = require('./lib/fastify/response-contract');
const { sharedSchemas, stripInternalKeywords } = require('./lib/fastify/validation');
const {
baseServerOptions,
maskUrl,
requestParams,
attachRequestDecorations,
attachReplyDecorations,
attachPayloadStash,
attachResponseHeaders,
attachAccessLog,
attachErrorHandler
} = require('./lib/fastify/bootstrap');
require('./lib/schemas/json-schemas'); // populates sharedSchemas
const apiDocsConfig = require('./config/apigeneration.json');
const packageData = require('./package.json');
const acmeRoutes = require('./lib/api/acme');
const usersRoutes = require('./lib/api/users');
const addressesRoutes = require('./lib/api/addresses');
const mailboxesRoutes = require('./lib/api/mailboxes');
const messagesRoutes = require('./lib/api/messages');
const storageRoutes = require('./lib/api/storage');
const filtersRoutes = require('./lib/api/filters');
const domainaccessRoutes = require('./lib/api/domainaccess');
const aspsRoutes = require('./lib/api/asps');
const totpRoutes = require('./lib/api/2fa/totp');
const custom2faRoutes = require('./lib/api/2fa/custom');
const webauthnRoutes = require('./lib/api/2fa/webauthn');
const updatesRoutes = require('./lib/api/updates');
const authRoutes = require('./lib/api/auth');
const autoreplyRoutes = require('./lib/api/autoreply');
const submitRoutes = require('./lib/api/submit');
const auditRoutes = require('./lib/api/audit');
const domainaliasRoutes = require('./lib/api/domainaliases');
const dkimRoutes = require('./lib/api/dkim');
const certsRoutes = require('./lib/api/certs');
const webhooksRoutes = require('./lib/api/webhooks');
const settingsRoutes = require('./lib/api/settings');
const healthRoutes = require('./lib/api/health');
const pushsubscriptionsRoutes = require('./lib/api/pushsubscriptions');
const mcpTokensRoutes = require('./lib/api/mcp-tokens');
const { SettingsHandler } = require('./lib/settings-handler');
const McpTokenHandler = require('./lib/mcp-token-handler');
const roles = require('./lib/roles');
// The only routes an MCP credential may reach, by route name and method. Read routes map to
// the resource whose field allowlist shapes the response. The sole non-read route revokes the
// credential that authenticated that same request.
const MCP_ROUTES = new Map([
['getuser', { method: 'GET', resource: 'users' }],
['getuseraddresses', { method: 'GET', resource: 'addresses' }],
['getmailboxes', { method: 'GET', resource: 'mailboxes' }],
['getmailbox', { method: 'GET', resource: 'mailboxes' }],
['getuserlabels', { method: 'GET', resource: 'labels', listKey: 'labels' }],
['getmessages', { method: 'GET', resource: 'messages' }],
['getmessage', { method: 'GET', resource: 'messages' }],
['searchmessages', { method: 'GET', resource: 'messages' }],
['invalidateaccesstoken', { method: 'DELETE' }]
]);
// logged param values are truncated to 128 chars, so there is no point in
// rendering more than that per nested value
const INSPECT_OPTIONS = { depth: 3, maxStringLength: 160, maxArrayLength: 20, breakLength: Infinity };
// assigned by createApp(); module level because the TLS SNI callback built in
// buildServer() logs through it
let loggelf;
function buildServer() {
const serverOptions = {
...baseServerOptions(),
// restify read bodies without a size limit (maxBodySize: 0)
bodyLimit: 1024 * 1024 * 1024
};
let certOptions = {};
certs.loadTLSOptions(certOptions, 'api');
if (config.api.secure && certOptions.key) {
let httpsServerOptions = {};
httpsServerOptions.key = certOptions.key;
httpsServerOptions.cert = tools.buildCertChain(certOptions.cert, certOptions.ca);
let defaultSecureContext = tls.createSecureContext(httpsServerOptions);
httpsServerOptions.SNICallback = (servername, cb) => {
const opts = {
servername,
meta: {}
};
certs
.getContextForServername(
opts.servername,
httpsServerOptions,
{
source: 'API',
...opts.meta
},
{
loggelf: message => loggelf(message)
}
)
.then(context => {
cb(null, context || defaultSecureContext);
})
.catch(err => cb(err));
};
serverOptions.https = httpsServerOptions;
}
const app = fastify(serverOptions);
// shared schema definitions, referenced from route response schemas via
// $ref and published to OpenAPI docs
for (const schema of sharedSchemas.values()) {
app.addSchema(schema);
}
// OpenAPI generation from the route schemas
app.register(fastifySwagger, {
openapi: {
openapi: apiDocsConfig.openapiVersion || '3.0.0',
info: { ...apiDocsConfig.info, version: packageData.version },
servers: apiDocsConfig.servers,
tags: apiDocsConfig.tags,
components: apiDocsConfig.components,
security: apiDocsConfig.security
},
refResolver: {
buildLocalReference(json, baseUri, fragment, i) {
return String(json.$id || `def-${i}`).replace(/^wd:/, '');
}
},
transformObject({ openapiObject }) {
return stripInternalKeywords(openapiObject);
}
});
// ---- body parsing (restify bodyParser equivalents) ----
app.removeAllContentTypeParsers();
const jsonParser = async (request, payload) => {
let data = payload;
if (Buffer.isBuffer(data)) {
data = data.toString('utf8');
}
if (!data || !data.trim()) {
// restify skipped parsing empty bodies
return undefined;
}
try {
return JSON.parse(data);
} catch (err) {
const parseError = new Error('Invalid JSON: ' + err.message);
parseError.responseCode = 400;
parseError.code = 'InvalidContent';
throw parseError;
}
};
app.addContentTypeParser('application/json', { parseAs: 'buffer' }, jsonParser);
// fastify matches the regex against the full header value including
// parameters, so 'application/report+json; charset=utf-8' must match too;
// the ^ anchor also keeps fastify from printing the FSTSEC001 warning
app.addContentTypeParser(/^application\/[a-z0-9._-]+\+json\b/i, { parseAs: 'buffer' }, jsonParser);
app.addContentTypeParser('application/x-www-form-urlencoded', { parseAs: 'buffer' }, async (request, payload) => {
try {
return qs.parse(payload.toString('utf8'));
} catch (err) {
err.responseCode = 400;
throw err;
}
});
// restify's bodyReader explicitly skipped application/octet-stream: the
// request stream stays unconsumed so handlers can pipe it themselves
// (POST /data/import does)
app.addContentTypeParser('application/octet-stream', async () => undefined);
// restify's bodyParser handed multipart/form-data to formidable and mapped
// form fields and uploaded file contents into req.params (mapParams +
// mapFiles); keyValues mode replicates that: fields arrive as strings and
// files as Buffers under their field names (POST /users/:user/storage).
// Parts buffer in memory, so cap them at the largest payload any consumer
// accepts (storage uploads, wdMaxBytes MAX_ALLOWED_MESSAGE_SIZE)
app.register(fastifyMultipart, {
attachFieldsToBody: 'keyValues',
limits: {
fieldSize: consts.MAX_ALLOWED_MESSAGE_SIZE,
// one byte of headroom over the wdMaxBytes ceiling the routes
// declare: an oversized upload then fails request validation with
// the documented 400 InputValidationError instead of being cut off
// by the parser with a bare 413 FST_REQ_FILE_TOO_LARGE
fileSize: consts.MAX_ALLOWED_MESSAGE_SIZE + 1
}
});
// everything else: Buffer for binary types, utf8 string for text/*
// (message/rfc822 uploads and similar raw payloads)
app.addContentTypeParser('*', { parseAs: 'buffer' }, async (request, payload) => {
const contentType = (request.headers['content-type'] || '').toLowerCase();
if (/^text\//.test(contentType)) {
return payload.toString('utf8');
}
return payload;
});
return app;
}
/**
* Builds the API application with every hook and route registered, without
* listening. Expects the database connections in lib/db to be open.
*
* Tests build one in-process and drive it with app.inject().
*
* @param {Object} [options]
* @param {Object} [options.contractViolations] Violation log (createViolationLog()); when set, every reply of a documented
* route is checked against its response model and each difference is recorded (see lib/fastify/response-contract.js)
* @returns {Object} Fastify instance
*/
function createApp(options = {}) {
let userHandler;
let mailboxHandler;
let messageHandler;
let storageHandler;
let auditHandler;
let settingsHandler;
let mcpTokenHandler;
let notifier;
let apnClient;
const component = config.log.gelf.component || 'wildduck';
const hostname = config.log.gelf.hostname || os.hostname();
const gelf =
config.log.gelf && config.log.gelf.enabled
? new Gelf(config.log.gelf.options)
: {
// placeholder
emit: (key, message) => log.info('Gelf', JSON.stringify(message))
};
loggelf = (message, requiredKeys = []) => {
if (typeof message === 'string') {
message = {
short_message: message
};
}
message = message || {};
normalizeLoggelfMessage(message);
if (!message.short_message || message.short_message.indexOf(component.toUpperCase()) !== 0) {
message.short_message = component.toUpperCase() + ' ' + (message.short_message || '');
}
message.facility = component; // facility is deprecated but set by the driver if not provided
message.host = hostname;
message.timestamp = Date.now() / 1000;
message._component = component;
Object.keys(message).forEach(key => {
if (!message[key] && !requiredKeys.includes(key)) {
// remove the key if it empty/falsy/undefined/null and it is not required to stay
delete message[key];
}
});
gelf.emit('gelf.log', message);
};
const app = buildServer();
// named route registry, served by the test-only /api-methods route and
// used by the test overview generator
const routeRegistry = {};
const corsOrigins = [].concat(config.api.cors.origins || ['*']);
app.register(fastifyCors, {
// `true` reflects the request Origin (and sets Vary: Origin) instead of
// sending a literal "*", which browsers reject outright when combined
// with credentials. restify-cors-middleware2 echoed the origin too
origin: corsOrigins.includes('*') ? true : corsOrigins,
methods: ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
// restify-cors-middleware2 always merged its own defaults into the
// configured lists (src/constants.js), so the full set has to be
// spelled out here; without content-type every cross-origin JSON
// request fails preflight
allowedHeaders: [
'X-Access-Token',
'Authorization',
'Accept',
'Accept-Version',
'Content-Type',
'Request-Id',
'Origin',
'X-Api-Version',
'X-Request-Id',
'X-Requested-With'
],
exposedHeaders: ['Api-Version', 'Content-Length', 'Content-Md5', 'Content-Type', 'Date', 'Request-Id', 'Response-Time'],
credentials: true
});
attachRequestDecorations(app);
attachReplyDecorations(app);
// Applies an MCP access level's field allowlist (set by the token check below) to whatever a
// route is about to answer. Most routes filter their own output already, but the message and
// mailbox routes do not, and adding a filter call to each of them would leave the next one to
// remember. preSerialization sees every object payload, from a handler, a hook or the error
// handler, and nothing else: a stream or a buffer is not a resource. Which bodies carry
// resource fields (an error body does not) is decided by roles.filterResponseBody. Registered
// ahead of the payload stash so the Gelf line logs what was actually sent.
app.decorateRequest('wdResponseFieldFilter', null);
app.addHook('preSerialization', async (request, reply, payload) => {
let filter = request.wdResponseFieldFilter;
return filter ? roles.filterResponseBody(filter.permission, payload, filter.listKey) : payload;
});
attachPayloadStash(app);
attachResponseHeaders(app, 'WildDuck API');
attachNativeRoutes(app, routeRegistry);
if (options.contractViolations) {
attachResponseContractCheck(app, finding => options.contractViolations.record(finding));
}
// public files (restify serveStatic joined the route path to the root
// directory, so the files live under public/public)
app.register(fastifyStatic, {
root: Path.join(__dirname, 'public', 'public'),
prefix: '/public/',
index: 'index.html'
});
// ---- access token check (previously a restify server.use middleware) ----
app.addHook('onRequest', async request => {
if (request.is404) {
return;
}
const routeConfig = (request.routeOptions && request.routeOptions.config) || {};
// routes opt out of the token check with config.public; the static
// files under /public/ are matched by URL prefix instead
if (routeConfig.public || request.url.startsWith('/public/')) {
request.wdIsPublic = true;
return;
}
// Where a credential arrived, resolved before the carriers are cleared below. The merge
// itself is unchanged, and deliberately loose, because that is what every other credential
// kind has always been read with.
let bearerToken = McpTokenHandler.getBearerToken(request.headers.authorization);
let misplacedMcpToken =
McpTokenHandler.isToken(request.query && request.query.accessToken) || McpTokenHandler.isToken(request.headers['x-access-token']);
let accessToken =
(request.query && request.query.accessToken) ||
request.headers['x-access-token'] ||
(request.headers.authorization ? request.headers.authorization.replace(/^Bearer\s+/i, '').trim() : false) ||
false;
if (request.query && request.query.accessToken) {
// delete or the strict routes would reject it as an unknown key
delete request.query.accessToken;
}
if (request.headers['x-access-token']) {
request.headers['x-access-token'] = '';
}
if (request.headers.authorization) {
request.headers.authorization = '';
}
let tokenRequired = false;
let fail = () => {
let error = new Error('Invalid accessToken value');
error.responseCode = 403;
error.code = 'InvalidToken';
throw error;
};
// An MCP token is a bearer credential and nothing else. A wdmcp_ value in a query string or
// an X-Access-Token header has already been written somewhere a credential does not belong,
// since a URL reaches proxy logs, browser history and referrer headers, so the request is
// refused even when the same token is also presented correctly. Serving it would teach a
// client that the unsafe carrier works. Refused ahead of every other credential, including
// the master token, so no combination of carriers can serve a request that also carried a
// wdmcp_ value where one does not belong.
if (misplacedMcpToken) {
return fail();
}
// hard coded master token
if (config.api.accessToken) {
tokenRequired = true;
if (config.api.accessToken === accessToken) {
request.role = 'root';
request.user = 'root';
return;
}
}
// Dedicated MCP tokens resolve to the access level stored on the token record, so what an
// agent may do is decided by config/roles.json like every other role. These are only ever
// presented by the MCP service over the private network; they are not API access tokens
// and carry none of their privileges.
//
// The bearer value has to be the one the merge selected as well, so that a token presented
// alongside an ordinary access token cannot shadow it: precedence between carriers is the
// same for every credential kind, and this branch does not get its own.
if (accessToken === bearerToken && McpTokenHandler.isToken(bearerToken)) {
tokenRequired = true;
// A role alone is too coarse to describe what an agent may reach. `read:own` on
// messages and users also covers the raw RFC822 source, the archive, the address
// register, the journal stream and PUT /users/:user/logout, which is a state change
// guarded by readOwn('users'). So the credential is additionally pinned to the exact
// routes the MCP tools dispatch to, plus self-revocation: adding a route under an
// existing grant cannot widen what an agent token reaches, and the read-only promise
// belongs to the credential rather than to the client that happens to be using it.
let mcpRoute = MCP_ROUTES.get(String(routeConfig.name || '').toLowerCase());
if (!mcpRoute || request.method !== mcpRoute.method) {
return fail();
}
let authenticated;
try {
// No address is passed, so no failure is counted here. The failure budget belongs
// to the MCP listener, which is the surface a guess can actually be aimed at; this
// caller has already authenticated there, and the `ip` param is supplied by the
// caller, so keying a limiter on it would let one dodge or poison another's budget.
// Every MCP token holder reaches this listener from the same socket, so a budget
// here would also let one of them spend everyone else's.
authenticated = await mcpTokenHandler.authenticate(bearerToken);
} catch {
return fail();
}
request.role = authenticated.role;
request.user = authenticated.user._id.toString();
if (mcpRoute.resource) {
// config/roles.json is meant to be the single declaration of what an agent may
// see, so the allowlist is enforced at the exit of the API (the preSerialization
// hook below) rather than in the MCP service that usually calls it. A level with
// no read grant for the resource has no allowlist to apply: refused rather than
// answered unfiltered, so the next entry added to MCP_ROUTES cannot leak.
let permission = roles.can(request.role).readOwn(mcpRoute.resource);
if (!permission.granted) {
return fail();
}
request.wdResponseFieldFilter = { permission, listKey: mcpRoute.listKey };
} else {
request.accessToken = {
revoke: () => mcpTokenHandler.revokeCurrent(bearerToken)
};
}
return;
}
if (config.api.accessControl.enabled || accessToken) {
tokenRequired = true;
if (accessToken && accessToken.length === 40 && /^[a-fA-F0-9]{40}$/.test(accessToken)) {
let tokenData;
let tokenHash = crypto.createHash('sha256').update(accessToken).digest('hex');
try {
let key = 'tn:token:' + tokenHash;
tokenData = await db.redis.hgetall(key);
} catch (err) {
err.responseCode = 500;
err.code = 'InternalDatabaseError';
throw err;
}
if (tokenData && tokenData.user && tokenData.role && config.api.roles[tokenData.role]) {
let signData;
if ('authVersion' in tokenData) {
// cast value to number
tokenData.authVersion = Number(tokenData.authVersion) || 0;
signData = {
token: accessToken,
user: tokenData.user,
authVersion: tokenData.authVersion,
role: tokenData.role
};
if ('mfaRequired' in tokenData || 'mfaVerified' in tokenData || 'passwordChangeRequired' in tokenData) {
signData.mfaRequired = tokenData.mfaRequired;
signData.mfaVerified = tokenData.mfaVerified;
signData.passwordChangeRequired = tokenData.passwordChangeRequired;
}
} else {
signData = {
token: accessToken,
user: tokenData.user,
role: tokenData.role
};
}
let signature = crypto.createHmac('sha256', config.api.accessControl.secret).update(JSON.stringify(signData)).digest('hex');
if (signature !== tokenData.s) {
// rogue token or invalidated secret
/*
// do not delete just in case there is something wrong with the check
try {
await db.redis.del('tn:token:' + tokenHash);
} catch (err) {
// ignore
}
*/
} else if (tokenData.ttl && !isNaN(tokenData.ttl) && Number(tokenData.ttl) > 0) {
let tokenTTL = Number(tokenData.ttl);
let tokenLifetime = config.api.accessControl.tokenLifetime || consts.ACCESS_TOKEN_MAX_LIFETIME;
// check if token is not too old
if ((Date.now() - Number(tokenData.created)) / 1000 < tokenLifetime) {
let assuranceRecorded = 'mfaRequired' in tokenData && 'mfaVerified' in tokenData && 'passwordChangeRequired' in tokenData;
let mfaRequired = assuranceRecorded && tokenData.mfaRequired === 'true';
let mfaVerified = assuranceRecorded && tokenData.mfaVerified === 'true';
// set when a later 2FA completion verified this token through the side key
let mfaProofVerified = false;
if (mfaRequired && !mfaVerified) {
try {
let proof = await db.redis.get('tn:token:mfa:' + tokenHash);
mfaProofVerified = proof === userHandler.getAuthTokenMfaProof(tokenHash, tokenData.user, tokenData.authVersion);
} catch {
// treat as unverified
}
mfaVerified = mfaProofVerified;
}
// token is still usable, increase session length
try {
let refresh = db.redis.multi().expire('tn:token:' + tokenHash, tokenTTL);
if (mfaProofVerified) {
refresh.expire('tn:token:mfa:' + tokenHash, tokenTTL);
}
await refresh.exec();
} catch (err) {
// ignore
}
request.role = tokenData.role;
request.user = tokenData.user;
// make a reference to original method, otherwise might be overridden
let setAuthToken = userHandler.setAuthToken.bind(userHandler);
request.accessToken = {
hash: tokenHash,
user: tokenData.user,
authVersion: tokenData.authVersion,
assuranceRecorded,
mfaRequired,
mfaVerified,
passwordChangeRequired: assuranceRecorded && tokenData.passwordChangeRequired === 'true',
// keeps the session valid after its own account changed the password or 2FA
// setup (which bumps authVersion); a change of another account leaves it alone
update: async changedUser =>
String(changedUser) === tokenData.user &&
setAuthToken(tokenData.user, accessToken, {
role: tokenData.role,
mfaRequired,
mfaVerified,
passwordChangeRequired: false
})
};
} else {
// expired token, clear it
try {
await db.redis.del('tn:token:' + tokenHash);
} catch (err) {
// ignore
}
}
} else {
request.role = tokenData.role;
request.user = tokenData.user;
}
if (!request.role) {
return fail();
}
if (/^[0-9a-f]{24}$/i.test(request.user)) {
let tokenAuthVersion = Number(tokenData.authVersion) || 0;
let userData = await db.users.collection('users').findOne(
{
_id: new ObjectId(request.user)
},
{ projection: { authVersion: true, disabled: true, suspended: true } }
);
let userAuthVersion = Number(userData && userData.authVersion) || 0;
if (!userData || tokenAuthVersion < userAuthVersion) {
// unknown user or expired session
return fail();
}
if (userData.disabled || userData.suspended) {
// locked out account, existing tokens must not keep working
return fail();
}
}
// pass
return;
}
}
}
if (tokenRequired) {
// no valid token found
return fail();
}
// allow all
request.role = 'root';
request.user = 'root';
});
// a token redundantly included in the request body must never reach
// validation (it is not a declared field) nor the logs. The body is not
// parsed yet in onRequest, so this runs as an instance level preValidation
// hook, which fastify runs before the route's own params merge
app.addHook('preValidation', async request => {
if (request.is404 || request.wdIsPublic) {
return;
}
if (request.body && typeof request.body === 'object' && !Array.isArray(request.body) && !Buffer.isBuffer(request.body) && request.body.accessToken) {
delete request.body.accessToken;
}
});
// ---- metrics timing (previously a restify server.use middleware) ----
// ---- Gelf HTTP logging (previously done inside the restify JSON formatter) ----
app.addHook('onResponse', async (request, reply) => {
const body = reply.wdResponseBody;
if (!body || typeof body !== 'object') {
return;
}
// only documented API routes produce a gelf line; infra routes
// (metrics, the OpenAPI document, /api-methods) declare no
// validationObjs and are not API responses
const routeConfig = (request.routeOptions && request.routeOptions.config) || {};
if (!routeConfig.validationObjs) {
return;
}
// fastify has already computed the body size for content-length
const size = Number(reply.getHeader('content-length')) || 0;
const params = requestParams(request);
let path = (request.routeOptions && request.routeOptions.url) || maskUrl(request.url);
let message = {
short_message: 'HTTP [' + request.method + ' ' + path + '] ' + (body.success ? 'OK' : 'FAILED'),
_req_remoteAddress: request.headers['x-forwarded-for'] || request.raw.socket.remoteAddress,
_ip: ((params && params.ip) || '').toString().substr(0, 40) || '',
_sess: ((params && params.sess) || '').toString().substr(0, 40) || '',
_http_route: path,
_http_method: request.method,
_user: request.user,
_role: request.role,
_api_response: body.success ? 'success' : 'fail',
_error: body.error,
_code: body.code,
_size: size
};
Object.keys(params || {}).forEach(key => {
let value = params[key];
if (!value && value !== 0) {
// if falsy don't continue, allow 0 integer as value
return;
}
// cast value to string if not string
value = typeof value === 'string' ? value : util.inspect(value, INSPECT_OPTIONS).trim();
if (['password', 'existingPassword', 'accessToken'].includes(key)) {
value = '***';
} else if (value.length > 128) {
value = value.substr(0, 128) + '…';
}
if (key.length > 30) {
key = key.substr(0, 30) + '…';
}
if (key === 'sendTime') {
try {
value = new Date(value).toISOString();
} catch {
// ignore
}
}
message['_req_' + key] = value;
});
if (typeof body.id !== 'undefined') {
let value = typeof body.id === 'string' ? body.id : util.inspect(body.id, INSPECT_OPTIONS).trim();
message._res_id = value.length > 128 ? value.substr(0, 128) + '…' : value;
}
loggelf(message);
});
attachAccessLog(app, 'API', { includeUser: true, serverName: 'WildDuck API' });
attachErrorHandler(app, 'API');
app.setNotFoundHandler((request, reply) => {
const body = {
code: 'ResourceNotFound',
message: `${maskUrl(request.url)} does not exist`
};
reply.status(404);
reply.wdContentType = 'application/json';
return reply.send(body);
});
// ---- handlers and routes ----
settingsHandler = new SettingsHandler({ db: db.database });
notifier = new ImapNotifier({
database: db.database,
redis: db.redis,
settingsHandler
});
apnClient = ApnClient.get({ config: config.imap && config.imap.aps, database: db.database, loggelf: message => loggelf(message) });
messageHandler = new MessageHandler({
database: db.database,
users: db.users,
redis: db.redis,
gridfs: db.gridfs,
attachments: config.attachments,
settingsHandler,
apn: apnClient,
loggelf: message => loggelf(message)
});
storageHandler = new StorageHandler({
database: db.database,
users: db.users,
gridfs: db.gridfs,
loggelf: message => loggelf(message)
});
userHandler = new UserHandler({
database: db.database,
users: db.users,
redis: db.redis,
messageHandler,
loggelf: message => loggelf(message)
});
// Built after userHandler so a failed MCP authentication here reaches the same authlog it
// would through the MCP listener. Without the binding this path failed silently, and the
// user saw a different history depending on which listener the token hit.
//
// Successes are left to the MCP listener, which is the hop that sees the client. This one
// re-checks the same credential on each request that listener makes on a caller's behalf,
// so recording them here would name the internal address and add two awaited round trips
// to every one of those requests.
mcpTokenHandler = new McpTokenHandler({
users: db.users,
redis: db.redis,
counters: userHandler.counters,
logAuthEvent: userHandler.logAuthEvent.bind(userHandler),
logSuccessfulAuth: false
});
mailboxHandler = new MailboxHandler({
database: db.database,
users: db.users,
redis: db.redis,
notifier,
settingsHandler,
loggelf: message => loggelf(message)
});
auditHandler = new AuditHandler({
database: db.database,
users: db.users,
gridfs: db.gridfs,
bucket: 'audit',
loggelf: message => loggelf(message)
});
// route modules read these off the fastify instance
app.decorate('loggelf', (message, requiredKeys = []) => loggelf(message, requiredKeys));
app.decorate(
'lock',
new Lock({
redis: db.redis,
namespace: 'mail'
})
);
// route modules load in a sibling plugin context so they boot after the
// swagger plugin (its onRoute hook only sees routes registered later)
app.register(async () => {
acmeRoutes(db, app);
usersRoutes(db, app, userHandler, settingsHandler);
addressesRoutes(db, app, userHandler, settingsHandler);
mailboxesRoutes(db, app, mailboxHandler);
messagesRoutes(db, app, messageHandler, userHandler, storageHandler, settingsHandler);
storageRoutes(db, app, storageHandler);
filtersRoutes(db, app, userHandler, settingsHandler);
domainaccessRoutes(db, app);
aspsRoutes(db, app, userHandler);
totpRoutes(db, app, userHandler, mcpTokenHandler);
custom2faRoutes(db, app, userHandler);
webauthnRoutes(db, app, userHandler, mcpTokenHandler);
updatesRoutes(db, app, notifier);
authRoutes(db, app, userHandler, mcpTokenHandler);
autoreplyRoutes(db, app);
submitRoutes(db, app, messageHandler, userHandler, settingsHandler);
auditRoutes(db, app, auditHandler);
domainaliasRoutes(db, app);
dkimRoutes(db, app);
certsRoutes(db, app);
webhooksRoutes(db, app);
pushsubscriptionsRoutes(db, app, apnClient);
settingsRoutes(db, app, settingsHandler);
healthRoutes(db, app, loggelf);
mcpTokensRoutes(app, mcpTokenHandler);
});
if (process.env.NODE_ENV === 'test') {
app.get('/api-methods', { config: { name: 'api-methods' } }, async () => routeRegistry);
if (options.contractViolations) {
// what the response contract check found so far (paths and kinds, no values)
app.get('/api-contract-violations', { config: { name: 'api-contract-violations' } }, async () => options.contractViolations.list());
}
}
// the specification is static once the routes are registered, build once
let openApiDocsCache = null;
app.get(apiDocsConfig.docsPath || '/docs/api/openapidocs.json', { config: { name: 'openapidocs' }, schema: { hide: true } }, async () => {
if (!openApiDocsCache) {
openApiDocsCache = app.swagger();
}
return openApiDocsCache;
});
return app;
}
/**
* Starts the API server
*
* @returns {Promise<Object|false>} The fastify instance, or false if the API is disabled
*/
module.exports = async () => {
if (!config.api.enabled) {
metrics.setServiceUp('api', false);
return false;
}
// the test server checks every reply against its response model, the API
// test suite reads the findings from /api-contract-violations at the end
const app = createApp(process.env.NODE_ENV === 'test' ? { contractViolations: createViolationLog() } : {});
if (process.env.GENERATE_API_DOCS === 'true') {
await app.ready();
try {
fs.writeFileSync(Path.join(__dirname, 'docs', 'api', 'openapidocs.json'), JSON.stringify(app.swagger(), null, 4));
log.info('API', 'Generated OpenAPI docs to docs/api/openapidocs.json');
} catch (err) {
log.error('API', 'Failed to generate OpenAPI docs: %s', err.message);
}
}
if (process.env.REGENERATE_API_DOCS === 'true') {
// allow 2.5 seconds for services to start and the api doc to be generated, after that exit process
setTimeout(() => process.exit(0), 2500);
}
await app.listen({ port: config.api.port, host: config.api.host || '0.0.0.0' });
metrics.setServiceUp('api', true);
log.info('API', 'Server listening on %s:%s', config.api.host || '0.0.0.0', config.api.port);
return app;
};
module.exports.createApp = createApp;