From 15063c68bbd4f6918648d9f23c069861d39dad98 Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Tue, 11 Jun 2024 14:33:45 +1000 Subject: [PATCH] fix(aens): validate minus chars in name as node does --- src/tx/builder/helpers.ts | 9 +++++++++ test/unit/aens.ts | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/tx/builder/helpers.ts b/src/tx/builder/helpers.ts index 0a1c0693a9..f959e821d8 100644 --- a/src/tx/builder/helpers.ts +++ b/src/tx/builder/helpers.ts @@ -70,6 +70,15 @@ export function nameToPunycode(maybeName: string): AensName { if (/\p{Emoji_Presentation}/u.test(name)) { throw new ArgumentError('aens name', 'not containing emoji', maybeName); } + if (name[2] === '-' && name[3] === '-') { + throw new ArgumentError('aens name', 'without "-" char in both the third and fourth positions', maybeName); + } + if (name[0] === '-') { + throw new ArgumentError('aens name', 'starting with no "-" char', maybeName); + } + if (name.at(-1) === '-') { + throw new ArgumentError('aens name', 'ending with no "-" char', maybeName); + } let punycode; try { const u = new URL(`http://${name}.${suffix}`); diff --git a/test/unit/aens.ts b/test/unit/aens.ts index 5f03383635..fad5510260 100644 --- a/test/unit/aens.ts +++ b/test/unit/aens.ts @@ -295,6 +295,18 @@ describe('AENS utils', () => { expect(() => ensureName('ldiDxa1Yxy1iiTRztYEN4F8nrnfZib3Q1MllPghmst8fjJ1sI3DXzOoAddE2ETxp.chain')) .to.throw(ArgumentError, 'aens name should be not too long, got ldiDxa1Yxy1iiTRztYEN4F8nrnfZib3Q1MllPghmst8fjJ1sI3DXzOoAddE2ETxp.chain instead'); }); + + it('fails if name starts or ends with minus', () => { + expect(() => ensureName('-test.chain')) + .to.throw(ArgumentError, 'aens name should be starting with no "-" char, got -test.chain instead'); + expect(() => ensureName('test-.chain')) + .to.throw(ArgumentError, 'aens name should be ending with no "-" char, got test-.chain instead'); + }); + + it('fails if name has minus at 2, 3 chars', () => { + expect(() => ensureName('te--st.chain')) + .to.throw(ArgumentError, 'aens name should be without "-" char in both the third and fourth positions, got te--st.chain instead'); + }); }); describe('isAuctionName', () => {