Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 516dcc5

Browse files
committed
Correctly handle internationalised domain name routes, closes #186
1 parent 05cc8ef commit 516dcc5

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

packages/core/src/router.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { URL } from "url";
1+
import { URL, domainToUnicode } from "url";
22
import { MiniflareError } from "@miniflare/shared";
33

44
export 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("*");

packages/core/test/router.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
});
6172
test("Router: route paths may end with *", (t) => {
6273
const router = new Router();
6374
router.update(new Map([["a", ["https://example.com/path*"]]]));

0 commit comments

Comments
 (0)