From 625c363842ffa3eb48e3c1c73bc79546c304a14c Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 9 Oct 2026 19:30:44 -0700 Subject: [PATCH] Apply constant-time string parsing. --- src/lib/primordials.ts | 32 ++++++++++++++++++++++++++------ test/node.mjs | 2 +- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/lib/primordials.ts b/src/lib/primordials.ts index ec734c2..636ec12 100644 --- a/src/lib/primordials.ts +++ b/src/lib/primordials.ts @@ -51,6 +51,7 @@ const TypedArray = Object.getPrototypeOf(Bytes.prototype) const bytesSet = Function.prototype.call.bind(Bytes.prototype.set) const bytesFill = Function.prototype.call.bind(Bytes.prototype.fill) const charCodeAt = Function.prototype.call.bind(String.prototype.charCodeAt) +const fromCharCode = Function.prototype.call.bind(String.fromCharCode) function get (target: object, key: PropertyKey): (this: unknown) => T { const found = Object.getOwnPropertyDescriptor(target, key) @@ -158,19 +159,38 @@ for (let i = 0; i < 16; i++) { NIBBLE[UPPER[i]] = i } +/** 0-9a-f for a valid hex digit 0..15 */ +function hexcode (c: number): number { + const isDigit = (c - 10) >> 31 + const digit = (c + 0x30) & isDigit + const alpha = (c + 0x57) & ~isDigit + return digit | alpha +} + +/** 0..15 for a valid hex char code, else set bit 9 which is past 0xff */ +function nibble (c: number): number { + const isDigit = ((0x2f - c) & (c - 0x3a)) >> 31 + const digit = (c - 0x30) & isDigit + c |= 0x20 + const isAlpha = ((0x60 - c) & (c - 0x67)) >> 31 + const alpha = (c - 0x57) & isAlpha + const valid = ~(isDigit | isAlpha) + return digit | alpha | valid +} + /** Lowercase hexadecimal encoding of `bytes`. */ export function bytesToHex (bytes: Uint8Array): string { const byteLength = getByteLength(bytes) let hex = '' for (let i = 0; i < byteLength; i++) { const byte = bytes[i] - hex += LOWER[byte >> 4] + LOWER[byte & 15] + hex += fromCharCode(hexcode(byte >> 4), hexcode(byte & 15)) } return hex } /** - * Decodes hex string to Bytes.. + * Decodes hex string to Bytes. */ function hexToBytes (hex: string): Bytes { const length = hex.length @@ -180,12 +200,12 @@ function hexToBytes (hex: string): Bytes { const bytes = new Bytes(length >> 1) let d = 0 for (let i = 0, b = 0; i < length; i += 2, b++) { - const hi = charCodeAt(hex, i) - const lo = charCodeAt(hex, i + 1) + const hi = nibble(charCodeAt(hex, i)) + const lo = nibble(charCodeAt(hex, i + 1)) d |= hi | lo - bytes[b] = (NIBBLE[hi] << 4) | NIBBLE[lo] + bytes[b] = (hi << 4) | lo } - if (d & 0x100) { + if (d >> 4) { fill(bytes, 0) throw new TypeError('Invalid hexadecimal character') } diff --git a/test/node.mjs b/test/node.mjs index b1f8225..ab17c13 100644 --- a/test/node.mjs +++ b/test/node.mjs @@ -39,7 +39,7 @@ try { result = false } catch (err) { if (err != null && typeof err === 'object' && 'message' in err) { - result = err.message === 'Invalid hexadecimal character z' + result = err.message === 'Invalid hexadecimal character' } else { result = false } -- 2.52.0