@sourceregistry/sveltekit-enhance 2.0.3 → 2.0.5
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/dist/helpers/auth.d.ts +1 -1
- package/dist/helpers/auth.js +2 -3
- package/dist/helpers/form.js +9 -4
- package/dist/helpers/internal/Deferred.js +4 -1
- package/dist/helpers/internal/correlation_id.d.ts +1 -0
- package/dist/helpers/internal/correlation_id.js +14 -0
- package/dist/helpers/request-correlation.js +1 -14
- package/dist/helpers/request-monitor.js +7 -4
- package/dist/index.js +7 -3
- package/package.json +1 -1
package/dist/helpers/auth.d.ts
CHANGED
package/dist/helpers/auth.js
CHANGED
|
@@ -4,9 +4,8 @@ export const Auth = {
|
|
|
4
4
|
if (!input.request.headers.has('Authorization'))
|
|
5
5
|
not_good(input, 401, { message: "Invalid 'Authorization' header" });
|
|
6
6
|
const authorization = input.request.headers.get('Authorization');
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const token = authorization.split(' ')?.[1];
|
|
7
|
+
// RFC 6750: case-insensitive "Bearer" scheme followed by a single token
|
|
8
|
+
const token = /^Bearer\s+(\S+)\s*$/i.exec(authorization ?? '')?.[1];
|
|
10
9
|
if (!token)
|
|
11
10
|
not_good(input, 401, { message: 'Invalid Bearer token provided' });
|
|
12
11
|
return {
|
package/dist/helpers/form.js
CHANGED
|
@@ -231,6 +231,7 @@ export function json$(formdata, name, transformer = (val) => val) {
|
|
|
231
231
|
});
|
|
232
232
|
}
|
|
233
233
|
}
|
|
234
|
+
const UNSAFE_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
|
|
234
235
|
export function jsond(formdata, options = {
|
|
235
236
|
unpack_prefixed: true,
|
|
236
237
|
transform: (value) => value
|
|
@@ -245,11 +246,15 @@ export function jsond(formdata, options = {
|
|
|
245
246
|
: [key, value]))
|
|
246
247
|
.forEach(([key, value]) => {
|
|
247
248
|
const splits = key.split('.');
|
|
249
|
+
// Field names are attacker controlled: never walk into prototype-bearing keys.
|
|
250
|
+
if (splits.some((part) => UNSAFE_KEYS.has(part)))
|
|
251
|
+
return;
|
|
248
252
|
let context = result;
|
|
249
253
|
splits.forEach((part, index) => {
|
|
254
|
+
const exists = Object.hasOwn(context, part);
|
|
250
255
|
if (index === splits.length - 1) {
|
|
251
256
|
// If it's the last part, check if the key already exists
|
|
252
|
-
if (context[part] === undefined) {
|
|
257
|
+
if (!exists || context[part] === undefined) {
|
|
253
258
|
context[part] = value;
|
|
254
259
|
}
|
|
255
260
|
else if (Array.isArray(context[part])) {
|
|
@@ -261,7 +266,7 @@ export function jsond(formdata, options = {
|
|
|
261
266
|
}
|
|
262
267
|
else {
|
|
263
268
|
// If the key doesn't exist, initialize it as an object
|
|
264
|
-
if (!context[part])
|
|
269
|
+
if (!exists || !context[part])
|
|
265
270
|
context[part] = {};
|
|
266
271
|
// Move deeper into the object
|
|
267
272
|
context = context[part];
|
|
@@ -357,7 +362,7 @@ export function Enum(formdata, name, _enum) {
|
|
|
357
362
|
if (typeof data != 'string') {
|
|
358
363
|
return fail(400, { targets: [name], message: `${name} isn't of type in enum` });
|
|
359
364
|
}
|
|
360
|
-
if (!(data
|
|
365
|
+
if (!Object.hasOwn(_enum, data))
|
|
361
366
|
return undefined;
|
|
362
367
|
return data;
|
|
363
368
|
}
|
|
@@ -400,7 +405,7 @@ export function basedOn(val, processor = (val) => val) {
|
|
|
400
405
|
export function selector(formData, cases, useArray = false) {
|
|
401
406
|
try {
|
|
402
407
|
for (const key of formData.keys()) {
|
|
403
|
-
if (key
|
|
408
|
+
if (Object.hasOwn(cases, key) && !key.startsWith('$')) {
|
|
404
409
|
const processor = cases[key];
|
|
405
410
|
if (formData.has(key)) {
|
|
406
411
|
if (useArray) {
|
|
@@ -19,10 +19,14 @@ export class Deferred {
|
|
|
19
19
|
this.reject = (reason) => {
|
|
20
20
|
if (!this._isResolved && !this._isRejected) {
|
|
21
21
|
this._isRejected = true;
|
|
22
|
+
this._rejectReason = reason;
|
|
22
23
|
reject(reason);
|
|
23
24
|
}
|
|
24
25
|
};
|
|
25
26
|
});
|
|
27
|
+
// Rejection is observable through isRejected/rejectReason; without a handler here a
|
|
28
|
+
// rejected Deferred nobody awaits would crash Node with an unhandled rejection.
|
|
29
|
+
this.promise.catch(() => { });
|
|
26
30
|
}
|
|
27
31
|
/**
|
|
28
32
|
* Returns true if the promise has been resolved
|
|
@@ -58,7 +62,6 @@ export class Deferred {
|
|
|
58
62
|
return r;
|
|
59
63
|
},
|
|
60
64
|
(reason) => {
|
|
61
|
-
this._rejectReason = reason;
|
|
62
65
|
this.reject(reason);
|
|
63
66
|
return reason;
|
|
64
67
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const normalizeCorrelationId: (raw?: string | null) => string | undefined;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const MAX_CORRELATION_ID_LENGTH = 128;
|
|
2
|
+
const SAFE_CORRELATION_ID_PATTERN = /^[A-Za-z0-9._:-]+$/;
|
|
3
|
+
export const normalizeCorrelationId = (raw) => {
|
|
4
|
+
if (!raw)
|
|
5
|
+
return undefined;
|
|
6
|
+
const candidate = raw.trim();
|
|
7
|
+
if (!candidate)
|
|
8
|
+
return undefined;
|
|
9
|
+
if (candidate.length > MAX_CORRELATION_ID_LENGTH)
|
|
10
|
+
return undefined;
|
|
11
|
+
if (!SAFE_CORRELATION_ID_PATTERN.test(candidate))
|
|
12
|
+
return undefined;
|
|
13
|
+
return candidate;
|
|
14
|
+
};
|
|
@@ -1,20 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { normalizeCorrelationId } from "./internal/correlation_id.js";
|
|
2
3
|
const CORRELATION_ID_HEADER = 'x-correlation-id';
|
|
3
4
|
const REQUEST_ID_HEADER = 'x-request-id';
|
|
4
|
-
const MAX_CORRELATION_ID_LENGTH = 128;
|
|
5
|
-
const SAFE_CORRELATION_ID_PATTERN = /^[A-Za-z0-9._:-]+$/;
|
|
6
|
-
const normalizeCorrelationId = (raw) => {
|
|
7
|
-
if (!raw)
|
|
8
|
-
return undefined;
|
|
9
|
-
const candidate = raw.trim();
|
|
10
|
-
if (!candidate)
|
|
11
|
-
return undefined;
|
|
12
|
-
if (candidate.length > MAX_CORRELATION_ID_LENGTH)
|
|
13
|
-
return undefined;
|
|
14
|
-
if (!SAFE_CORRELATION_ID_PATTERN.test(candidate))
|
|
15
|
-
return undefined;
|
|
16
|
-
return candidate;
|
|
17
|
-
};
|
|
18
5
|
const withCorrelationHeader = (response, correlationId) => {
|
|
19
6
|
try {
|
|
20
7
|
response.headers.set(CORRELATION_ID_HEADER, correlationId);
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { normalizeCorrelationId } from "./internal/correlation_id.js";
|
|
2
3
|
const durationMs = (startedAt) => Number(process.hrtime.bigint() - startedAt) / 1_000_000;
|
|
4
|
+
/** URL without query string/hash: those commonly carry tokens and must not reach logs. */
|
|
5
|
+
const safeUrl = (url) => `${url.origin}${url.pathname}`;
|
|
3
6
|
const resolveRoute = (event) => event.route.id ?? event.url.pathname;
|
|
4
7
|
export const RequestMonitor = {
|
|
5
8
|
trace: (options = {}) => {
|
|
@@ -15,8 +18,8 @@ export const RequestMonitor = {
|
|
|
15
18
|
}
|
|
16
19
|
const requestId = locals.correlation_id ??
|
|
17
20
|
locals.request_id ??
|
|
18
|
-
event.request.headers.get('x-correlation-id') ??
|
|
19
|
-
event.request.headers.get('x-request-id') ??
|
|
21
|
+
normalizeCorrelationId(event.request.headers.get('x-correlation-id')) ??
|
|
22
|
+
normalizeCorrelationId(event.request.headers.get('x-request-id')) ??
|
|
20
23
|
randomUUID();
|
|
21
24
|
const route = resolveRoute(event);
|
|
22
25
|
locals.trace = {
|
|
@@ -45,7 +48,7 @@ export const RequestMonitor = {
|
|
|
45
48
|
request_id: requestId,
|
|
46
49
|
method: event.request.method,
|
|
47
50
|
route,
|
|
48
|
-
url: event.url
|
|
51
|
+
url: safeUrl(event.url),
|
|
49
52
|
status: response.status,
|
|
50
53
|
duration_ms: elapsedMs,
|
|
51
54
|
client_ip: clientIp
|
|
@@ -74,7 +77,7 @@ export const RequestMonitor = {
|
|
|
74
77
|
request_id: requestId,
|
|
75
78
|
method: event.request.method,
|
|
76
79
|
route,
|
|
77
|
-
url: event.url,
|
|
80
|
+
url: safeUrl(event.url),
|
|
78
81
|
duration_ms: elapsedMs,
|
|
79
82
|
client_ip: clientIp,
|
|
80
83
|
error: error instanceof Error
|
package/dist/index.js
CHANGED
|
@@ -77,7 +77,7 @@ export const load = (load, ...contexts) => {
|
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
try {
|
|
80
|
-
return load(Object.assign(contextInput, { context: combined }));
|
|
80
|
+
return await load(Object.assign(contextInput, { context: combined }));
|
|
81
81
|
}
|
|
82
82
|
catch (e) {
|
|
83
83
|
return EnhanceErrorHandle(e, contextInput);
|
|
@@ -209,11 +209,15 @@ export const handle = (handle, ...contexts) => async (input) => {
|
|
|
209
209
|
}
|
|
210
210
|
};
|
|
211
211
|
try {
|
|
212
|
-
|
|
212
|
+
let response = await apply_handle(0, input.event);
|
|
213
213
|
const isSSE = response.headers.get('content-type')?.includes('text/event-stream');
|
|
214
214
|
if (!isSSE && contextInput.responseHandlers.length > 0) {
|
|
215
215
|
for (const responseHandler of contextInput.responseHandlers) {
|
|
216
|
-
|
|
216
|
+
// Handlers may return a replacement Response (needed when headers are immutable,
|
|
217
|
+
// eg. Response.redirect() or a proxied fetch response).
|
|
218
|
+
const replacement = await responseHandler({ event: contextInput, response });
|
|
219
|
+
if (replacement instanceof Response)
|
|
220
|
+
response = replacement;
|
|
217
221
|
}
|
|
218
222
|
}
|
|
219
223
|
return response;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sourceregistry/sveltekit-enhance",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.5",
|
|
4
4
|
"description": "Composable enhance and form utilities for SvelteKit actions, loads, methods, and hooks.",
|
|
5
5
|
"author": "A.P.A. Slaa (a.p.a.slaa@projectsource.nl)",
|
|
6
6
|
"engines": {
|