Skip to content

Commit 58f2484

Browse files
committed
fix(server): enhance CORS origin matching with wildcard support
1 parent db088d2 commit 58f2484

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

apps/server/server/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ function corsMaxAge(): number {
9797
return process.env.CORS_MAX_AGE ? parseInt(process.env.CORS_MAX_AGE, 10) : 86400;
9898
}
9999

100+
/**
101+
* Check if a request origin matches an allowed origin pattern.
102+
* Supports simple wildcard `*` matching (e.g. `http://localhost:*`
103+
* matches `http://localhost:5173`).
104+
*/
105+
function originMatches(pattern: string, origin: string): boolean {
106+
if (pattern === origin) return true;
107+
if (!pattern.includes('*')) return false;
108+
const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*');
109+
return new RegExp(`^${escaped}$`).test(origin);
110+
}
111+
100112
/**
101113
* Resolve the `Access-Control-Allow-Origin` value for a given request.
102114
*
@@ -125,7 +137,7 @@ function resolveAllowOrigin(requestOrigin: string | null): string | null {
125137
? envOrigin.split(',').map((s: string) => s.trim()).filter(Boolean)
126138
: [envOrigin];
127139

128-
if (requestOrigin && allowed.includes(requestOrigin)) return requestOrigin;
140+
if (requestOrigin && allowed.some(pattern => originMatches(pattern, requestOrigin))) return requestOrigin;
129141
// Exact match with the single configured origin is allowed as a safe default
130142
if (allowed.length === 1 && !requestOrigin) return allowed[0];
131143
return null;

0 commit comments

Comments
 (0)