File tree Expand file tree Collapse file tree 1 file changed +13
-1
lines changed
Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments