This repository was archived by the owner on Mar 13, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +23
-3
lines changed Expand file tree Collapse file tree 2 files changed +23
-3
lines changed Original file line number Diff line number Diff line change 1- import { URL } from "url" ;
1+ import { URL , domainToUnicode } from "url" ;
22import { MiniflareError } from "@miniflare/shared" ;
33
44export type RouterErrorCode = "ERR_QUERY_STRING" | "ERR_INFIX_WILDCARD" ;
@@ -35,10 +35,19 @@ export class Router {
3535
3636 const protocol = hasProtocol ? url . protocol : undefined ;
3737
38- const allowHostnamePrefix = url . hostname . startsWith ( "*" ) ;
38+ const internationalisedAllowHostnamePrefix =
39+ url . hostname . startsWith ( "xn--*" ) ;
40+ const allowHostnamePrefix =
41+ url . hostname . startsWith ( "*" ) || internationalisedAllowHostnamePrefix ;
3942 const anyHostname = url . hostname === "*" ;
4043 if ( allowHostnamePrefix && ! anyHostname ) {
41- url . hostname = url . hostname . substring ( 1 ) ;
44+ let hostname = url . hostname ;
45+ // If hostname is internationalised (e.g. `xn--gld-tna.se`), decode it
46+ if ( internationalisedAllowHostnamePrefix ) {
47+ hostname = domainToUnicode ( hostname ) ;
48+ }
49+ // Remove leading "*"
50+ url . hostname = hostname . substring ( 1 ) ;
4251 }
4352
4453 const allowPathSuffix = url . pathname . endsWith ( "*" ) ;
Original file line number Diff line number Diff line change @@ -58,6 +58,17 @@ test("Router: route hostnames may begin with *", (t) => {
5858 t . is ( router . match ( new URL ( "https://example.com/" ) ) , null ) ;
5959 t . is ( router . match ( new URL ( "https://www.example.com/" ) ) , "a" ) ;
6060} ) ;
61+ test ( "Router: correctly handles internationalised domain names beginning with *" , ( t ) => {
62+ // https://github.com/cloudflare/miniflare/issues/186
63+ const router = new Router ( ) ;
64+ router . update ( new Map ( [ [ "a" , [ "*glöd.se/*" ] ] ] ) ) ;
65+ t . is ( router . match ( new URL ( "https://glöd.se/*" ) ) , "a" ) ;
66+ t . is ( router . match ( new URL ( "https://www.glöd.se/*" ) ) , "a" ) ;
67+
68+ router . update ( new Map ( [ [ "a" , [ "*.glöd.se/*" ] ] ] ) ) ;
69+ t . is ( router . match ( new URL ( "https://glöd.se/*" ) ) , null ) ;
70+ t . is ( router . match ( new URL ( "https://www.glöd.se/*" ) ) , "a" ) ;
71+ } ) ;
6172test ( "Router: route paths may end with *" , ( t ) => {
6273 const router = new Router ( ) ;
6374 router . update ( new Map ( [ [ "a" , [ "https://example.com/path*" ] ] ] ) ) ;
You can’t perform that action at this time.
0 commit comments