Skip to content

Commit a3da3f3

Browse files
Security Fix: Enforce strict hex validation (TOB-21)
1 parent 26f8954 commit a3da3f3

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

src/primitives/hex.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
// Accepts empty string because empty byte arrays are valid in Bitcoin.
44
const PURE_HEX_REGEX = /^[0-9a-fA-F]*$/
55

6-
export function assertValidHex(msg: string): void {
6+
export function assertValidHex (msg: string): void {
77
if (typeof msg !== 'string') {
8-
console.error("assertValidHex FAIL (non-string):", msg)
8+
console.error('assertValidHex FAIL (non-string):', msg)
99
throw new Error('Invalid hex string')
1010
}
1111

1212
// allow empty
1313
if (msg.length === 0) return
1414

1515
if (!PURE_HEX_REGEX.test(msg)) {
16-
console.error("assertValidHex FAIL (bad hex):", msg)
16+
console.error('assertValidHex FAIL (bad hex):', msg)
1717
throw new Error('Invalid hex string')
1818
}
1919
}
2020

21-
export function normalizeHex(msg: string): string {
22-
assertValidHex(msg);
21+
export function normalizeHex (msg: string): string {
22+
assertValidHex(msg)
2323

2424
// If empty, return empty — never force to "00"
2525
if (msg.length === 0) return ''

src/primitives/utils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import BigNumber from './BigNumber.js'
22
import { hash256 } from './Hash.js'
3-
import { assertValidHex, normalizeHex } from './hex.js'
3+
import { assertValidHex } from './hex.js'
44

55
const BufferCtor =
66
typeof globalThis !== 'undefined' ? (globalThis as any).Buffer : undefined
77
const CAN_USE_BUFFER =
88
BufferCtor != null && typeof BufferCtor.from === 'function'
9-
const PURE_HEX_REGEX = /^[0-9a-fA-F]+$/
109

1110
/**
1211
* Prepends a '0' to an odd character length word to ensure it has an even number of characters.
@@ -232,7 +231,7 @@ export const toUTF8 = (arr: number[]): string => {
232231
result += String.fromCharCode(byte1)
233232
continue
234233
}
235-
const emitReplacement = () => {
234+
const emitReplacement = (): void => {
236235
result += replacementChar
237236
}
238237
if (byte1 >= 0xc0 && byte1 <= 0xdf) {

0 commit comments

Comments
 (0)