@getstrata/core 2.2.47 → 2.2.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/dist/core/audit/exportStore.d.ts +17 -0
- package/dist/core/auth/membershipService.d.ts +3 -23
- package/dist/core/auth/membershipServiceImplementation.d.ts +22 -0
- package/dist/core/auth/resolveMembershipService.d.ts +1 -1
- package/dist/core/cache/redisCacheScript.d.ts +2 -0
- package/dist/core/contracts/container.d.ts +2 -0
- package/dist/core/database/baseRepository.d.ts +3 -21
- package/dist/core/database/boundConnection.d.ts +1 -1
- package/dist/core/database/bunSql.d.ts +1 -1
- package/dist/core/database/connection.d.ts +1 -1
- package/dist/core/database/connectionContracts.d.ts +21 -0
- package/dist/core/database/defaultConnection.d.ts +1 -1
- package/dist/core/database/index.d.ts +1 -1
- package/dist/core/database/isolatedReadOnlyObservation.d.ts +1 -1
- package/dist/core/database/model.d.ts +6 -113
- package/dist/core/database/modelAttributes.d.ts +17 -0
- package/dist/core/database/modelQuery.d.ts +139 -0
- package/dist/core/database/modelRegistry.d.ts +34 -0
- package/dist/core/database/mysqlConnection.d.ts +1 -1
- package/dist/core/database/queryProxy.d.ts +1 -1
- package/dist/core/database/relationQueries.d.ts +20 -0
- package/dist/core/database/repositoryConnection.d.ts +1 -1
- package/dist/core/database/sqliteConnection.d.ts +1 -1
- package/dist/core/database/transaction.d.ts +1 -1
- package/dist/core/http/middleware.d.ts +2 -2
- package/dist/core/http/routeMiddleware.d.ts +1 -1
- package/dist/core/mail/mailer.d.ts +1 -4
- package/dist/core/mail/smtpMessage.d.ts +7 -0
- package/dist/core/queue/redisListScripts.d.ts +5 -0
- package/dist/core/queue/redisStreamsScripts.d.ts +11 -0
- package/dist/core/scheduler/leaseScript.d.ts +2 -0
- package/dist/entries/audit/exportAuditLogs.js +63 -58
- package/dist/entries/auth/basicAuthGuard.js +14 -2
- package/dist/entries/auth/jwtGuard.js +14 -2
- package/dist/entries/auth/membershipService.js +13 -14
- package/dist/entries/auth/scimAuthMiddleware.js +34 -20
- package/dist/entries/auth/sessionGuard.js +14 -2
- package/dist/entries/cache/createCacheStore.js +2 -1
- package/dist/entries/config/envSchema.js +2 -1
- package/dist/entries/contracts/container.js +14 -2
- package/dist/entries/contracts/serviceTokens.js +14 -2
- package/dist/entries/facades.js +103 -101
- package/dist/entries/http/routeMiddleware.js +3 -3
- package/dist/entries/jobs/exportAuditLogsJob.js +63 -58
- package/dist/entries/mail/mailer.js +103 -101
- package/dist/entries/queue/createAppQueue.js +277 -271
- package/dist/entries/queue/publicQueue.js +277 -271
- package/dist/entries/queue/queueMetrics.js +277 -271
- package/dist/entries/queue/redisQueue.js +277 -271
- package/dist/framework/public-api.d.ts +1 -1
- package/dist/index.js +1603 -1535
- package/package.json +1 -1
package/dist/entries/facades.js
CHANGED
|
@@ -97,30 +97,7 @@ function sdkClientClassName() {
|
|
|
97
97
|
return fromName ? `${fromName}Client` : "AppClient";
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
// ../../src/core/mail/
|
|
101
|
-
function resolveSmtpConfig() {
|
|
102
|
-
const host = process.env.MAIL_HOST?.trim();
|
|
103
|
-
if (!host) {
|
|
104
|
-
throw new Error('MAIL_DRIVER="smtp" requires MAIL_HOST to be set.');
|
|
105
|
-
}
|
|
106
|
-
const from = process.env.MAIL_FROM?.trim();
|
|
107
|
-
if (!from) {
|
|
108
|
-
throw new Error('MAIL_DRIVER="smtp" requires MAIL_FROM to be set.');
|
|
109
|
-
}
|
|
110
|
-
const port = Number(process.env.MAIL_PORT ?? "587");
|
|
111
|
-
if (!Number.isInteger(port) || port <= 0 || port > 65535) {
|
|
112
|
-
throw new Error('Environment variable "MAIL_PORT" must be a positive integer.');
|
|
113
|
-
}
|
|
114
|
-
return {
|
|
115
|
-
host,
|
|
116
|
-
port,
|
|
117
|
-
from,
|
|
118
|
-
secure: (process.env.MAIL_SECURE ?? "false") === "true",
|
|
119
|
-
...process.env.MAIL_TIMEOUT_MS !== undefined ? { timeoutMs: smtpTimeout(Number(process.env.MAIL_TIMEOUT_MS)) } : {},
|
|
120
|
-
...process.env.MAIL_USERNAME?.trim() ? { username: process.env.MAIL_USERNAME.trim() } : {},
|
|
121
|
-
...process.env.MAIL_PASSWORD?.trim() ? { password: process.env.MAIL_PASSWORD.trim() } : {}
|
|
122
|
-
};
|
|
123
|
-
}
|
|
100
|
+
// ../../src/core/mail/smtpMessage.ts
|
|
124
101
|
var EMAIL_SHAPE = /^[^\s@\r\n]+@[^\s@\r\n]+\.[^\s@\r\n]+$/u;
|
|
125
102
|
function assertSafeSmtpField(value, field) {
|
|
126
103
|
if (/[\r\n]/u.test(value)) {
|
|
@@ -146,12 +123,111 @@ function assertSafeSmtpAddress(value, field) {
|
|
|
146
123
|
smtpEnvelopeAddress(value, field);
|
|
147
124
|
return assertSafeSmtpField(value, field).trim();
|
|
148
125
|
}
|
|
149
|
-
function redactMailBody(body) {
|
|
150
|
-
return body.replace(/https?:\/\/[^\s]+/giu, "[redacted-url]").replace(/[?&](token|signature|expires|code)=[^&\s]+/giu, "[redacted-query]");
|
|
151
|
-
}
|
|
152
126
|
function encodeBase64(value) {
|
|
153
127
|
return Buffer.from(value, "utf8").toString("base64");
|
|
154
128
|
}
|
|
129
|
+
function encodeSmtpHeader(value) {
|
|
130
|
+
if (/^[\x20-\x7e]{0,60}$/u.test(value))
|
|
131
|
+
return value;
|
|
132
|
+
const words = [];
|
|
133
|
+
let part = "";
|
|
134
|
+
for (const character of value) {
|
|
135
|
+
if (Buffer.byteLength(part + character, "utf8") > 45) {
|
|
136
|
+
words.push(`=?UTF-8?B?${encodeBase64(part)}?=`);
|
|
137
|
+
part = "";
|
|
138
|
+
}
|
|
139
|
+
part += character;
|
|
140
|
+
}
|
|
141
|
+
if (part)
|
|
142
|
+
words.push(`=?UTF-8?B?${encodeBase64(part)}?=`);
|
|
143
|
+
return words.join(`\r
|
|
144
|
+
`);
|
|
145
|
+
}
|
|
146
|
+
function smtpAddressHeader(value) {
|
|
147
|
+
if (!value.includes("<"))
|
|
148
|
+
return value;
|
|
149
|
+
const name = value.slice(0, value.lastIndexOf("<")).trim();
|
|
150
|
+
const address = smtpEnvelopeAddress(value, "address");
|
|
151
|
+
if (!name)
|
|
152
|
+
return address;
|
|
153
|
+
const encoded = encodeSmtpHeader(name);
|
|
154
|
+
const display = encoded.startsWith("=?UTF-8?B?") ? encoded : `"${encoded.replace(/[\\"]/gu, "\\$&")}"`;
|
|
155
|
+
return `${display} <${address}>`;
|
|
156
|
+
}
|
|
157
|
+
function smtpMimePart(type, value) {
|
|
158
|
+
const normalized = value.replace(/\r\n|\r|\n/gu, `\r
|
|
159
|
+
`);
|
|
160
|
+
const base64 = /[^\t\r\n\x20-\x7e]/u.test(normalized) || normalized.split(`\r
|
|
161
|
+
`).some((line) => line.length > 998);
|
|
162
|
+
return [
|
|
163
|
+
`Content-Type: ${type}; charset=utf-8`,
|
|
164
|
+
`Content-Transfer-Encoding: ${base64 ? "base64" : "7bit"}`,
|
|
165
|
+
"",
|
|
166
|
+
base64 ? (encodeBase64(normalized).match(/.{1,76}/gu) ?? []).join(`\r
|
|
167
|
+
`) : normalized
|
|
168
|
+
];
|
|
169
|
+
}
|
|
170
|
+
function buildSmtpPayload(from, message) {
|
|
171
|
+
const headers = [
|
|
172
|
+
`From: ${smtpAddressHeader(assertSafeSmtpAddress(from, "from"))}`,
|
|
173
|
+
`To: ${smtpAddressHeader(assertSafeSmtpAddress(message.to, "to"))}`,
|
|
174
|
+
`Subject: ${encodeSmtpHeader(assertSafeSmtpField(message.subject, "subject"))}`,
|
|
175
|
+
"MIME-Version: 1.0"
|
|
176
|
+
];
|
|
177
|
+
let lines;
|
|
178
|
+
if (message.html) {
|
|
179
|
+
let boundary;
|
|
180
|
+
do {
|
|
181
|
+
boundary = `strata-${crypto.randomUUID()}`;
|
|
182
|
+
} while (message.body.includes(boundary) || message.html.includes(boundary));
|
|
183
|
+
lines = [
|
|
184
|
+
...headers,
|
|
185
|
+
`Content-Type: multipart/alternative; boundary="${boundary}"`,
|
|
186
|
+
"",
|
|
187
|
+
`--${boundary}`,
|
|
188
|
+
...smtpMimePart("text/plain", message.body),
|
|
189
|
+
`--${boundary}`,
|
|
190
|
+
...smtpMimePart("text/html", message.html),
|
|
191
|
+
`--${boundary}--`,
|
|
192
|
+
""
|
|
193
|
+
];
|
|
194
|
+
} else
|
|
195
|
+
lines = [...headers, ...smtpMimePart("text/plain", message.body)];
|
|
196
|
+
const content = lines.join(`\r
|
|
197
|
+
`).replace(/(^|\r\n)\./gu, "$1..");
|
|
198
|
+
return `${content}${content.endsWith(`\r
|
|
199
|
+
`) ? "" : `\r
|
|
200
|
+
`}.\r
|
|
201
|
+
`;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// ../../src/core/mail/mailer.ts
|
|
205
|
+
function resolveSmtpConfig() {
|
|
206
|
+
const host = process.env.MAIL_HOST?.trim();
|
|
207
|
+
if (!host) {
|
|
208
|
+
throw new Error('MAIL_DRIVER="smtp" requires MAIL_HOST to be set.');
|
|
209
|
+
}
|
|
210
|
+
const from = process.env.MAIL_FROM?.trim();
|
|
211
|
+
if (!from) {
|
|
212
|
+
throw new Error('MAIL_DRIVER="smtp" requires MAIL_FROM to be set.');
|
|
213
|
+
}
|
|
214
|
+
const port = Number(process.env.MAIL_PORT ?? "587");
|
|
215
|
+
if (!Number.isInteger(port) || port <= 0 || port > 65535) {
|
|
216
|
+
throw new Error('Environment variable "MAIL_PORT" must be a positive integer.');
|
|
217
|
+
}
|
|
218
|
+
return {
|
|
219
|
+
host,
|
|
220
|
+
port,
|
|
221
|
+
from,
|
|
222
|
+
secure: (process.env.MAIL_SECURE ?? "false") === "true",
|
|
223
|
+
...process.env.MAIL_TIMEOUT_MS !== undefined ? { timeoutMs: smtpTimeout(Number(process.env.MAIL_TIMEOUT_MS)) } : {},
|
|
224
|
+
...process.env.MAIL_USERNAME?.trim() ? { username: process.env.MAIL_USERNAME.trim() } : {},
|
|
225
|
+
...process.env.MAIL_PASSWORD?.trim() ? { password: process.env.MAIL_PASSWORD.trim() } : {}
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function redactMailBody(body) {
|
|
229
|
+
return body.replace(/https?:\/\/[^\s]+/giu, "[redacted-url]").replace(/[?&](token|signature|expires|code)=[^&\s]+/giu, "[redacted-query]");
|
|
230
|
+
}
|
|
155
231
|
function smtpTimeout(value = 1e4) {
|
|
156
232
|
if (!Number.isSafeInteger(value) || value < 1 || value > 2147483647) {
|
|
157
233
|
throw new RangeError("SMTP timeoutMs must be a positive integer within the timer range.");
|
|
@@ -380,80 +456,6 @@ async function defaultSmtpTransport(config, message) {
|
|
|
380
456
|
socket?.end();
|
|
381
457
|
}
|
|
382
458
|
}
|
|
383
|
-
function encodeSmtpHeader(value) {
|
|
384
|
-
if (/^[\x20-\x7e]{0,60}$/u.test(value))
|
|
385
|
-
return value;
|
|
386
|
-
const words = [];
|
|
387
|
-
let part = "";
|
|
388
|
-
for (const character of value) {
|
|
389
|
-
if (Buffer.byteLength(part + character, "utf8") > 45) {
|
|
390
|
-
words.push(`=?UTF-8?B?${encodeBase64(part)}?=`);
|
|
391
|
-
part = "";
|
|
392
|
-
}
|
|
393
|
-
part += character;
|
|
394
|
-
}
|
|
395
|
-
if (part)
|
|
396
|
-
words.push(`=?UTF-8?B?${encodeBase64(part)}?=`);
|
|
397
|
-
return words.join(`\r
|
|
398
|
-
`);
|
|
399
|
-
}
|
|
400
|
-
function smtpAddressHeader(value) {
|
|
401
|
-
if (!value.includes("<"))
|
|
402
|
-
return value;
|
|
403
|
-
const name = value.slice(0, value.lastIndexOf("<")).trim();
|
|
404
|
-
const address = smtpEnvelopeAddress(value, "address");
|
|
405
|
-
if (!name)
|
|
406
|
-
return address;
|
|
407
|
-
const encoded = encodeSmtpHeader(name);
|
|
408
|
-
const display = encoded.startsWith("=?UTF-8?B?") ? encoded : `"${encoded.replace(/[\\"]/gu, "\\$&")}"`;
|
|
409
|
-
return `${display} <${address}>`;
|
|
410
|
-
}
|
|
411
|
-
function smtpMimePart(type, value) {
|
|
412
|
-
const normalized = value.replace(/\r\n|\r|\n/gu, `\r
|
|
413
|
-
`);
|
|
414
|
-
const base64 = /[^\t\r\n\x20-\x7e]/u.test(normalized) || normalized.split(`\r
|
|
415
|
-
`).some((line) => line.length > 998);
|
|
416
|
-
return [
|
|
417
|
-
`Content-Type: ${type}; charset=utf-8`,
|
|
418
|
-
`Content-Transfer-Encoding: ${base64 ? "base64" : "7bit"}`,
|
|
419
|
-
"",
|
|
420
|
-
base64 ? (encodeBase64(normalized).match(/.{1,76}/gu) ?? []).join(`\r
|
|
421
|
-
`) : normalized
|
|
422
|
-
];
|
|
423
|
-
}
|
|
424
|
-
function buildSmtpPayload(from, message) {
|
|
425
|
-
const headers = [
|
|
426
|
-
`From: ${smtpAddressHeader(assertSafeSmtpAddress(from, "from"))}`,
|
|
427
|
-
`To: ${smtpAddressHeader(assertSafeSmtpAddress(message.to, "to"))}`,
|
|
428
|
-
`Subject: ${encodeSmtpHeader(assertSafeSmtpField(message.subject, "subject"))}`,
|
|
429
|
-
"MIME-Version: 1.0"
|
|
430
|
-
];
|
|
431
|
-
let lines;
|
|
432
|
-
if (message.html) {
|
|
433
|
-
let boundary;
|
|
434
|
-
do {
|
|
435
|
-
boundary = `strata-${crypto.randomUUID()}`;
|
|
436
|
-
} while (message.body.includes(boundary) || message.html.includes(boundary));
|
|
437
|
-
lines = [
|
|
438
|
-
...headers,
|
|
439
|
-
`Content-Type: multipart/alternative; boundary="${boundary}"`,
|
|
440
|
-
"",
|
|
441
|
-
`--${boundary}`,
|
|
442
|
-
...smtpMimePart("text/plain", message.body),
|
|
443
|
-
`--${boundary}`,
|
|
444
|
-
...smtpMimePart("text/html", message.html),
|
|
445
|
-
`--${boundary}--`,
|
|
446
|
-
""
|
|
447
|
-
];
|
|
448
|
-
} else
|
|
449
|
-
lines = [...headers, ...smtpMimePart("text/plain", message.body)];
|
|
450
|
-
const content = lines.join(`\r
|
|
451
|
-
`).replace(/(^|\r\n)\./gu, "$1..");
|
|
452
|
-
return `${content}${content.endsWith(`\r
|
|
453
|
-
`) ? "" : `\r
|
|
454
|
-
`}.\r
|
|
455
|
-
`;
|
|
456
|
-
}
|
|
457
459
|
|
|
458
460
|
class LogMailDriver {
|
|
459
461
|
async send(message) {
|
|
@@ -42,16 +42,16 @@ function currentRequestMeta() {
|
|
|
42
42
|
// ../../src/core/http/middleware.ts
|
|
43
43
|
function composeMiddleware(...middleware) {
|
|
44
44
|
return (handler) => {
|
|
45
|
-
return async (request) => {
|
|
45
|
+
return async (request, ...arguments_) => {
|
|
46
46
|
let index = 0;
|
|
47
47
|
const dispatch = async () => {
|
|
48
48
|
if (index >= middleware.length) {
|
|
49
|
-
return await handler(request);
|
|
49
|
+
return await handler(request, ...arguments_);
|
|
50
50
|
}
|
|
51
51
|
const current = middleware[index];
|
|
52
52
|
index += 1;
|
|
53
53
|
if (!current) {
|
|
54
|
-
return await handler(request);
|
|
54
|
+
return await handler(request, ...arguments_);
|
|
55
55
|
}
|
|
56
56
|
return await current(request, dispatch);
|
|
57
57
|
};
|
|
@@ -913,9 +913,6 @@ var init_databaseTenantContext = __esm(() => {
|
|
|
913
913
|
init_tenancyConfig();
|
|
914
914
|
});
|
|
915
915
|
|
|
916
|
-
// ../../src/core/audit/exportAuditLogs.ts
|
|
917
|
-
import { repositoryConnection as db } from "@getstrata/core/database/repositoryConnection";
|
|
918
|
-
|
|
919
916
|
// ../../src/core/runtime/appEnv.ts
|
|
920
917
|
var NON_PRODUCTION_APP_ENVS = new Set(["local", "development", "dev", "test", "testing", "ci"]);
|
|
921
918
|
function normalizeEnvValue(value) {
|
|
@@ -1338,8 +1335,39 @@ function assertSafeOutboundUrl(rawUrl, options = {}) {
|
|
|
1338
1335
|
return parsed;
|
|
1339
1336
|
}
|
|
1340
1337
|
|
|
1341
|
-
// ../../src/core/audit/
|
|
1338
|
+
// ../../src/core/audit/exportStore.ts
|
|
1342
1339
|
init_databaseTenantContext();
|
|
1340
|
+
import { repositoryConnection as db } from "@getstrata/core/database/repositoryConnection";
|
|
1341
|
+
async function readPendingAuditLogs(batchSize) {
|
|
1342
|
+
return await runWithMigrationBypass(async () => {
|
|
1343
|
+
return await db`
|
|
1344
|
+
SELECT
|
|
1345
|
+
id,
|
|
1346
|
+
user_id,
|
|
1347
|
+
action,
|
|
1348
|
+
subject_type,
|
|
1349
|
+
subject_id,
|
|
1350
|
+
payload,
|
|
1351
|
+
ip_address,
|
|
1352
|
+
user_agent,
|
|
1353
|
+
checksum,
|
|
1354
|
+
tenant_id,
|
|
1355
|
+
trace_id,
|
|
1356
|
+
created_at
|
|
1357
|
+
FROM audit_log
|
|
1358
|
+
WHERE exported_at IS NULL
|
|
1359
|
+
ORDER BY id
|
|
1360
|
+
LIMIT ${batchSize}
|
|
1361
|
+
`;
|
|
1362
|
+
});
|
|
1363
|
+
}
|
|
1364
|
+
async function acknowledgeAuditLogs(ids) {
|
|
1365
|
+
await runWithMigrationBypass(async () => {
|
|
1366
|
+
for (const id of ids) {
|
|
1367
|
+
await db`UPDATE audit_log SET exported_at = NOW() WHERE id = ${id}`;
|
|
1368
|
+
}
|
|
1369
|
+
});
|
|
1370
|
+
}
|
|
1343
1371
|
|
|
1344
1372
|
// ../../src/core/audit/siemFormatter.ts
|
|
1345
1373
|
function formatSiemAuditEvent(input) {
|
|
@@ -1393,61 +1421,38 @@ async function exportPendingAuditLogs() {
|
|
|
1393
1421
|
if (!config) {
|
|
1394
1422
|
return 0;
|
|
1395
1423
|
}
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
LIMIT ${config.batchSize}
|
|
1415
|
-
`;
|
|
1416
|
-
if (rows.length === 0) {
|
|
1417
|
-
return 0;
|
|
1418
|
-
}
|
|
1419
|
-
const events = rows.map((row) => formatSiemAuditEvent({
|
|
1420
|
-
action: row.action,
|
|
1421
|
-
subject_type: row.subject_type,
|
|
1422
|
-
subject_id: row.subject_id,
|
|
1423
|
-
user_id: row.user_id,
|
|
1424
|
-
tenant_id: row.tenant_id,
|
|
1425
|
-
trace_id: row.trace_id,
|
|
1426
|
-
ip_address: row.ip_address,
|
|
1427
|
-
user_agent: row.user_agent,
|
|
1428
|
-
checksum: row.checksum,
|
|
1429
|
-
payload: row.payload,
|
|
1430
|
-
created_at: row.created_at
|
|
1431
|
-
}));
|
|
1432
|
-
const body = config.format === "cef" ? events.map((event) => formatCefLine(event)).join(`
|
|
1424
|
+
const rows = await readPendingAuditLogs(config.batchSize);
|
|
1425
|
+
if (rows.length === 0) {
|
|
1426
|
+
return 0;
|
|
1427
|
+
}
|
|
1428
|
+
const events = rows.map((row) => formatSiemAuditEvent({
|
|
1429
|
+
action: row.action,
|
|
1430
|
+
subject_type: row.subject_type,
|
|
1431
|
+
subject_id: row.subject_id,
|
|
1432
|
+
user_id: row.user_id,
|
|
1433
|
+
tenant_id: row.tenant_id,
|
|
1434
|
+
trace_id: row.trace_id,
|
|
1435
|
+
ip_address: row.ip_address,
|
|
1436
|
+
user_agent: row.user_agent,
|
|
1437
|
+
checksum: row.checksum,
|
|
1438
|
+
payload: row.payload,
|
|
1439
|
+
created_at: row.created_at
|
|
1440
|
+
}));
|
|
1441
|
+
const body = config.format === "cef" ? events.map((event) => formatCefLine(event)).join(`
|
|
1433
1442
|
`) : JSON.stringify({ events });
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
await db`UPDATE audit_log SET exported_at = NOW() WHERE id = ${id}`;
|
|
1448
|
-
}
|
|
1449
|
-
return rows.length;
|
|
1450
|
-
});
|
|
1443
|
+
const response = await safeFetch(config.endpoint, {
|
|
1444
|
+
method: "POST",
|
|
1445
|
+
headers: {
|
|
1446
|
+
"content-type": config.format === "cef" ? "text/plain" : "application/json",
|
|
1447
|
+
...process.env.SIEM_EXPORT_TOKEN ? { authorization: `Bearer ${process.env.SIEM_EXPORT_TOKEN}` } : {}
|
|
1448
|
+
},
|
|
1449
|
+
body
|
|
1450
|
+
}, { allowHttp: appEnv() !== "production" });
|
|
1451
|
+
if (!response.ok) {
|
|
1452
|
+
throw new Error(`SIEM export failed with status ${response.status}.`);
|
|
1453
|
+
}
|
|
1454
|
+
await acknowledgeAuditLogs(rows.map((row) => row.id));
|
|
1455
|
+
return rows.length;
|
|
1451
1456
|
}
|
|
1452
1457
|
|
|
1453
1458
|
// ../../src/core/logging/logger.ts
|
|
@@ -85,30 +85,7 @@ function sdkClientClassName() {
|
|
|
85
85
|
return fromName ? `${fromName}Client` : "AppClient";
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
// ../../src/core/mail/
|
|
89
|
-
function resolveSmtpConfig() {
|
|
90
|
-
const host = process.env.MAIL_HOST?.trim();
|
|
91
|
-
if (!host) {
|
|
92
|
-
throw new Error('MAIL_DRIVER="smtp" requires MAIL_HOST to be set.');
|
|
93
|
-
}
|
|
94
|
-
const from = process.env.MAIL_FROM?.trim();
|
|
95
|
-
if (!from) {
|
|
96
|
-
throw new Error('MAIL_DRIVER="smtp" requires MAIL_FROM to be set.');
|
|
97
|
-
}
|
|
98
|
-
const port = Number(process.env.MAIL_PORT ?? "587");
|
|
99
|
-
if (!Number.isInteger(port) || port <= 0 || port > 65535) {
|
|
100
|
-
throw new Error('Environment variable "MAIL_PORT" must be a positive integer.');
|
|
101
|
-
}
|
|
102
|
-
return {
|
|
103
|
-
host,
|
|
104
|
-
port,
|
|
105
|
-
from,
|
|
106
|
-
secure: (process.env.MAIL_SECURE ?? "false") === "true",
|
|
107
|
-
...process.env.MAIL_TIMEOUT_MS !== undefined ? { timeoutMs: smtpTimeout(Number(process.env.MAIL_TIMEOUT_MS)) } : {},
|
|
108
|
-
...process.env.MAIL_USERNAME?.trim() ? { username: process.env.MAIL_USERNAME.trim() } : {},
|
|
109
|
-
...process.env.MAIL_PASSWORD?.trim() ? { password: process.env.MAIL_PASSWORD.trim() } : {}
|
|
110
|
-
};
|
|
111
|
-
}
|
|
88
|
+
// ../../src/core/mail/smtpMessage.ts
|
|
112
89
|
var EMAIL_SHAPE = /^[^\s@\r\n]+@[^\s@\r\n]+\.[^\s@\r\n]+$/u;
|
|
113
90
|
function assertSafeSmtpField(value, field) {
|
|
114
91
|
if (/[\r\n]/u.test(value)) {
|
|
@@ -134,12 +111,111 @@ function assertSafeSmtpAddress(value, field) {
|
|
|
134
111
|
smtpEnvelopeAddress(value, field);
|
|
135
112
|
return assertSafeSmtpField(value, field).trim();
|
|
136
113
|
}
|
|
137
|
-
function redactMailBody(body) {
|
|
138
|
-
return body.replace(/https?:\/\/[^\s]+/giu, "[redacted-url]").replace(/[?&](token|signature|expires|code)=[^&\s]+/giu, "[redacted-query]");
|
|
139
|
-
}
|
|
140
114
|
function encodeBase64(value) {
|
|
141
115
|
return Buffer.from(value, "utf8").toString("base64");
|
|
142
116
|
}
|
|
117
|
+
function encodeSmtpHeader(value) {
|
|
118
|
+
if (/^[\x20-\x7e]{0,60}$/u.test(value))
|
|
119
|
+
return value;
|
|
120
|
+
const words = [];
|
|
121
|
+
let part = "";
|
|
122
|
+
for (const character of value) {
|
|
123
|
+
if (Buffer.byteLength(part + character, "utf8") > 45) {
|
|
124
|
+
words.push(`=?UTF-8?B?${encodeBase64(part)}?=`);
|
|
125
|
+
part = "";
|
|
126
|
+
}
|
|
127
|
+
part += character;
|
|
128
|
+
}
|
|
129
|
+
if (part)
|
|
130
|
+
words.push(`=?UTF-8?B?${encodeBase64(part)}?=`);
|
|
131
|
+
return words.join(`\r
|
|
132
|
+
`);
|
|
133
|
+
}
|
|
134
|
+
function smtpAddressHeader(value) {
|
|
135
|
+
if (!value.includes("<"))
|
|
136
|
+
return value;
|
|
137
|
+
const name = value.slice(0, value.lastIndexOf("<")).trim();
|
|
138
|
+
const address = smtpEnvelopeAddress(value, "address");
|
|
139
|
+
if (!name)
|
|
140
|
+
return address;
|
|
141
|
+
const encoded = encodeSmtpHeader(name);
|
|
142
|
+
const display = encoded.startsWith("=?UTF-8?B?") ? encoded : `"${encoded.replace(/[\\"]/gu, "\\$&")}"`;
|
|
143
|
+
return `${display} <${address}>`;
|
|
144
|
+
}
|
|
145
|
+
function smtpMimePart(type, value) {
|
|
146
|
+
const normalized = value.replace(/\r\n|\r|\n/gu, `\r
|
|
147
|
+
`);
|
|
148
|
+
const base64 = /[^\t\r\n\x20-\x7e]/u.test(normalized) || normalized.split(`\r
|
|
149
|
+
`).some((line) => line.length > 998);
|
|
150
|
+
return [
|
|
151
|
+
`Content-Type: ${type}; charset=utf-8`,
|
|
152
|
+
`Content-Transfer-Encoding: ${base64 ? "base64" : "7bit"}`,
|
|
153
|
+
"",
|
|
154
|
+
base64 ? (encodeBase64(normalized).match(/.{1,76}/gu) ?? []).join(`\r
|
|
155
|
+
`) : normalized
|
|
156
|
+
];
|
|
157
|
+
}
|
|
158
|
+
function buildSmtpPayload(from, message) {
|
|
159
|
+
const headers = [
|
|
160
|
+
`From: ${smtpAddressHeader(assertSafeSmtpAddress(from, "from"))}`,
|
|
161
|
+
`To: ${smtpAddressHeader(assertSafeSmtpAddress(message.to, "to"))}`,
|
|
162
|
+
`Subject: ${encodeSmtpHeader(assertSafeSmtpField(message.subject, "subject"))}`,
|
|
163
|
+
"MIME-Version: 1.0"
|
|
164
|
+
];
|
|
165
|
+
let lines;
|
|
166
|
+
if (message.html) {
|
|
167
|
+
let boundary;
|
|
168
|
+
do {
|
|
169
|
+
boundary = `strata-${crypto.randomUUID()}`;
|
|
170
|
+
} while (message.body.includes(boundary) || message.html.includes(boundary));
|
|
171
|
+
lines = [
|
|
172
|
+
...headers,
|
|
173
|
+
`Content-Type: multipart/alternative; boundary="${boundary}"`,
|
|
174
|
+
"",
|
|
175
|
+
`--${boundary}`,
|
|
176
|
+
...smtpMimePart("text/plain", message.body),
|
|
177
|
+
`--${boundary}`,
|
|
178
|
+
...smtpMimePart("text/html", message.html),
|
|
179
|
+
`--${boundary}--`,
|
|
180
|
+
""
|
|
181
|
+
];
|
|
182
|
+
} else
|
|
183
|
+
lines = [...headers, ...smtpMimePart("text/plain", message.body)];
|
|
184
|
+
const content = lines.join(`\r
|
|
185
|
+
`).replace(/(^|\r\n)\./gu, "$1..");
|
|
186
|
+
return `${content}${content.endsWith(`\r
|
|
187
|
+
`) ? "" : `\r
|
|
188
|
+
`}.\r
|
|
189
|
+
`;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ../../src/core/mail/mailer.ts
|
|
193
|
+
function resolveSmtpConfig() {
|
|
194
|
+
const host = process.env.MAIL_HOST?.trim();
|
|
195
|
+
if (!host) {
|
|
196
|
+
throw new Error('MAIL_DRIVER="smtp" requires MAIL_HOST to be set.');
|
|
197
|
+
}
|
|
198
|
+
const from = process.env.MAIL_FROM?.trim();
|
|
199
|
+
if (!from) {
|
|
200
|
+
throw new Error('MAIL_DRIVER="smtp" requires MAIL_FROM to be set.');
|
|
201
|
+
}
|
|
202
|
+
const port = Number(process.env.MAIL_PORT ?? "587");
|
|
203
|
+
if (!Number.isInteger(port) || port <= 0 || port > 65535) {
|
|
204
|
+
throw new Error('Environment variable "MAIL_PORT" must be a positive integer.');
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
host,
|
|
208
|
+
port,
|
|
209
|
+
from,
|
|
210
|
+
secure: (process.env.MAIL_SECURE ?? "false") === "true",
|
|
211
|
+
...process.env.MAIL_TIMEOUT_MS !== undefined ? { timeoutMs: smtpTimeout(Number(process.env.MAIL_TIMEOUT_MS)) } : {},
|
|
212
|
+
...process.env.MAIL_USERNAME?.trim() ? { username: process.env.MAIL_USERNAME.trim() } : {},
|
|
213
|
+
...process.env.MAIL_PASSWORD?.trim() ? { password: process.env.MAIL_PASSWORD.trim() } : {}
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function redactMailBody(body) {
|
|
217
|
+
return body.replace(/https?:\/\/[^\s]+/giu, "[redacted-url]").replace(/[?&](token|signature|expires|code)=[^&\s]+/giu, "[redacted-query]");
|
|
218
|
+
}
|
|
143
219
|
function smtpTimeout(value = 1e4) {
|
|
144
220
|
if (!Number.isSafeInteger(value) || value < 1 || value > 2147483647) {
|
|
145
221
|
throw new RangeError("SMTP timeoutMs must be a positive integer within the timer range.");
|
|
@@ -368,80 +444,6 @@ async function defaultSmtpTransport(config, message) {
|
|
|
368
444
|
socket?.end();
|
|
369
445
|
}
|
|
370
446
|
}
|
|
371
|
-
function encodeSmtpHeader(value) {
|
|
372
|
-
if (/^[\x20-\x7e]{0,60}$/u.test(value))
|
|
373
|
-
return value;
|
|
374
|
-
const words = [];
|
|
375
|
-
let part = "";
|
|
376
|
-
for (const character of value) {
|
|
377
|
-
if (Buffer.byteLength(part + character, "utf8") > 45) {
|
|
378
|
-
words.push(`=?UTF-8?B?${encodeBase64(part)}?=`);
|
|
379
|
-
part = "";
|
|
380
|
-
}
|
|
381
|
-
part += character;
|
|
382
|
-
}
|
|
383
|
-
if (part)
|
|
384
|
-
words.push(`=?UTF-8?B?${encodeBase64(part)}?=`);
|
|
385
|
-
return words.join(`\r
|
|
386
|
-
`);
|
|
387
|
-
}
|
|
388
|
-
function smtpAddressHeader(value) {
|
|
389
|
-
if (!value.includes("<"))
|
|
390
|
-
return value;
|
|
391
|
-
const name = value.slice(0, value.lastIndexOf("<")).trim();
|
|
392
|
-
const address = smtpEnvelopeAddress(value, "address");
|
|
393
|
-
if (!name)
|
|
394
|
-
return address;
|
|
395
|
-
const encoded = encodeSmtpHeader(name);
|
|
396
|
-
const display = encoded.startsWith("=?UTF-8?B?") ? encoded : `"${encoded.replace(/[\\"]/gu, "\\$&")}"`;
|
|
397
|
-
return `${display} <${address}>`;
|
|
398
|
-
}
|
|
399
|
-
function smtpMimePart(type, value) {
|
|
400
|
-
const normalized = value.replace(/\r\n|\r|\n/gu, `\r
|
|
401
|
-
`);
|
|
402
|
-
const base64 = /[^\t\r\n\x20-\x7e]/u.test(normalized) || normalized.split(`\r
|
|
403
|
-
`).some((line) => line.length > 998);
|
|
404
|
-
return [
|
|
405
|
-
`Content-Type: ${type}; charset=utf-8`,
|
|
406
|
-
`Content-Transfer-Encoding: ${base64 ? "base64" : "7bit"}`,
|
|
407
|
-
"",
|
|
408
|
-
base64 ? (encodeBase64(normalized).match(/.{1,76}/gu) ?? []).join(`\r
|
|
409
|
-
`) : normalized
|
|
410
|
-
];
|
|
411
|
-
}
|
|
412
|
-
function buildSmtpPayload(from, message) {
|
|
413
|
-
const headers = [
|
|
414
|
-
`From: ${smtpAddressHeader(assertSafeSmtpAddress(from, "from"))}`,
|
|
415
|
-
`To: ${smtpAddressHeader(assertSafeSmtpAddress(message.to, "to"))}`,
|
|
416
|
-
`Subject: ${encodeSmtpHeader(assertSafeSmtpField(message.subject, "subject"))}`,
|
|
417
|
-
"MIME-Version: 1.0"
|
|
418
|
-
];
|
|
419
|
-
let lines;
|
|
420
|
-
if (message.html) {
|
|
421
|
-
let boundary;
|
|
422
|
-
do {
|
|
423
|
-
boundary = `strata-${crypto.randomUUID()}`;
|
|
424
|
-
} while (message.body.includes(boundary) || message.html.includes(boundary));
|
|
425
|
-
lines = [
|
|
426
|
-
...headers,
|
|
427
|
-
`Content-Type: multipart/alternative; boundary="${boundary}"`,
|
|
428
|
-
"",
|
|
429
|
-
`--${boundary}`,
|
|
430
|
-
...smtpMimePart("text/plain", message.body),
|
|
431
|
-
`--${boundary}`,
|
|
432
|
-
...smtpMimePart("text/html", message.html),
|
|
433
|
-
`--${boundary}--`,
|
|
434
|
-
""
|
|
435
|
-
];
|
|
436
|
-
} else
|
|
437
|
-
lines = [...headers, ...smtpMimePart("text/plain", message.body)];
|
|
438
|
-
const content = lines.join(`\r
|
|
439
|
-
`).replace(/(^|\r\n)\./gu, "$1..");
|
|
440
|
-
return `${content}${content.endsWith(`\r
|
|
441
|
-
`) ? "" : `\r
|
|
442
|
-
`}.\r
|
|
443
|
-
`;
|
|
444
|
-
}
|
|
445
447
|
|
|
446
448
|
class LogMailDriver {
|
|
447
449
|
async send(message) {
|