@sourceregistry/sveltekit-enhance 2.0.3 → 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.
@@ -1,6 +1,6 @@
1
1
  import { type EnhanceInput } from "../index.js";
2
2
  export declare const Auth: {
3
3
  Bearer: (input: EnhanceInput) => {
4
- token: string;
4
+ token: string | undefined;
5
5
  };
6
6
  };
@@ -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
- if (!(authorization && authorization.split('Bearer')))
8
- not_good(input, 401, { message: 'Invalid Bearer token provided' });
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 {
@@ -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 in _enum))
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 in cases) {
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.toString(),
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",
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": {