@sourceregistry/sveltekit-enhance 2.0.2 → 2.0.4
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/CSRF.js +6 -1
- 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/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/package.json +3 -3
package/dist/helpers/CSRF.js
CHANGED
|
@@ -8,6 +8,11 @@ function isContentType(request, ...types) {
|
|
|
8
8
|
function isFormContentType(request) {
|
|
9
9
|
return isContentType(request, 'application/x-www-form-urlencoded', 'multipart/form-data', 'text/plain');
|
|
10
10
|
}
|
|
11
|
+
function acceptsJson(request) {
|
|
12
|
+
return (request.headers.get('accept') ?? '')
|
|
13
|
+
.split(',')
|
|
14
|
+
.some((part) => part.split(';', 1)[0].trim().toLowerCase() === 'application/json');
|
|
15
|
+
}
|
|
11
16
|
const MUTATING_METHODS = new Set(['POST', 'PUT', 'PATCH', 'DELETE']);
|
|
12
17
|
const SAFE_LOG_HEADERS = new Set(['content-type', 'origin', 'referer', 'user-agent', 'accept', 'host']);
|
|
13
18
|
export const CSRF = {
|
|
@@ -25,7 +30,7 @@ export const CSRF = {
|
|
|
25
30
|
url: url.toString(),
|
|
26
31
|
headers: Object.fromEntries([...request.headers.entries()].filter(([k]) => SAFE_LOG_HEADERS.has(k.toLowerCase())))
|
|
27
32
|
});
|
|
28
|
-
if (request
|
|
33
|
+
if (acceptsJson(request)) {
|
|
29
34
|
return json({ message }, { status: 403 });
|
|
30
35
|
}
|
|
31
36
|
return error(403, { message });
|
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) {
|
|
@@ -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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sourceregistry/sveltekit-enhance",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
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": {
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"@semantic-release/git": "^11.0.1",
|
|
61
61
|
"@semantic-release/npm": "^13.2.0",
|
|
62
62
|
"@semantic-release/release-notes-generator": "^14.1.1",
|
|
63
|
-
"@sveltejs/adapter-auto": "^
|
|
63
|
+
"@sveltejs/adapter-auto": "^9.0.0",
|
|
64
64
|
"@sveltejs/kit": "^3.0.1",
|
|
65
65
|
"@sveltejs/package": "^3.0.0",
|
|
66
66
|
"@sveltejs/vite-plugin-svelte": "^7.3.1",
|
|
@@ -72,7 +72,7 @@
|
|
|
72
72
|
"typedoc": "^0.28.20",
|
|
73
73
|
"typescript": "~6.0.3",
|
|
74
74
|
"vite": "^8.3.4",
|
|
75
|
-
"vitest": "^5.
|
|
75
|
+
"vitest": "^5.1.1"
|
|
76
76
|
},
|
|
77
77
|
"license": "Apache-2.0",
|
|
78
78
|
"bugs": {
|