From 43c5d03e049764969f2771db9a96aa8b3d82b07f Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 6 Jul 2026 00:20:21 -0700 Subject: [PATCH] Improve regex matching. --- src/lib/nano25519.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/lib/nano25519.ts b/src/lib/nano25519.ts index 3a328e9..3c99075 100644 --- a/src/lib/nano25519.ts +++ b/src/lib/nano25519.ts @@ -155,14 +155,13 @@ const nano25519_init = (bytes: number[]): { derive: typeof derive, sign: typeof throw new TypeError(`Invalid byte length for ${name}`) } if (typeof value === 'string') { - const regex = RegExp(`^[A-Fa-f0-9]{${byteLengthMin << 1},${byteLengthMax << 1}}$`) - if (!regex.test(value)) { + if (/[^0-9a-f]/i.test(value)) { throw new TypeError(`Invalid hexadecimal characters in ${name} ${value}`) } - if (value.length & 1) { - throw new TypeError(`Invalid hexadecimal length for ${name} ${value}`) + if (value.length & 1 || value.length < (byteLengthMin << 1) || value.length > (byteLengthMax << 1)) { + throw new TypeError(`Invalid hexadecimal length ${value.length} for ${name} ${value}`) } - value = new Uint8Array(value.match(/[A-Fa-f0-9]{2}/g)?.map(b => parseInt(b, 16)) || []) + value = new Uint8Array(value.match(/[0-9a-f]{2}/gi)?.map(b => parseInt(b, 16)) || []) } if (value instanceof ArrayBuffer) { value = new Uint8Array(value) @@ -441,7 +440,7 @@ export async function run (data: Record } try { const result = await dispatch(data) - if (typeof result === 'string' && !/^([a-f0-9]{64}|[a-f0-9]{128})$/i.test(result)) { + if (typeof result === 'string' && !/^([0-9a-f]{64}|[0-9a-f]{128})$/i.test(result)) { throw new Error(result) } return result -- 2.52.0