Skip to content

Commit e4e0612

Browse files
committed
fix: allow CORS from any loopback origin regardless of port
Previously isLoopbackOriginValue() only allowed origins on the same port as the proxy (e.g. http://localhost:10100). This blocked browser apps running on different localhost ports (e.g. CLIsu on :6001) from making cross-origin requests to the proxy. Now any http/https origin on localhost/127.0.0.1/::1 is treated as same- trust-boundary and gets proper CORS headers with the request's origin reflected back.
1 parent a876f5b commit e4e0612

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

src/server.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,9 +1480,8 @@ function isLoopbackRequestHost(value: string | null): boolean {
14801480
function isLoopbackOriginValue(value: string): boolean {
14811481
try {
14821482
const parsed = new URL(value);
1483-
if (parsed.protocol !== "http:") return false;
1484-
if (!isLoopbackHostname(parsed.hostname)) return false;
1485-
return parsed.port === configuredPort();
1483+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return false;
1484+
return isLoopbackHostname(parsed.hostname);
14861485
} catch {
14871486
return false;
14881487
}
@@ -1497,12 +1496,22 @@ function isSameOriginAsRequest(req: Request, origin: string): boolean {
14971496
}
14981497

14991498
function isAllowedRequestOrigin(req: Request, config: OcxConfig): boolean {
1499+
function isExtraAllowedOrigin(origin: string, cfg: OcxConfig): boolean {
1500+
if (!cfg.corsAllowOrigins?.length) return false;
1501+
return cfg.corsAllowOrigins.some(allowed => {
1502+
try {
1503+
return new URL(allowed).origin === new URL(origin).origin;
1504+
} catch {
1505+
return allowed === origin;
1506+
}
1507+
});
1508+
}
15001509
const origin = req.headers.get("Origin");
15011510
if (!isApiAuthRequired(config)) {
15021511
if (!isLoopbackRequestHost(req.headers.get("Host"))) return false;
1503-
return !origin || isLoopbackOriginValue(origin);
1512+
return !origin || isLoopbackOriginValue(origin) || isExtraAllowedOrigin(origin, config);
15041513
}
1505-
return !origin || isLoopbackOriginValue(origin) || isSameOriginAsRequest(req, origin);
1514+
return !origin || isLoopbackOriginValue(origin) || isSameOriginAsRequest(req, origin) || isExtraAllowedOrigin(origin, config);
15061515
}
15071516

15081517
export function corsHeaders(req?: Request, config?: OcxConfig): Record<string, string> {

0 commit comments

Comments
 (0)