abidlabs HF Staff commited on
Commit
486f33f
·
verified ·
1 Parent(s): 3f49e8f

Fix opaque Origin header crash after HF sign-in

Browse files
src/lib/server/hooks/error.ts CHANGED
@@ -19,10 +19,9 @@ export async function handleServerError({
19
  const errorId = crypto.randomUUID();
20
 
21
  logger.error({
22
- locals: event.locals,
23
- url: event.request.url,
24
- params: event.params,
25
- request: event.request,
26
  message,
27
  error,
28
  errorId,
 
19
  const errorId = crypto.randomUUID();
20
 
21
  logger.error({
22
+ // Locals and request URLs can contain OAuth tokens and account-link tickets.
23
+ url: event.url.pathname,
24
+ method: event.request.method,
 
25
  message,
26
  error,
27
  errorId,
src/lib/server/hooks/handle.ts CHANGED
@@ -15,6 +15,7 @@ import { adminTokenManager } from "$lib/server/adminToken";
15
  import { isHostLocalhost } from "$lib/server/isURLLocal";
16
  import { runWithRequestContext, updateRequestContext } from "$lib/server/requestContext";
17
  import { config, ready } from "$lib/server/config";
 
18
 
19
  type HandleInput = Parameters<Handle>[0];
20
 
@@ -154,7 +155,8 @@ export async function handleRequest({ event, resolve }: HandleInput): Promise<Re
154
  ...(config.PUBLIC_ORIGIN ? [new URL(config.PUBLIC_ORIGIN).host] : []),
155
  ];
156
 
157
- if (!validOrigins.includes(new URL(origin).host)) {
 
158
  return errorResponse(403, "Invalid referer for POST request");
159
  }
160
  }
@@ -224,6 +226,7 @@ export async function handleRequest({ event, resolve }: HandleInput): Promise<Re
224
  if (event.url.pathname.startsWith(`${base}/api/`)) {
225
  // get origin from the request
226
  const requestOrigin = event.request.headers.get("origin");
 
227
 
228
  // get origin from the config if its defined
229
  let allowedOrigin = config.PUBLIC_ORIGIN ? new URL(config.PUBLIC_ORIGIN).origin : undefined;
@@ -231,7 +234,7 @@ export async function handleRequest({ event, resolve }: HandleInput): Promise<Re
231
  if (
232
  dev || // if we're in dev mode
233
  !requestOrigin || // or the origin is null (SSR)
234
- isHostLocalhost(new URL(requestOrigin).hostname) // or the origin is localhost
235
  ) {
236
  allowedOrigin = "*"; // allow all origins
237
  } else if (allowedOrigin === requestOrigin) {
 
15
  import { isHostLocalhost } from "$lib/server/isURLLocal";
16
  import { runWithRequestContext, updateRequestContext } from "$lib/server/requestContext";
17
  import { config, ready } from "$lib/server/config";
18
+ import { parseRequestOrigin } from "./origin";
19
 
20
  type HandleInput = Parameters<Handle>[0];
21
 
 
155
  ...(config.PUBLIC_ORIGIN ? [new URL(config.PUBLIC_ORIGIN).host] : []),
156
  ];
157
 
158
+ const parsedOrigin = parseRequestOrigin(origin);
159
+ if (!parsedOrigin || !validOrigins.includes(parsedOrigin.host)) {
160
  return errorResponse(403, "Invalid referer for POST request");
161
  }
162
  }
 
226
  if (event.url.pathname.startsWith(`${base}/api/`)) {
227
  // get origin from the request
228
  const requestOrigin = event.request.headers.get("origin");
229
+ const parsedRequestOrigin = parseRequestOrigin(requestOrigin);
230
 
231
  // get origin from the config if its defined
232
  let allowedOrigin = config.PUBLIC_ORIGIN ? new URL(config.PUBLIC_ORIGIN).origin : undefined;
 
234
  if (
235
  dev || // if we're in dev mode
236
  !requestOrigin || // or the origin is null (SSR)
237
+ (parsedRequestOrigin !== undefined && isHostLocalhost(parsedRequestOrigin.hostname)) // or the origin is localhost
238
  ) {
239
  allowedOrigin = "*"; // allow all origins
240
  } else if (allowedOrigin === requestOrigin) {
src/lib/server/hooks/origin.spec.ts ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import { parseRequestOrigin } from "./origin";
3
+
4
+ describe("parseRequestOrigin", () => {
5
+ it.each([null, "null", "", "not a URL", "file:///tmp/test", "data:text/plain,test"])(
6
+ "does not treat %s as an HTTP origin",
7
+ (origin) => expect(parseRequestOrigin(origin)).toBeUndefined()
8
+ );
9
+ it("preserves valid origins for the existing origin checks", () => {
10
+ expect(parseRequestOrigin("https://example.com")?.host).toBe("example.com");
11
+ expect(parseRequestOrigin("http://localhost:5173")?.hostname).toBe("localhost");
12
+ });
13
+ });
src/lib/server/hooks/origin.ts ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ /** Browsers can send the literal "null" for an opaque origin after redirects. */
2
+ export function parseRequestOrigin(origin: string | null): URL | undefined {
3
+ if (!origin) return undefined;
4
+ try {
5
+ const url = new URL(origin);
6
+ return url.protocol === "http:" || url.protocol === "https:" ? url : undefined;
7
+ } catch {
8
+ return undefined;
9
+ }
10
+ }