@getstrata/core 3.0.4 → 3.0.7
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 +28 -0
- package/README.md +17 -9
- package/dist/core/auth/policy.d.ts +4 -1
- package/dist/core/auth/saml/samlServiceProvider.d.ts +2 -3
- package/dist/core/cache/createCacheStore.d.ts +1 -1
- package/dist/core/cache/expiration.d.ts +2 -0
- package/dist/core/cache/redisCacheScript.d.ts +1 -1
- package/dist/core/cache/redisCacheStore.d.ts +5 -5
- package/dist/core/cache/repository.d.ts +2 -0
- package/dist/core/cache/simpleCache.d.ts +5 -5
- package/dist/core/cache/simpleCacheStore.d.ts +3 -3
- package/dist/core/cache/store.d.ts +5 -4
- package/dist/core/cache/taggedCache.d.ts +2 -0
- package/dist/core/cache/tokens.d.ts +1 -1
- package/dist/core/config/envSchema.d.ts +13 -0
- package/dist/core/contracts/application.d.ts +4 -0
- package/dist/core/contracts/di.d.ts +2 -2
- package/dist/core/contracts/provider.d.ts +13 -0
- package/dist/core/contracts/serviceTokens.d.ts +12 -11
- package/dist/core/database/factory.d.ts +3 -0
- package/dist/core/database/index.d.ts +1 -1
- package/dist/core/database/migrations/declaration.d.ts +11 -0
- package/dist/core/database/migrations/runner.d.ts +2 -0
- package/dist/core/database/model.d.ts +16 -3
- package/dist/core/database/modelQuery.d.ts +1 -10
- package/dist/core/database/modelRelationLoader.d.ts +31 -0
- package/dist/core/database/mysqlConnection.d.ts +2 -2
- package/dist/core/database/mysqlNative.d.ts +2 -0
- package/dist/core/database/ormRuntime.d.ts +1 -1
- package/dist/core/database/rawMysql.d.ts +1 -1
- package/dist/core/events/eventBusClass.d.ts +6 -3
- package/dist/core/events/index.d.ts +1 -1
- package/dist/core/http/formRequest.d.ts +2 -0
- package/dist/core/http/loginThrottleMiddleware.d.ts +1 -2
- package/dist/core/http/memoryThrottleMiddleware.d.ts +1 -3
- package/dist/core/http/webFormRequest.d.ts +3 -5
- package/dist/core/mail/mailer.d.ts +6 -3
- package/dist/core/mail/markdownMailable.d.ts +2 -2
- package/dist/core/notifications/dispatcher.d.ts +5 -5
- package/dist/core/notifications/index.d.ts +1 -1
- package/dist/core/notifications/types.d.ts +9 -2
- package/dist/core/queue/index.d.ts +8 -3
- package/dist/core/queue/queueHandoff.d.ts +2 -2
- package/dist/core/runtime/application.d.ts +2 -0
- package/dist/core/security/oauthState.d.ts +12 -2
- package/dist/core/security/safeUrl.d.ts +1 -11
- package/dist/core/storage/storage.d.ts +23 -6
- package/dist/core/testing.d.ts +5 -0
- package/dist/core/testingState.d.ts +22 -0
- package/dist/entries/audit/exportAuditLogs.js +1 -0
- package/dist/entries/auth/intendedUrlCookie.js +106 -18
- package/dist/entries/auth/jwt.js +68 -6
- package/dist/entries/auth/jwtGuard.js +76 -12
- package/dist/entries/auth/oauth/samlProvider.js +4 -5
- package/dist/entries/auth/passwordConfirmCookie.js +69 -3
- package/dist/entries/auth/saml/samlServiceProvider.js +5 -7
- package/dist/entries/auth/sessionCookie.js +69 -6
- package/dist/entries/auth/sessionGuard.js +69 -6
- package/dist/entries/cache/createCacheStore.js +45 -23
- package/dist/entries/cache/repository.js +20 -5
- package/dist/entries/cache/simpleCache.js +21 -7
- package/dist/entries/cache/simpleCacheStore.js +2 -3
- package/dist/entries/config/envSchema.js +13 -0
- package/dist/entries/contracts/serviceTokens.js +2 -0
- package/dist/entries/database/bindings.js +19 -95
- package/dist/entries/database/factory.js +4 -0
- package/dist/entries/database/migrations.js +714 -1
- package/dist/entries/database/ownedReadOnlyObservation.js +1 -5
- package/dist/entries/database/sqliteConnection.js +1 -5
- package/dist/entries/http/flashMiddleware.js +33 -14
- package/dist/entries/http/flashSession.js +33 -14
- package/dist/entries/http/formRequest.js +12 -6
- package/dist/entries/http/memoryThrottleMiddleware.js +12 -11
- package/dist/entries/http/pagination.js +4 -4
- package/dist/entries/http/parseMultipartUpload.js +62 -9
- package/dist/entries/http/requirePasswordConfirmMiddleware.js +5 -2
- package/dist/entries/http/requireWebAuthMiddleware.js +5 -2
- package/dist/entries/http/routeModelBinding.js +4 -4
- package/dist/entries/http/safeInternalPath.js +5 -2
- package/dist/entries/http/scimThrottleMiddleware.js +5 -2
- package/dist/entries/http/securedRouteModelBinding.js +4 -4
- package/dist/entries/http/throttleMiddleware.js +5 -2
- package/dist/entries/http/uploads.js +62 -9
- package/dist/entries/http/validation.js +4 -4
- package/dist/entries/http/webFormRequest.js +74 -95
- package/dist/entries/jobs/exportAuditLogsJob.js +9 -4
- package/dist/entries/jobs/invalidateCacheTagsJob.js +9 -4
- package/dist/entries/queue/createAppQueue.js +10 -5
- package/dist/entries/queue/publicQueue.js +10 -5
- package/dist/entries/queue/queueWorkerTransport.js +10 -5
- package/dist/entries/queue.js +9 -4
- package/dist/entries/runtime/application.js +50 -3
- package/dist/entries/security/oauthState.js +61 -2
- package/dist/entries/security/tokenExpiry.js +62 -9
- package/dist/entries/storage/storage.js +102 -1
- package/dist/entries/testing.js +18 -0
- package/dist/framework/public-api.d.ts +16 -13
- package/dist/index.js +380 -266
- package/dist/internal/mysqlNative.js +97 -0
- package/dist/internal/ormRuntime.js +41 -4
- package/dist/internal/testingState.js +31 -0
- package/dist/types/services.d.ts +8 -1
- package/package.json +17 -4
|
@@ -538,10 +538,6 @@ function sqliteFilename(filename) {
|
|
|
538
538
|
throw new Error("SQLite path is not configured. Pass a filename or :memory:.");
|
|
539
539
|
return filename === ":memory:" ? filename : resolve(filename);
|
|
540
540
|
}
|
|
541
|
-
function isRowReturning(sql) {
|
|
542
|
-
const upper = sql.replace(/\s+/g, " ").trim().toUpperCase();
|
|
543
|
-
return upper.includes(" RETURNING ") || upper.startsWith("SELECT") || upper.startsWith("WITH") || upper.startsWith("PRAGMA") || upper.startsWith("EXPLAIN");
|
|
544
|
-
}
|
|
545
541
|
function openRawSqlitePool(options) {
|
|
546
542
|
let filename;
|
|
547
543
|
try {
|
|
@@ -585,7 +581,7 @@ function openRawSqlitePool(options) {
|
|
|
585
581
|
check();
|
|
586
582
|
const query = db.query(statement.text);
|
|
587
583
|
const params = statement.params.map((value) => value instanceof Date ? value.toISOString() : value);
|
|
588
|
-
if (
|
|
584
|
+
if (query.columnNames.length > 0)
|
|
589
585
|
return Promise.resolve(query.all(...params));
|
|
590
586
|
query.run(...params);
|
|
591
587
|
return Promise.resolve([]);
|
|
@@ -487,10 +487,6 @@ function sqliteFilename(filename) {
|
|
|
487
487
|
throw new Error("SQLite path is not configured. Pass a filename or :memory:.");
|
|
488
488
|
return filename === ":memory:" ? filename : resolve(filename);
|
|
489
489
|
}
|
|
490
|
-
function isRowReturning(sql) {
|
|
491
|
-
const upper = sql.replace(/\s+/g, " ").trim().toUpperCase();
|
|
492
|
-
return upper.includes(" RETURNING ") || upper.startsWith("SELECT") || upper.startsWith("WITH") || upper.startsWith("PRAGMA") || upper.startsWith("EXPLAIN");
|
|
493
|
-
}
|
|
494
490
|
function openRawSqlitePool(options) {
|
|
495
491
|
let filename;
|
|
496
492
|
try {
|
|
@@ -534,7 +530,7 @@ function openRawSqlitePool(options) {
|
|
|
534
530
|
check();
|
|
535
531
|
const query = db.query(statement.text);
|
|
536
532
|
const params = statement.params.map((value) => value instanceof Date ? value.toISOString() : value);
|
|
537
|
-
if (
|
|
533
|
+
if (query.columnNames.length > 0)
|
|
538
534
|
return Promise.resolve(query.all(...params));
|
|
539
535
|
query.run(...params);
|
|
540
536
|
return Promise.resolve([]);
|
|
@@ -56,6 +56,38 @@ function otelServiceName(env = process.env) {
|
|
|
56
56
|
return env.OTEL_SERVICE_NAME?.trim() || `${appKeyPrefix(env)}-api`;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
+
// ../../src/core/http/cookies.ts
|
|
60
|
+
function readRequestCookie(request, name) {
|
|
61
|
+
const cookies = request.cookies;
|
|
62
|
+
if (cookies && typeof cookies.get === "function") {
|
|
63
|
+
const value = cookies.get(name);
|
|
64
|
+
if (value) {
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const header = request.headers.get("cookie");
|
|
69
|
+
if (!header) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
for (const part of header.split(";")) {
|
|
73
|
+
const idx = part.indexOf("=");
|
|
74
|
+
if (idx === -1)
|
|
75
|
+
continue;
|
|
76
|
+
const cookieName = part.slice(0, idx).trim();
|
|
77
|
+
if (cookieName !== name)
|
|
78
|
+
continue;
|
|
79
|
+
try {
|
|
80
|
+
return decodeURIComponent(part.slice(idx + 1).trim());
|
|
81
|
+
} catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
function readBunRequestCookie(request, name) {
|
|
88
|
+
return request.cookies.get(name) ?? readRequestCookie(request, name);
|
|
89
|
+
}
|
|
90
|
+
|
|
59
91
|
// ../../src/core/http/flashSession.ts
|
|
60
92
|
var FLASH_COOKIE = "strata_flash";
|
|
61
93
|
var FLASH_TTL_MS = 60 * 1000;
|
|
@@ -73,19 +105,6 @@ function signFlashPayload(payload, issuedAt, options) {
|
|
|
73
105
|
const signature = createHmac("sha256", options.secret).update(`${payload}.${issuedAt}`).digest("hex");
|
|
74
106
|
return `${payload}.${issuedAt}.${signature}`;
|
|
75
107
|
}
|
|
76
|
-
function readFlashCookie(request, options) {
|
|
77
|
-
const cookieHeader = request.headers.get("cookie");
|
|
78
|
-
if (!cookieHeader) {
|
|
79
|
-
return null;
|
|
80
|
-
}
|
|
81
|
-
for (const part of cookieHeader.split(";")) {
|
|
82
|
-
const [name, ...rest] = part.trim().split("=");
|
|
83
|
-
if (name === options.cookieName) {
|
|
84
|
-
return decodeURIComponent(rest.join("="));
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return null;
|
|
88
|
-
}
|
|
89
108
|
function parseFlashCookie(cookieValue, options) {
|
|
90
109
|
const parts = cookieValue.split(".");
|
|
91
110
|
if (parts.length < 3) {
|
|
@@ -138,7 +157,7 @@ function clearFlashCookie(options) {
|
|
|
138
157
|
return `${options.cookieName}=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0${secure}`;
|
|
139
158
|
}
|
|
140
159
|
function pullFlash(request, options) {
|
|
141
|
-
const cookieValue =
|
|
160
|
+
const cookieValue = readRequestCookie(request, options.cookieName);
|
|
142
161
|
if (!cookieValue) {
|
|
143
162
|
return null;
|
|
144
163
|
}
|
|
@@ -56,6 +56,38 @@ function otelServiceName(env = process.env) {
|
|
|
56
56
|
return env.OTEL_SERVICE_NAME?.trim() || `${appKeyPrefix(env)}-api`;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
+
// ../../src/core/http/cookies.ts
|
|
60
|
+
function readRequestCookie(request, name) {
|
|
61
|
+
const cookies = request.cookies;
|
|
62
|
+
if (cookies && typeof cookies.get === "function") {
|
|
63
|
+
const value = cookies.get(name);
|
|
64
|
+
if (value) {
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const header = request.headers.get("cookie");
|
|
69
|
+
if (!header) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
for (const part of header.split(";")) {
|
|
73
|
+
const idx = part.indexOf("=");
|
|
74
|
+
if (idx === -1)
|
|
75
|
+
continue;
|
|
76
|
+
const cookieName = part.slice(0, idx).trim();
|
|
77
|
+
if (cookieName !== name)
|
|
78
|
+
continue;
|
|
79
|
+
try {
|
|
80
|
+
return decodeURIComponent(part.slice(idx + 1).trim());
|
|
81
|
+
} catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
function readBunRequestCookie(request, name) {
|
|
88
|
+
return request.cookies.get(name) ?? readRequestCookie(request, name);
|
|
89
|
+
}
|
|
90
|
+
|
|
59
91
|
// ../../src/core/http/flashSession.ts
|
|
60
92
|
var FLASH_COOKIE = "strata_flash";
|
|
61
93
|
var FLASH_TTL_MS = 60 * 1000;
|
|
@@ -73,19 +105,6 @@ function signFlashPayload(payload, issuedAt, options) {
|
|
|
73
105
|
const signature = createHmac("sha256", options.secret).update(`${payload}.${issuedAt}`).digest("hex");
|
|
74
106
|
return `${payload}.${issuedAt}.${signature}`;
|
|
75
107
|
}
|
|
76
|
-
function readFlashCookie(request, options) {
|
|
77
|
-
const cookieHeader = request.headers.get("cookie");
|
|
78
|
-
if (!cookieHeader) {
|
|
79
|
-
return null;
|
|
80
|
-
}
|
|
81
|
-
for (const part of cookieHeader.split(";")) {
|
|
82
|
-
const [name, ...rest] = part.trim().split("=");
|
|
83
|
-
if (name === options.cookieName) {
|
|
84
|
-
return decodeURIComponent(rest.join("="));
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return null;
|
|
88
|
-
}
|
|
89
108
|
function parseFlashCookie(cookieValue, options) {
|
|
90
109
|
const parts = cookieValue.split(".");
|
|
91
110
|
if (parts.length < 3) {
|
|
@@ -138,7 +157,7 @@ function clearFlashCookie(options) {
|
|
|
138
157
|
return `${options.cookieName}=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0${secure}`;
|
|
139
158
|
}
|
|
140
159
|
function pullFlash(request, options) {
|
|
141
|
-
const cookieValue =
|
|
160
|
+
const cookieValue = readRequestCookie(request, options.cookieName);
|
|
142
161
|
if (!cookieValue) {
|
|
143
162
|
return null;
|
|
144
163
|
}
|
|
@@ -27,8 +27,8 @@ function parseOptionalPositiveIntQueryParam(params, name) {
|
|
|
27
27
|
if (value === null || value.trim() === "") {
|
|
28
28
|
return;
|
|
29
29
|
}
|
|
30
|
-
const parsed = Number
|
|
31
|
-
if (!Number.
|
|
30
|
+
const parsed = Number(value);
|
|
31
|
+
if (!/^[0-9]+$/.test(value.trim()) || !Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
32
32
|
throw new BadRequestError(`Invalid query parameter "${name}". Expected a positive integer.`);
|
|
33
33
|
}
|
|
34
34
|
return parsed;
|
|
@@ -124,8 +124,8 @@ function readOptionalPositiveInt(payload, field) {
|
|
|
124
124
|
return readRequiredPositiveInt(payload, field);
|
|
125
125
|
}
|
|
126
126
|
function parsePositiveIntParam(value, name = "id") {
|
|
127
|
-
const parsed = Number
|
|
128
|
-
if (!Number.
|
|
127
|
+
const parsed = Number(value);
|
|
128
|
+
if (!/^[0-9]+$/.test(value.trim()) || !Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
129
129
|
throw new BadRequestError(`Invalid ${name}. Expected a positive integer.`);
|
|
130
130
|
}
|
|
131
131
|
return parsed;
|
|
@@ -136,11 +136,17 @@ class FormRequest {
|
|
|
136
136
|
authorize(_request) {
|
|
137
137
|
return true;
|
|
138
138
|
}
|
|
139
|
+
validatePayload(payload) {
|
|
140
|
+
return this.parse(payload);
|
|
141
|
+
}
|
|
142
|
+
readPayload(request) {
|
|
143
|
+
return parseJsonBody(request, (payload) => payload);
|
|
144
|
+
}
|
|
139
145
|
async validate(request) {
|
|
140
|
-
if (
|
|
146
|
+
if (await this.authorize(request) !== true) {
|
|
141
147
|
throw new ForbiddenError;
|
|
142
148
|
}
|
|
143
|
-
return await
|
|
149
|
+
return this.validatePayload(await this.readPayload(request));
|
|
144
150
|
}
|
|
145
151
|
}
|
|
146
152
|
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
// @bun
|
|
2
|
+
// ../../src/core/http/memoryThrottleMiddleware.ts
|
|
3
|
+
import { testState } from "#strata-test-state";
|
|
4
|
+
|
|
2
5
|
// ../../src/core/runtime/boundedThrottleStore.ts
|
|
3
6
|
class BoundedThrottleStore {
|
|
4
7
|
decayMs;
|
|
@@ -131,6 +134,9 @@ import { currentRequestMeta as currentRequestMeta3 } from "@getstrata/core/http/
|
|
|
131
134
|
var NESTED_REDIRECT_KEYS = new Set(["redirect", "next", "return", "returnTo", "return_to"]);
|
|
132
135
|
function looksLikeExternalTarget(value) {
|
|
133
136
|
const trimmed = value.trim();
|
|
137
|
+
if (/[\t\r\n]/.test(trimmed)) {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
134
140
|
if (!trimmed.startsWith("/") || trimmed.startsWith("//") || trimmed.includes("\\")) {
|
|
135
141
|
return true;
|
|
136
142
|
}
|
|
@@ -156,13 +162,13 @@ function sanitizeNestedQuery(search) {
|
|
|
156
162
|
for (const [key, value] of params.entries()) {
|
|
157
163
|
if (NESTED_REDIRECT_KEYS.has(key)) {
|
|
158
164
|
const sanitized = sanitizeInternalPath(value, "/");
|
|
159
|
-
next.
|
|
165
|
+
next.append(key, sanitized);
|
|
160
166
|
continue;
|
|
161
167
|
}
|
|
162
168
|
if (looksLikeExternalTarget(value) && value.includes("://")) {
|
|
163
169
|
continue;
|
|
164
170
|
}
|
|
165
|
-
next.
|
|
171
|
+
next.append(key, value);
|
|
166
172
|
}
|
|
167
173
|
const serialized = next.toString();
|
|
168
174
|
return serialized ? `?${serialized}` : "";
|
|
@@ -468,17 +474,16 @@ function createThrottleMiddleware(options) {
|
|
|
468
474
|
}
|
|
469
475
|
|
|
470
476
|
// ../../src/core/http/memoryThrottleMiddleware.ts
|
|
471
|
-
var resetGeneration = 0;
|
|
472
477
|
function createMemoryThrottleMiddleware(options) {
|
|
473
478
|
options = Object.freeze({ ...options });
|
|
474
479
|
const store = new BoundedThrottleStore(options.decaySeconds * 1000, options);
|
|
475
480
|
if (!Number.isSafeInteger(options.maxAttempts) || options.maxAttempts < 0)
|
|
476
481
|
throw new Error("Invalid memory throttle limit.");
|
|
477
|
-
let generation =
|
|
482
|
+
let generation = testState.throttleGeneration;
|
|
478
483
|
const middleware = async (request, next) => {
|
|
479
|
-
if (generation !==
|
|
484
|
+
if (generation !== testState.throttleGeneration) {
|
|
480
485
|
store.clear();
|
|
481
|
-
generation =
|
|
486
|
+
generation = testState.throttleGeneration;
|
|
482
487
|
}
|
|
483
488
|
let maxAttempts;
|
|
484
489
|
try {
|
|
@@ -495,10 +500,6 @@ function createMemoryThrottleMiddleware(options) {
|
|
|
495
500
|
};
|
|
496
501
|
return Object.assign(middleware, { dispose: () => store.dispose(), stats: () => store.stats() });
|
|
497
502
|
}
|
|
498
|
-
function resetMemoryThrottleForTests() {
|
|
499
|
-
resetGeneration++;
|
|
500
|
-
}
|
|
501
503
|
export {
|
|
502
|
-
createMemoryThrottleMiddleware
|
|
503
|
-
resetMemoryThrottleForTests
|
|
504
|
+
createMemoryThrottleMiddleware
|
|
504
505
|
};
|
|
@@ -38,8 +38,8 @@ function parseOptionalPositiveIntQueryParam(params, name) {
|
|
|
38
38
|
if (value === null || value.trim() === "") {
|
|
39
39
|
return;
|
|
40
40
|
}
|
|
41
|
-
const parsed = Number
|
|
42
|
-
if (!Number.
|
|
41
|
+
const parsed = Number(value);
|
|
42
|
+
if (!/^[0-9]+$/.test(value.trim()) || !Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
43
43
|
throw new BadRequestError(`Invalid query parameter "${name}". Expected a positive integer.`);
|
|
44
44
|
}
|
|
45
45
|
return parsed;
|
|
@@ -135,8 +135,8 @@ function readOptionalPositiveInt(payload, field) {
|
|
|
135
135
|
return readRequiredPositiveInt(payload, field);
|
|
136
136
|
}
|
|
137
137
|
function parsePositiveIntParam(value, name = "id") {
|
|
138
|
-
const parsed = Number
|
|
139
|
-
if (!Number.
|
|
138
|
+
const parsed = Number(value);
|
|
139
|
+
if (!/^[0-9]+$/.test(value.trim()) || !Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
140
140
|
throw new BadRequestError(`Invalid ${name}. Expected a positive integer.`);
|
|
141
141
|
}
|
|
142
142
|
return parsed;
|
|
@@ -2,6 +2,67 @@
|
|
|
2
2
|
// ../../src/core/http/parseMultipartUpload.ts
|
|
3
3
|
import { BadRequestError, PayloadTooLargeError } from "@getstrata/core/errors/http";
|
|
4
4
|
|
|
5
|
+
// ../../src/core/config/envSchema.ts
|
|
6
|
+
function defineEnvSchema(schema) {
|
|
7
|
+
return schema;
|
|
8
|
+
}
|
|
9
|
+
function validateEnv(schema, env = process.env) {
|
|
10
|
+
const resolved = {};
|
|
11
|
+
for (const [name, rule] of Object.entries(schema)) {
|
|
12
|
+
if (rule.integer) {
|
|
13
|
+
if (rule.minimum !== undefined && !Number.isSafeInteger(rule.minimum) || rule.maximum !== undefined && !Number.isSafeInteger(rule.maximum) || rule.minimum !== undefined && rule.maximum !== undefined && rule.minimum > rule.maximum) {
|
|
14
|
+
throw new TypeError(`Environment integer rule for "${name}" has invalid bounds.`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const rawValue = env[name];
|
|
18
|
+
if (rawValue !== undefined && typeof rawValue !== "string") {
|
|
19
|
+
throw new TypeError(`Environment variable "${name}" must be a string.`);
|
|
20
|
+
}
|
|
21
|
+
const value = rawValue === undefined || rawValue.trim() === "" ? rule.default : rawValue;
|
|
22
|
+
if (value !== undefined && typeof value !== "string") {
|
|
23
|
+
throw new TypeError(`Environment variable "${name}" must resolve to a string.`);
|
|
24
|
+
}
|
|
25
|
+
if (value === undefined || value.trim() === "") {
|
|
26
|
+
if (rule.required) {
|
|
27
|
+
throw new Error(`Missing required environment variable "${name}".`);
|
|
28
|
+
}
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (rule.integer) {
|
|
32
|
+
const completeInteger = /^[+-]?\d+$/.test(value.trim());
|
|
33
|
+
const parsed = Number.parseInt(value, 10);
|
|
34
|
+
const minimum = rule.minimum ?? Number.NEGATIVE_INFINITY;
|
|
35
|
+
const maximum = rule.maximum ?? Number.POSITIVE_INFINITY;
|
|
36
|
+
if (!completeInteger || !Number.isSafeInteger(parsed) || parsed < minimum || parsed > maximum) {
|
|
37
|
+
const comparison = minimum !== Number.NEGATIVE_INFINITY && maximum !== Number.POSITIVE_INFINITY ? `an integer between ${minimum} and ${maximum}` : maximum !== Number.POSITIVE_INFINITY ? `an integer <= ${maximum}` : minimum === Number.NEGATIVE_INFINITY ? "an integer" : `an integer >= ${minimum}`;
|
|
38
|
+
throw new Error(`Environment variable "${name}" must be ${comparison}.`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (rule.pattern && !new RegExp(rule.pattern.source, rule.pattern.flags).test(value)) {
|
|
42
|
+
throw new Error(`Environment variable "${name}" has an invalid format.`);
|
|
43
|
+
}
|
|
44
|
+
Object.defineProperty(resolved, name, {
|
|
45
|
+
value,
|
|
46
|
+
enumerable: true,
|
|
47
|
+
configurable: true,
|
|
48
|
+
writable: true
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return resolved;
|
|
52
|
+
}
|
|
53
|
+
function resolveEnv(schema, env = process.env) {
|
|
54
|
+
const validated = validateEnv(schema, env);
|
|
55
|
+
const resolved = {};
|
|
56
|
+
for (const [name, rule] of Object.entries(schema)) {
|
|
57
|
+
const value = validated[name];
|
|
58
|
+
Object.defineProperty(resolved, name, {
|
|
59
|
+
value: value === undefined ? undefined : rule.integer ? Number(value) : value,
|
|
60
|
+
enumerable: true
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return Object.freeze(resolved);
|
|
64
|
+
}
|
|
65
|
+
|
|
5
66
|
// ../../src/core/http/uploads.ts
|
|
6
67
|
var DEFAULT_MAX_UPLOAD_BYTES = 5 * 1024 * 1024;
|
|
7
68
|
var ALLOWED_UPLOAD_MIME_TYPES = new Set([
|
|
@@ -21,15 +82,7 @@ var DEFAULT_UPLOAD_POLICY = Object.freeze({
|
|
|
21
82
|
allowUnknownMime: false
|
|
22
83
|
});
|
|
23
84
|
function resolveMaxUploadBytes(value) {
|
|
24
|
-
|
|
25
|
-
if (!raw) {
|
|
26
|
-
return DEFAULT_MAX_UPLOAD_BYTES;
|
|
27
|
-
}
|
|
28
|
-
const parsed = Number.parseInt(raw, 10);
|
|
29
|
-
if (!Number.isInteger(parsed) || parsed <= 0) {
|
|
30
|
-
return DEFAULT_MAX_UPLOAD_BYTES;
|
|
31
|
-
}
|
|
32
|
-
return parsed;
|
|
85
|
+
return resolveEnv({ MAX_UPLOAD_BYTES: { integer: true, minimum: 1, default: String(DEFAULT_MAX_UPLOAD_BYTES) } }, { MAX_UPLOAD_BYTES: value }).MAX_UPLOAD_BYTES;
|
|
33
86
|
}
|
|
34
87
|
function normalizeMimeType(mimeType) {
|
|
35
88
|
return mimeType.split(";")[0]?.trim().toLowerCase() ?? "";
|
|
@@ -69,6 +69,9 @@ function currentRequestMeta() {
|
|
|
69
69
|
var NESTED_REDIRECT_KEYS = new Set(["redirect", "next", "return", "returnTo", "return_to"]);
|
|
70
70
|
function looksLikeExternalTarget(value) {
|
|
71
71
|
const trimmed = value.trim();
|
|
72
|
+
if (/[\t\r\n]/.test(trimmed)) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
72
75
|
if (!trimmed.startsWith("/") || trimmed.startsWith("//") || trimmed.includes("\\")) {
|
|
73
76
|
return true;
|
|
74
77
|
}
|
|
@@ -94,13 +97,13 @@ function sanitizeNestedQuery(search) {
|
|
|
94
97
|
for (const [key, value] of params.entries()) {
|
|
95
98
|
if (NESTED_REDIRECT_KEYS.has(key)) {
|
|
96
99
|
const sanitized = sanitizeInternalPath(value, "/");
|
|
97
|
-
next.
|
|
100
|
+
next.append(key, sanitized);
|
|
98
101
|
continue;
|
|
99
102
|
}
|
|
100
103
|
if (looksLikeExternalTarget(value) && value.includes("://")) {
|
|
101
104
|
continue;
|
|
102
105
|
}
|
|
103
|
-
next.
|
|
106
|
+
next.append(key, value);
|
|
104
107
|
}
|
|
105
108
|
const serialized = next.toString();
|
|
106
109
|
return serialized ? `?${serialized}` : "";
|
|
@@ -39,6 +39,9 @@ function requestPrefersJson(request) {
|
|
|
39
39
|
var NESTED_REDIRECT_KEYS = new Set(["redirect", "next", "return", "returnTo", "return_to"]);
|
|
40
40
|
function looksLikeExternalTarget(value) {
|
|
41
41
|
const trimmed = value.trim();
|
|
42
|
+
if (/[\t\r\n]/.test(trimmed)) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
42
45
|
if (!trimmed.startsWith("/") || trimmed.startsWith("//") || trimmed.includes("\\")) {
|
|
43
46
|
return true;
|
|
44
47
|
}
|
|
@@ -64,13 +67,13 @@ function sanitizeNestedQuery(search) {
|
|
|
64
67
|
for (const [key, value] of params.entries()) {
|
|
65
68
|
if (NESTED_REDIRECT_KEYS.has(key)) {
|
|
66
69
|
const sanitized = sanitizeInternalPath(value, "/");
|
|
67
|
-
next.
|
|
70
|
+
next.append(key, sanitized);
|
|
68
71
|
continue;
|
|
69
72
|
}
|
|
70
73
|
if (looksLikeExternalTarget(value) && value.includes("://")) {
|
|
71
74
|
continue;
|
|
72
75
|
}
|
|
73
|
-
next.
|
|
76
|
+
next.append(key, value);
|
|
74
77
|
}
|
|
75
78
|
const serialized = next.toString();
|
|
76
79
|
return serialized ? `?${serialized}` : "";
|
|
@@ -24,8 +24,8 @@ function parseOptionalPositiveIntQueryParam(params, name) {
|
|
|
24
24
|
if (value === null || value.trim() === "") {
|
|
25
25
|
return;
|
|
26
26
|
}
|
|
27
|
-
const parsed = Number
|
|
28
|
-
if (!Number.
|
|
27
|
+
const parsed = Number(value);
|
|
28
|
+
if (!/^[0-9]+$/.test(value.trim()) || !Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
29
29
|
throw new BadRequestError(`Invalid query parameter "${name}". Expected a positive integer.`);
|
|
30
30
|
}
|
|
31
31
|
return parsed;
|
|
@@ -121,8 +121,8 @@ function readOptionalPositiveInt(payload, field) {
|
|
|
121
121
|
return readRequiredPositiveInt(payload, field);
|
|
122
122
|
}
|
|
123
123
|
function parsePositiveIntParam(value, name = "id") {
|
|
124
|
-
const parsed = Number
|
|
125
|
-
if (!Number.
|
|
124
|
+
const parsed = Number(value);
|
|
125
|
+
if (!/^[0-9]+$/.test(value.trim()) || !Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
126
126
|
throw new BadRequestError(`Invalid ${name}. Expected a positive integer.`);
|
|
127
127
|
}
|
|
128
128
|
return parsed;
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
var NESTED_REDIRECT_KEYS = new Set(["redirect", "next", "return", "returnTo", "return_to"]);
|
|
4
4
|
function looksLikeExternalTarget(value) {
|
|
5
5
|
const trimmed = value.trim();
|
|
6
|
+
if (/[\t\r\n]/.test(trimmed)) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
6
9
|
if (!trimmed.startsWith("/") || trimmed.startsWith("//") || trimmed.includes("\\")) {
|
|
7
10
|
return true;
|
|
8
11
|
}
|
|
@@ -28,13 +31,13 @@ function sanitizeNestedQuery(search) {
|
|
|
28
31
|
for (const [key, value] of params.entries()) {
|
|
29
32
|
if (NESTED_REDIRECT_KEYS.has(key)) {
|
|
30
33
|
const sanitized = sanitizeInternalPath(value, "/");
|
|
31
|
-
next.
|
|
34
|
+
next.append(key, sanitized);
|
|
32
35
|
continue;
|
|
33
36
|
}
|
|
34
37
|
if (looksLikeExternalTarget(value) && value.includes("://")) {
|
|
35
38
|
continue;
|
|
36
39
|
}
|
|
37
|
-
next.
|
|
40
|
+
next.append(key, value);
|
|
38
41
|
}
|
|
39
42
|
const serialized = next.toString();
|
|
40
43
|
return serialized ? `?${serialized}` : "";
|
|
@@ -134,6 +134,9 @@ import { currentRequestMeta as currentRequestMeta3 } from "@getstrata/core/http/
|
|
|
134
134
|
var NESTED_REDIRECT_KEYS = new Set(["redirect", "next", "return", "returnTo", "return_to"]);
|
|
135
135
|
function looksLikeExternalTarget(value) {
|
|
136
136
|
const trimmed = value.trim();
|
|
137
|
+
if (/[\t\r\n]/.test(trimmed)) {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
137
140
|
if (!trimmed.startsWith("/") || trimmed.startsWith("//") || trimmed.includes("\\")) {
|
|
138
141
|
return true;
|
|
139
142
|
}
|
|
@@ -159,13 +162,13 @@ function sanitizeNestedQuery(search) {
|
|
|
159
162
|
for (const [key, value] of params.entries()) {
|
|
160
163
|
if (NESTED_REDIRECT_KEYS.has(key)) {
|
|
161
164
|
const sanitized = sanitizeInternalPath(value, "/");
|
|
162
|
-
next.
|
|
165
|
+
next.append(key, sanitized);
|
|
163
166
|
continue;
|
|
164
167
|
}
|
|
165
168
|
if (looksLikeExternalTarget(value) && value.includes("://")) {
|
|
166
169
|
continue;
|
|
167
170
|
}
|
|
168
|
-
next.
|
|
171
|
+
next.append(key, value);
|
|
169
172
|
}
|
|
170
173
|
const serialized = next.toString();
|
|
171
174
|
return serialized ? `?${serialized}` : "";
|
|
@@ -128,8 +128,8 @@ function parseOptionalPositiveIntQueryParam(params, name) {
|
|
|
128
128
|
if (value === null || value.trim() === "") {
|
|
129
129
|
return;
|
|
130
130
|
}
|
|
131
|
-
const parsed = Number
|
|
132
|
-
if (!Number.
|
|
131
|
+
const parsed = Number(value);
|
|
132
|
+
if (!/^[0-9]+$/.test(value.trim()) || !Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
133
133
|
throw new BadRequestError(`Invalid query parameter "${name}". Expected a positive integer.`);
|
|
134
134
|
}
|
|
135
135
|
return parsed;
|
|
@@ -225,8 +225,8 @@ function readOptionalPositiveInt(payload, field) {
|
|
|
225
225
|
return readRequiredPositiveInt(payload, field);
|
|
226
226
|
}
|
|
227
227
|
function parsePositiveIntParam(value, name = "id") {
|
|
228
|
-
const parsed = Number
|
|
229
|
-
if (!Number.
|
|
228
|
+
const parsed = Number(value);
|
|
229
|
+
if (!/^[0-9]+$/.test(value.trim()) || !Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
230
230
|
throw new BadRequestError(`Invalid ${name}. Expected a positive integer.`);
|
|
231
231
|
}
|
|
232
232
|
return parsed;
|
|
@@ -70,6 +70,9 @@ import { currentRequestMeta as currentRequestMeta3 } from "@getstrata/core/http/
|
|
|
70
70
|
var NESTED_REDIRECT_KEYS = new Set(["redirect", "next", "return", "returnTo", "return_to"]);
|
|
71
71
|
function looksLikeExternalTarget(value) {
|
|
72
72
|
const trimmed = value.trim();
|
|
73
|
+
if (/[\t\r\n]/.test(trimmed)) {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
73
76
|
if (!trimmed.startsWith("/") || trimmed.startsWith("//") || trimmed.includes("\\")) {
|
|
74
77
|
return true;
|
|
75
78
|
}
|
|
@@ -95,13 +98,13 @@ function sanitizeNestedQuery(search) {
|
|
|
95
98
|
for (const [key, value] of params.entries()) {
|
|
96
99
|
if (NESTED_REDIRECT_KEYS.has(key)) {
|
|
97
100
|
const sanitized = sanitizeInternalPath(value, "/");
|
|
98
|
-
next.
|
|
101
|
+
next.append(key, sanitized);
|
|
99
102
|
continue;
|
|
100
103
|
}
|
|
101
104
|
if (looksLikeExternalTarget(value) && value.includes("://")) {
|
|
102
105
|
continue;
|
|
103
106
|
}
|
|
104
|
-
next.
|
|
107
|
+
next.append(key, value);
|
|
105
108
|
}
|
|
106
109
|
const serialized = next.toString();
|
|
107
110
|
return serialized ? `?${serialized}` : "";
|
|
@@ -1,4 +1,65 @@
|
|
|
1
1
|
// @bun
|
|
2
|
+
// ../../src/core/config/envSchema.ts
|
|
3
|
+
function defineEnvSchema(schema) {
|
|
4
|
+
return schema;
|
|
5
|
+
}
|
|
6
|
+
function validateEnv(schema, env = process.env) {
|
|
7
|
+
const resolved = {};
|
|
8
|
+
for (const [name, rule] of Object.entries(schema)) {
|
|
9
|
+
if (rule.integer) {
|
|
10
|
+
if (rule.minimum !== undefined && !Number.isSafeInteger(rule.minimum) || rule.maximum !== undefined && !Number.isSafeInteger(rule.maximum) || rule.minimum !== undefined && rule.maximum !== undefined && rule.minimum > rule.maximum) {
|
|
11
|
+
throw new TypeError(`Environment integer rule for "${name}" has invalid bounds.`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
const rawValue = env[name];
|
|
15
|
+
if (rawValue !== undefined && typeof rawValue !== "string") {
|
|
16
|
+
throw new TypeError(`Environment variable "${name}" must be a string.`);
|
|
17
|
+
}
|
|
18
|
+
const value = rawValue === undefined || rawValue.trim() === "" ? rule.default : rawValue;
|
|
19
|
+
if (value !== undefined && typeof value !== "string") {
|
|
20
|
+
throw new TypeError(`Environment variable "${name}" must resolve to a string.`);
|
|
21
|
+
}
|
|
22
|
+
if (value === undefined || value.trim() === "") {
|
|
23
|
+
if (rule.required) {
|
|
24
|
+
throw new Error(`Missing required environment variable "${name}".`);
|
|
25
|
+
}
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (rule.integer) {
|
|
29
|
+
const completeInteger = /^[+-]?\d+$/.test(value.trim());
|
|
30
|
+
const parsed = Number.parseInt(value, 10);
|
|
31
|
+
const minimum = rule.minimum ?? Number.NEGATIVE_INFINITY;
|
|
32
|
+
const maximum = rule.maximum ?? Number.POSITIVE_INFINITY;
|
|
33
|
+
if (!completeInteger || !Number.isSafeInteger(parsed) || parsed < minimum || parsed > maximum) {
|
|
34
|
+
const comparison = minimum !== Number.NEGATIVE_INFINITY && maximum !== Number.POSITIVE_INFINITY ? `an integer between ${minimum} and ${maximum}` : maximum !== Number.POSITIVE_INFINITY ? `an integer <= ${maximum}` : minimum === Number.NEGATIVE_INFINITY ? "an integer" : `an integer >= ${minimum}`;
|
|
35
|
+
throw new Error(`Environment variable "${name}" must be ${comparison}.`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (rule.pattern && !new RegExp(rule.pattern.source, rule.pattern.flags).test(value)) {
|
|
39
|
+
throw new Error(`Environment variable "${name}" has an invalid format.`);
|
|
40
|
+
}
|
|
41
|
+
Object.defineProperty(resolved, name, {
|
|
42
|
+
value,
|
|
43
|
+
enumerable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
writable: true
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return resolved;
|
|
49
|
+
}
|
|
50
|
+
function resolveEnv(schema, env = process.env) {
|
|
51
|
+
const validated = validateEnv(schema, env);
|
|
52
|
+
const resolved = {};
|
|
53
|
+
for (const [name, rule] of Object.entries(schema)) {
|
|
54
|
+
const value = validated[name];
|
|
55
|
+
Object.defineProperty(resolved, name, {
|
|
56
|
+
value: value === undefined ? undefined : rule.integer ? Number(value) : value,
|
|
57
|
+
enumerable: true
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return Object.freeze(resolved);
|
|
61
|
+
}
|
|
62
|
+
|
|
2
63
|
// ../../src/core/http/uploads.ts
|
|
3
64
|
var DEFAULT_MAX_UPLOAD_BYTES = 5 * 1024 * 1024;
|
|
4
65
|
var ALLOWED_UPLOAD_MIME_TYPES = new Set([
|
|
@@ -18,15 +79,7 @@ var DEFAULT_UPLOAD_POLICY = Object.freeze({
|
|
|
18
79
|
allowUnknownMime: false
|
|
19
80
|
});
|
|
20
81
|
function resolveMaxUploadBytes(value) {
|
|
21
|
-
|
|
22
|
-
if (!raw) {
|
|
23
|
-
return DEFAULT_MAX_UPLOAD_BYTES;
|
|
24
|
-
}
|
|
25
|
-
const parsed = Number.parseInt(raw, 10);
|
|
26
|
-
if (!Number.isInteger(parsed) || parsed <= 0) {
|
|
27
|
-
return DEFAULT_MAX_UPLOAD_BYTES;
|
|
28
|
-
}
|
|
29
|
-
return parsed;
|
|
82
|
+
return resolveEnv({ MAX_UPLOAD_BYTES: { integer: true, minimum: 1, default: String(DEFAULT_MAX_UPLOAD_BYTES) } }, { MAX_UPLOAD_BYTES: value }).MAX_UPLOAD_BYTES;
|
|
30
83
|
}
|
|
31
84
|
function normalizeMimeType(mimeType) {
|
|
32
85
|
return mimeType.split(";")[0]?.trim().toLowerCase() ?? "";
|