forked from kriiv/inbound-email
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
419 lines (356 loc) · 13.4 KB
/
server.js
File metadata and controls
419 lines (356 loc) · 13.4 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
const SMTPServer = require('smtp-server').SMTPServer;
const config = require('./config');
const { parseEmail } = require('./services/emailParser');
const { sendToWebhook } = require('./services/webhookService');
const Queue = require('better-queue');
const logger = require('./services/logger');
const { isRecoverableNetworkError, serializeError } = require('./services/errorClassifier');
const DurableQueue = require('./services/durableQueue');
const { SlidingWindowRateLimiter } = require('./services/rateLimiter');
const {
normalizeIp,
isIpAllowed,
isSenderDomainAllowed,
hasRequiredAuthResults
} = require('./services/emailSecurity');
// Validate configuration BEFORE any component initialization
function validateConfig() {
const errors = [];
// Check required keys
if (!config.WEBHOOK_URL && !config.WEBHOOK_RULES) {
errors.push('Either WEBHOOK_URL or WEBHOOK_RULES must be configured');
}
if (typeof config.PORT !== 'number' || config.PORT <= 0) {
errors.push('PORT must be a valid positive number');
}
if (typeof config.WEBHOOK_CONCURRENCY !== 'number' || config.WEBHOOK_CONCURRENCY <= 0) {
errors.push('WEBHOOK_CONCURRENCY must be a valid positive number');
}
if (typeof config.WEBHOOK_RETRY_DELAY_MS !== 'number' || config.WEBHOOK_RETRY_DELAY_MS <= 0) {
errors.push('WEBHOOK_RETRY_DELAY_MS must be a valid positive number');
}
if (typeof config.SMTP_MAX_CLIENTS !== 'number' || config.SMTP_MAX_CLIENTS <= 0) {
errors.push('SMTP_MAX_CLIENTS must be a valid positive number');
}
if (typeof config.SMTP_SOCKET_TIMEOUT !== 'number' || config.SMTP_SOCKET_TIMEOUT <= 0) {
errors.push('SMTP_SOCKET_TIMEOUT must be a valid positive number');
}
if (typeof config.SMTP_CLOSE_TIMEOUT !== 'number' || config.SMTP_CLOSE_TIMEOUT <= 0) {
errors.push('SMTP_CLOSE_TIMEOUT must be a valid positive number');
}
if (typeof config.SMTP_MAX_MESSAGE_SIZE !== 'number' || config.SMTP_MAX_MESSAGE_SIZE <= 0) {
errors.push('SMTP_MAX_MESSAGE_SIZE must be a valid positive number');
}
if (typeof config.SMTP_RATE_LIMIT_WINDOW_MS !== 'number' || config.SMTP_RATE_LIMIT_WINDOW_MS <= 0) {
errors.push('SMTP_RATE_LIMIT_WINDOW_MS must be a valid positive number');
}
if (typeof config.SMTP_RATE_LIMIT_MAX_CONNECTIONS !== 'number' || config.SMTP_RATE_LIMIT_MAX_CONNECTIONS <= 0) {
errors.push('SMTP_RATE_LIMIT_MAX_CONNECTIONS must be a valid positive number');
}
if (typeof config.MAX_QUEUE_SIZE !== 'number' || config.MAX_QUEUE_SIZE <= 0) {
errors.push('MAX_QUEUE_SIZE must be a valid positive number');
}
if (config.REQUIRE_TRUSTED_RELAY && config.TRUSTED_RELAY_IPS.length === 0) {
errors.push('TRUSTED_RELAY_IPS must be configured when REQUIRE_TRUSTED_RELAY=true');
}
if (config.REQUIRED_AUTH_RESULTS.length > 0 && config.TRUSTED_RELAY_IPS.length === 0) {
errors.push('TRUSTED_RELAY_IPS must be configured when REQUIRED_AUTH_RESULTS is used');
}
if (process.env.NODE_ENV === 'production') {
if (!config.REQUIRE_TRUSTED_RELAY) {
errors.push('REQUIRE_TRUSTED_RELAY must be true in production');
}
if (config.TRUSTED_RELAY_IPS.length === 0) {
errors.push('TRUSTED_RELAY_IPS must be configured in production');
}
if (config.ALLOWED_RECIPIENT_DOMAINS.length === 0) {
errors.push('ALLOWED_RECIPIENT_DOMAINS must be configured in production');
}
if (!config.WEBHOOK_SECRET) {
errors.push('WEBHOOK_SECRET must be configured in production');
}
if (config.ALLOW_INSECURE_WEBHOOK_HTTP) {
errors.push('ALLOW_INSECURE_WEBHOOK_HTTP must be false in production');
}
}
// Check TLS configuration if secure mode is enabled
if (config.SMTP_SECURE && config.TLS && config.TLS.error) {
errors.push(config.TLS.error);
}
if (errors.length > 0) {
throw new Error(`Configuration errors:\n - ${errors.join('\n - ')}`);
}
}
// Validate configuration immediately
try {
validateConfig();
} catch (error) {
logger.error('Configuration error:', { message: error.message });
process.exit(1);
}
const durableQueue = new DurableQueue(config.DURABLE_QUEUE_PATH);
const connectionRateLimiter = new SlidingWindowRateLimiter({
windowMs: config.SMTP_RATE_LIMIT_WINDOW_MS,
maxHits: config.SMTP_RATE_LIMIT_MAX_CONNECTIONS
});
function createSmtpError(message, responseCode = 451) {
const error = new Error(message);
error.responseCode = responseCode;
return error;
}
const webhookQueue = new Queue(async function (task, cb) {
const { taskId } = task;
if (!taskId) {
cb(new Error('Missing durable task id'));
return;
}
let queueItem;
try {
queueItem = await durableQueue.get(taskId);
} catch (error) {
if (error.code === 'ENOENT') {
logger.warn('Durable queue item no longer exists, skipping', { taskId });
cb();
return;
}
cb(error);
return;
}
const { parsed } = queueItem;
let failedWebhooks = queueItem.failedWebhooks || null;
const maxRetries = 3;
let retries = 0;
const attemptWebhook = async () => {
try {
const result = await sendToWebhook(parsed, failedWebhooks);
await durableQueue.remove(taskId);
logger.info('Successfully sent to webhook', {
taskId,
successful: result.successful,
failed: result.failed
});
cb(null, result);
} catch (error) {
logger.error('Webhook error:', { taskId, message: error.message, stack: error.stack });
if (Array.isArray(error.failedWebhooks) && error.failedWebhooks.length > 0) {
failedWebhooks = error.failedWebhooks;
}
retries++;
if (retries < maxRetries) {
logger.info(`Retrying webhook (attempt ${retries}/${maxRetries})`);
// Use exponential backoff
const delay = Math.min(1000 * Math.pow(2, retries - 1), 10000);
setTimeout(() => {
attemptWebhook().catch(err => cb(err));
}, delay);
} else {
try {
await durableQueue.update(taskId, {
failedWebhooks,
lastError: error.message,
attempts: (queueItem.attempts || 0) + retries
});
} catch (persistError) {
logger.error('Failed to persist queue failure state', {
taskId,
message: persistError.message
});
}
const retryTimer = setTimeout(() => {
webhookQueue.push({ taskId });
}, config.WEBHOOK_RETRY_DELAY_MS);
retryTimer.unref();
logger.warn('Queued durable task for delayed retry', {
taskId,
delayMs: config.WEBHOOK_RETRY_DELAY_MS
});
cb(null, { queuedForRetry: true });
}
}
};
attemptWebhook().catch(err => cb(err));
}, { concurrent: config.WEBHOOK_CONCURRENCY });
// Build SMTP server options
const smtpOptions = {
onConnect(session, callback) {
const remoteIp = normalizeIp(session.remoteAddress);
if (!isIpAllowed(remoteIp, config.ALLOWED_SMTP_CLIENTS)) {
logger.warn('Rejected SMTP client not in ALLOWED_SMTP_CLIENTS', { remoteIp });
return callback(createSmtpError('SMTP client not allowed', 550));
}
if (config.REQUIRE_TRUSTED_RELAY && !isIpAllowed(remoteIp, config.TRUSTED_RELAY_IPS)) {
logger.warn('Rejected SMTP client not in TRUSTED_RELAY_IPS', { remoteIp });
return callback(createSmtpError('SMTP relay is not trusted', 550));
}
if (!connectionRateLimiter.isAllowed(remoteIp)) {
logger.warn('Rate limit exceeded for SMTP client', { remoteIp });
return callback(createSmtpError('Too many requests, try again later', 421));
}
callback();
},
onMailFrom(address, session, callback) {
if (!isSenderDomainAllowed(address.address, config.ALLOWED_SENDER_DOMAINS)) {
const domain = address.address?.split('@')?.[1] || 'invalid';
logger.warn('Rejected sender domain not in ALLOWED_SENDER_DOMAINS', { sender: address.address, domain });
return callback(createSmtpError('Sender domain not allowed', 553));
}
callback();
},
onRcptTo(address, session, callback) {
const recipient = address.address;
if (!config.isRecipientDomainAllowed(recipient)) {
const domain = recipient.split('@')[1] || 'invalid';
logger.warn('Rejected email to unauthorized domain', { recipient, domain });
return callback(createSmtpError(`Recipient domain not allowed: ${domain}`, 553));
}
callback();
},
onData(stream, session, callback) {
if (webhookQueue.getStats().total >= config.MAX_QUEUE_SIZE) {
logger.warn('Rejected email because queue is full', { maxQueueSize: config.MAX_QUEUE_SIZE });
stream.on('error', () => false);
stream.on('end', () => callback(createSmtpError('Server busy, please retry later', 451)));
stream.resume();
return;
}
parseEmail(stream)
.then(async (parsed) => {
const remoteIp = normalizeIp(session.remoteAddress);
const fromTrustedRelay = isIpAllowed(remoteIp, config.TRUSTED_RELAY_IPS);
if (config.REQUIRE_TRUSTED_RELAY && !fromTrustedRelay) {
throw createSmtpError('SMTP relay is not trusted', 550);
}
if (config.REQUIRED_AUTH_RESULTS.length > 0) {
if (!fromTrustedRelay) {
throw createSmtpError('Authentication results require a trusted relay', 550);
}
if (!hasRequiredAuthResults(parsed, config.REQUIRED_AUTH_RESULTS)) {
throw createSmtpError('Email failed authentication policy checks', 550);
}
}
const taskId = await durableQueue.create({
parsed,
failedWebhooks: null
});
webhookQueue.push({ taskId });
logger.info('Email added to durable queue', { queueSize: webhookQueue.getStats().total, taskId });
callback();
})
.catch(error => {
logger.error('Parsing or policy error:', { message: error.message, stack: error.stack });
if (error.responseCode) {
callback(error);
return;
}
callback(createSmtpError('Failed to parse email', 451));
});
},
disabledCommands: ['AUTH'],
secure: config.SMTP_SECURE,
maxClients: config.SMTP_MAX_CLIENTS,
socketTimeout: config.SMTP_SOCKET_TIMEOUT,
closeTimeout: config.SMTP_CLOSE_TIMEOUT,
size: config.SMTP_MAX_MESSAGE_SIZE
};
// Add TLS options if secure mode is enabled
if (config.SMTP_SECURE && config.TLS && !config.TLS.error) {
smtpOptions.key = config.TLS.key;
smtpOptions.cert = config.TLS.cert;
}
const server = new SMTPServer(smtpOptions);
server.on('error', (error) => {
const errorData = serializeError(error);
if (isRecoverableNetworkError(error)) {
logger.warn('Recoverable SMTP connection error', errorData);
return;
}
logger.error('SMTP server error:', errorData);
});
async function replayDurableQueue() {
const taskIds = await durableQueue.listIds();
if (taskIds.length === 0) {
return;
}
for (const taskId of taskIds) {
webhookQueue.push({ taskId });
}
logger.info('Replayed durable queue tasks on startup', { count: taskIds.length });
}
async function startServer() {
try {
await durableQueue.ensureInitialized();
await replayDurableQueue();
} catch (error) {
logger.error('Failed to initialize durable queue:', { message: error.message, stack: error.stack });
process.exit(1);
return;
}
server.listen(config.PORT, '0.0.0.0', err => {
if (err) {
logger.error('Failed to start SMTP server:', { message: err.message, stack: err.stack });
process.exit(1);
}
logger.info(`SMTP server listening on port ${config.PORT} on all interfaces`);
});
}
startServer().catch((error) => {
logger.error('Unexpected startup failure:', { message: error.message, stack: error.stack });
process.exit(1);
});
let isShuttingDown = false;
function gracefulShutdown(reason) {
if (isShuttingDown) {
return;
}
isShuttingDown = true;
logger.info(`Shutting down: ${reason}`);
// Stop accepting new connections
server.close(() => {
logger.info('SMTP server closed, waiting for queue to drain...');
// Check if queue is empty
const checkQueue = () => {
const stats = webhookQueue.getStats();
if (stats.total === 0) {
logger.info('Queue drained. Exiting process.');
process.exit(0);
} else {
logger.info(`Waiting for ${stats.total} items in queue...`);
setTimeout(checkQueue, 1000);
}
};
// Give queue 30 seconds max to drain
const forceExitTimeout = setTimeout(() => {
const stats = webhookQueue.getStats();
logger.warn(`Force exiting with ${stats.total} items still in queue`);
process.exit(1);
}, 30000);
forceExitTimeout.unref();
checkQueue();
});
}
process.on('uncaughtException', (err) => {
const errorData = serializeError(err);
if (isRecoverableNetworkError(err)) {
logger.warn('Recoverable uncaught exception ignored', errorData);
return;
}
logger.error('Uncaught exception:', errorData);
gracefulShutdown('Uncaught exception');
});
process.on('unhandledRejection', (reason, promise) => {
const reasonData = serializeError(reason);
if (isRecoverableNetworkError(reason)) {
logger.warn('Recoverable unhandled rejection ignored', reasonData);
return;
}
logger.error('Unhandled Rejection:', { reason: reasonData, promise: String(promise) });
gracefulShutdown('Unhandled rejection');
});
process.on('SIGTERM', () => {
gracefulShutdown('SIGTERM signal received');
});
process.on('SIGINT', () => {
gracefulShutdown('SIGINT signal received');
});
// Export for testing
module.exports = { server, webhookQueue };