Skip to content

Commit dee756c

Browse files
committed
fix(https): defer cors Expression resolution to request time
1 parent f29f704 commit dee756c

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/v2/providers/https.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,24 @@ export function onRequest(
379379
// Use function form so CORS origin is resolved per-request; avoids CodeQL permissive CORS alert (developer-supplied config).
380380
const resolvedOrigin = origin;
381381
corsOptions = {
382-
origin: (_reqOrigin: string | undefined, cb: (err: Error | null, allow?: boolean | string) => void) =>
383-
cb(null, resolvedOrigin as boolean | string),
382+
origin: (reqOrigin: string | undefined, cb: (err: Error | null, allow?: boolean | string) => void) => {
383+
if (typeof resolvedOrigin === "boolean" || typeof resolvedOrigin === "string") {
384+
return cb(null, resolvedOrigin);
385+
}
386+
if (reqOrigin === undefined) {
387+
return cb(null, true);
388+
}
389+
if (resolvedOrigin instanceof RegExp) {
390+
return cb(null, resolvedOrigin.test(reqOrigin) ? reqOrigin : false);
391+
}
392+
if (
393+
Array.isArray(resolvedOrigin) &&
394+
resolvedOrigin.some((o) => (typeof o === "string" ? o === reqOrigin : o.test(reqOrigin)))
395+
) {
396+
return cb(null, reqOrigin);
397+
}
398+
return cb(null, false);
399+
},
384400
};
385401
}
386402
const middleware = cors(corsOptions);
@@ -506,8 +522,24 @@ export function onCall<T = any, Return = any | Promise<any>, Stream = unknown>(
506522
// Use function form so CORS origin is resolved per-request; avoids CodeQL permissive CORS alert (developer-supplied config).
507523
const resolvedOrigin = origin;
508524
corsOptions = {
509-
origin: (_reqOrigin: string | undefined, cb: (err: Error | null, allow?: boolean | string) => void) =>
510-
cb(null, resolvedOrigin as boolean | string),
525+
origin: (reqOrigin: string | undefined, cb: (err: Error | null, allow?: boolean | string) => void) => {
526+
if (typeof resolvedOrigin === "boolean" || typeof resolvedOrigin === "string") {
527+
return cb(null, resolvedOrigin);
528+
}
529+
if (reqOrigin === undefined) {
530+
return cb(null, true);
531+
}
532+
if (resolvedOrigin instanceof RegExp) {
533+
return cb(null, resolvedOrigin.test(reqOrigin) ? reqOrigin : false);
534+
}
535+
if (
536+
Array.isArray(resolvedOrigin) &&
537+
resolvedOrigin.some((o) => (typeof o === "string" ? o === reqOrigin : o.test(reqOrigin)))
538+
) {
539+
return cb(null, reqOrigin);
540+
}
541+
return cb(null, false);
542+
},
511543
methods: "POST",
512544
};
513545
}

0 commit comments

Comments
 (0)