From b98572e8ac33054412a766152270384f2332665c Mon Sep 17 00:00:00 2001 From: shuse2 Date: Mon, 5 Jun 2023 14:48:18 +0200 Subject: [PATCH 1/3] :bug: Update isHexString to be strict on length --- elements/lisk-validator/src/validation.ts | 5 +++++ elements/lisk-validator/test/validation.spec.ts | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/elements/lisk-validator/src/validation.ts b/elements/lisk-validator/src/validation.ts index 14e32dbc45e..e674fe7381a 100644 --- a/elements/lisk-validator/src/validation.ts +++ b/elements/lisk-validator/src/validation.ts @@ -74,6 +74,11 @@ export const isHexString = (data: unknown): boolean => { return false; } + // if data is not empty, and length is not even, it's not valid hex string + if (data.length > 0 && data.length % 2 !== 0) { + return false; + } + return data === '' || /^[a-f0-9]+$/i.test(data); }; diff --git a/elements/lisk-validator/test/validation.spec.ts b/elements/lisk-validator/test/validation.spec.ts index f49e3ee8694..4b35d2cbff7 100644 --- a/elements/lisk-validator/test/validation.spec.ts +++ b/elements/lisk-validator/test/validation.spec.ts @@ -79,7 +79,7 @@ describe('validation', () => { describe('#isHexString', () => { it('should return true when valid hex was provided', () => { return expect( - isHexString('215b667a32a5cd51a94c9c2046c11fffb08c65748febec099451e3b164452bc'), + isHexString('215b667a32a5cd51a94c9c2046c11fffb08c65748febec099451e3b164452b'), ).toBeTrue(); }); @@ -92,6 +92,14 @@ describe('validation', () => { isHexString('zzzzzzza32a5cd51a94c9c2046c11fffb08c65748febec099451e3b164452bc'), ).toBeFalse(); }); + + it('should return true when input is empty', () => { + return expect(isHexString('')).toBeTrue(); + }); + + it('should return false when input length is odd', () => { + return expect(isHexString('ff1')).toBeFalse(); + }); }); describe('#isEncryptedPassphrase', () => { From 870bf5af2ed422f0c7f3f5fc146876292800c163 Mon Sep 17 00:00:00 2001 From: shuse2 Date: Tue, 6 Jun 2023 06:29:48 +0200 Subject: [PATCH 2/3] :recycle: Update to use regex --- elements/lisk-validator/src/validation.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/elements/lisk-validator/src/validation.ts b/elements/lisk-validator/src/validation.ts index e674fe7381a..9ebea404ce5 100644 --- a/elements/lisk-validator/src/validation.ts +++ b/elements/lisk-validator/src/validation.ts @@ -74,12 +74,7 @@ export const isHexString = (data: unknown): boolean => { return false; } - // if data is not empty, and length is not even, it's not valid hex string - if (data.length > 0 && data.length % 2 !== 0) { - return false; - } - - return data === '' || /^[a-f0-9]+$/i.test(data); + return data === '' || /^([0-9a-fA-F]{2})+$/i.test(data); }; export const isEncryptedPassphrase = (data: string): boolean => { From c77427e155a9c57f758c6c6416e599c0193e5429 Mon Sep 17 00:00:00 2001 From: shuse2 Date: Tue, 6 Jun 2023 09:00:16 +0200 Subject: [PATCH 3/3] Update to simplify regex and add additional test --- elements/lisk-validator/src/validation.ts | 2 +- elements/lisk-validator/test/validation.spec.ts | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/elements/lisk-validator/src/validation.ts b/elements/lisk-validator/src/validation.ts index 9ebea404ce5..726d4ac1f07 100644 --- a/elements/lisk-validator/src/validation.ts +++ b/elements/lisk-validator/src/validation.ts @@ -74,7 +74,7 @@ export const isHexString = (data: unknown): boolean => { return false; } - return data === '' || /^([0-9a-fA-F]{2})+$/i.test(data); + return data === '' || /^([0-9a-f]{2})+$/i.test(data); }; export const isEncryptedPassphrase = (data: string): boolean => { diff --git a/elements/lisk-validator/test/validation.spec.ts b/elements/lisk-validator/test/validation.spec.ts index 4b35d2cbff7..cab25f30f98 100644 --- a/elements/lisk-validator/test/validation.spec.ts +++ b/elements/lisk-validator/test/validation.spec.ts @@ -78,9 +78,12 @@ describe('validation', () => { describe('#isHexString', () => { it('should return true when valid hex was provided', () => { - return expect( + expect( isHexString('215b667a32a5cd51a94c9c2046c11fffb08c65748febec099451e3b164452b'), ).toBeTrue(); + expect( + isHexString('215B667A32a5cd51A94c9c2046c11fffb08c65748febec099451e3b164452B'), + ).toBeTrue(); }); it('should return false when number was provided', () => {