diff --git a/src/engine/operations/encodings.ts b/src/engine/operations/encodings.ts index b2cc7e6..a90236c 100644 --- a/src/engine/operations/encodings.ts +++ b/src/engine/operations/encodings.ts @@ -328,7 +328,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 72c4830..4bb28ff 100644 --- a/src/engine/tests/operations.verified.test.ts +++ b/src/engine/tests/operations.verified.test.ts @@ -510,6 +510,13 @@ describe('edge cases the property tests turned up', () => { expect(await refusal('%%%%%', ['from-base85', { Alphabet: 'Z85' }])).toMatch(/out of range/); }); + 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');