From 4ca4dba97665e800f2bcaa70afa9abc88d0f47c9 Mon Sep 17 00:00:00 2001 From: tate Date: Mon, 11 Sep 2023 10:28:03 +1000 Subject: [PATCH] fix: manual exeption for onion/onion3 encoding length --- packages/ensjs/src/utils/contentHash.test.ts | 6 ++++++ packages/ensjs/src/utils/contentHash.ts | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/packages/ensjs/src/utils/contentHash.test.ts b/packages/ensjs/src/utils/contentHash.test.ts index e47facd5..fdca9c6b 100644 --- a/packages/ensjs/src/utils/contentHash.test.ts +++ b/packages/ensjs/src/utils/contentHash.test.ts @@ -129,4 +129,10 @@ describe('encodeContentHash', () => { it.each(displayArray)('$input => $encoded', ({ input, encoded }) => { expect(encodeContentHash(input)).toEqual(encoded) }) + it('fails to encode onion with invalid length', () => { + expect(() => encodeContentHash('onion://123')).toThrow() + }) + it('fails to encode onion3 with invalid length', () => { + expect(() => encodeContentHash('onion3://123')).toThrow() + }) }) diff --git a/packages/ensjs/src/utils/contentHash.ts b/packages/ensjs/src/utils/contentHash.ts index 18778913..3f95e7ea 100644 --- a/packages/ensjs/src/utils/contentHash.ts +++ b/packages/ensjs/src/utils/contentHash.ts @@ -92,5 +92,11 @@ export function encodeContentHash(text: string): Hex { const internalCodec = getInternalCodec(typeData.protocolType) + // manual exceptions for onion/onion3 which are just utf8 encoded + if (internalCodec === 'onion' && typeData.decoded.length !== 16) + throw new InvalidContentHashError() + if (internalCodec === 'onion3' && typeData.decoded.length !== 56) + throw new InvalidContentHashError() + return `0x${encode(internalCodec, typeData.decoded)}` }