Relax ENS normalize for double-hyphen to only throw on punycode conflicts (#42, #2376, #2754).

This commit is contained in:
Richard Moore 2022-08-16 17:00:34 -04:00
parent d9897e0fdb
commit fce9aaa734

@ -14,7 +14,7 @@ Zeros.fill(0);
function checkComponent(comp: Uint8Array): Uint8Array { function checkComponent(comp: Uint8Array): Uint8Array {
if (comp.length === 0) { throw new Error("invalid ENS name; empty component"); } if (comp.length === 0) { throw new Error("invalid ENS name; empty component"); }
let nonUnder = false; let nonUnder = false;
let last = -1; let allAscii = true;
for (let i = 0; i < comp.length; i++) { for (let i = 0; i < comp.length; i++) {
const c = comp[i]; const c = comp[i];
@ -22,14 +22,19 @@ function checkComponent(comp: Uint8Array): Uint8Array {
if (c === 0x5f) { if (c === 0x5f) {
if (nonUnder) { throw new Error("invalid ENS name; non-prefix underscore"); } if (nonUnder) { throw new Error("invalid ENS name; non-prefix underscore"); }
} else { } else {
// A hyphen (i.e. "-"); only allows a single in a row // Non-ASCII byte
if (c === 0x2d && last === c) { if (c & 0x80) { allAscii = false; }
throw new Error("invalid ENS name; double-hyphen");
} // Non-underscore found
nonUnder = true; 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; return comp;
} }
@ -98,4 +103,3 @@ export function dnsEncode(name: string): string {
}))) + "00"; }))) + "00";
} }