From c9208f78ab94948102136aa1ac6d0ec8391e956d Mon Sep 17 00:00:00 2001 From: KVR Vishnu <46535652+VISHNU0906@users.noreply.github.com> Date: Tue, 1 Dec 2026 10:14:14 +0530 Subject: [PATCH] fix(encodings): refuse punctuation in Punycode digits The digit lookup relied on unsigned comparisons, as in the RFC 3492 sample code, but JavaScript subtraction is signed. Characters below '0', between '9' and 'A', and between 'Z' and 'a' came out as small or negative digits, so 'xn--mnchen-3y!' decoded to a wrong label instead of the 'non-basic digit' error. Check each digit range explicitly. --- src/engine/operations/encodings.ts | 8 +++++++- src/engine/tests/operations.verified.test.ts | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/engine/operations/encodings.ts b/src/engine/operations/encodings.ts index 8a60779..560d465 100644 --- a/src/engine/operations/encodings.ts +++ b/src/engine/operations/encodings.ts @@ -324,7 +324,13 @@ function decodePunycodeLabel(label: string): string { if (index >= encoded.length) throw new OperationError('Punycode input ends mid-sequence.'); const char = encoded.charCodeAt(index++); const digit = - char - 48 < 10 ? char - 22 : char - 65 < 26 ? char - 65 : char - 97 < 26 ? char - 97 : PUNY_BASE; + char >= 48 && char <= 57 + ? char - 22 + : char >= 65 && char <= 90 + ? char - 65 + : char >= 97 && char <= 122 + ? char - 97 + : PUNY_BASE; if (digit >= PUNY_BASE) throw new OperationError('Punycode contains a non-basic digit.'); i += digit * w; diff --git a/src/engine/tests/operations.verified.test.ts b/src/engine/tests/operations.verified.test.ts index 5f5dc5a..fb0b7aa 100644 --- a/src/engine/tests/operations.verified.test.ts +++ b/src/engine/tests/operations.verified.test.ts @@ -496,6 +496,13 @@ describe('edge cases the property tests turned up', () => { expect(await text('BB8\n', 'from-base45')).toBe('AB'); }); + it('Punycode refuses punctuation where a digit should be', async () => { + expect(await text('xn--mnchen-3YA', 'from-punycode')).toBe('münchen'); + expect(await refusal('xn--mnchen-3y!', 'from-punycode')).toMatch(/non-basic digit/); + expect(await refusal('xn--a:b', 'from-punycode')).toMatch(/non-basic digit/); + expect(await refusal('xn--[]', 'from-punycode')).toMatch(/non-basic digit/); + }); + it('Base58 turns each leading 1 into one zero byte, and nothing into nothing', async () => { expect(await bytesOut('', 'from-base58')).toBe(''); expect(toHex(await bytesOut('1', 'from-base58'))).toBe('00');