From fce9aaa7345a001a4a56bce66298ee23948d120c Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Tue, 16 Aug 2022 17:00:34 -0400 Subject: [PATCH] Relax ENS normalize for double-hyphen to only throw on punycode conflicts (#42, #2376, #2754). --- packages/hash/src.ts/namehash.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/hash/src.ts/namehash.ts b/packages/hash/src.ts/namehash.ts index ded377e0d4..953ff99041 100644 --- a/packages/hash/src.ts/namehash.ts +++ b/packages/hash/src.ts/namehash.ts @@ -14,7 +14,7 @@ Zeros.fill(0); function checkComponent(comp: Uint8Array): Uint8Array { if (comp.length === 0) { throw new Error("invalid ENS name; empty component"); } let nonUnder = false; - let last = -1; + let allAscii = true; for (let i = 0; i < comp.length; i++) { const c = comp[i]; @@ -22,14 +22,19 @@ function checkComponent(comp: Uint8Array): Uint8Array { if (c === 0x5f) { if (nonUnder) { throw new Error("invalid ENS name; non-prefix underscore"); } } else { - // A hyphen (i.e. "-"); only allows a single in a row - if (c === 0x2d && last === c) { - throw new Error("invalid ENS name; double-hyphen"); - } + // Non-ASCII byte + if (c & 0x80) { allAscii = false; } + + // Non-underscore found nonUnder = true; } - last = c; } + + // Prevent punycode-looking components + if (allAscii && comp[2] === 0x2d && comp[3] === 0x2d) { + throw new Error("invalid ENS name; punycode conflict"); + } + return comp; } @@ -98,4 +103,3 @@ export function dnsEncode(name: string): string { }))) + "00"; } -