{"record":{"id":"7b930ca8e095f47c","repo":"beekeeper-studio/beekeeper-studio","slug":"string-should-only-contain-hex-characters","errorCode":null,"errorMessage":"String should only contain hex characters","messagePattern":"String should only contain hex characters","errorType":"validation","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"apps/ui-kit/lib/utils/binary.ts","lineNumber":40,"sourceCode":"var NOT_HEX = /[^\\da-f]/i;\nvar exec = uncurryThis(NOT_HEX.exec);\nvar stringSlice = uncurryThis(''.slice);\n\n/**\n  * Extracted from https://github.com/zloirock/core-js/blob/master/packages/core-js/internals/uint8-from-hex.js\n  * FIXME we don't need this soon after `UInt8Array.prototype.fromHex` is\n  * implemented. See https://github.com/tc39/proposal-arraybuffer-base64\n  */\nexport function hexToUint8Array(string: string, into?: any): Uint8Array {\n  var stringLength = string.length;\n  if (stringLength % 2 !== 0) throw new SyntaxError('String should be an even number of characters');\n  var maxLength = into ? min(into.length, stringLength / 2) : stringLength / 2;\n  var bytes = into || new Uint8Array(maxLength);\n  var read = 0;\n  var written = 0;\n  while (written < maxLength) {\n    var hexits = stringSlice(string, read, read += 2);\n    if (exec(NOT_HEX, hexits)) throw new SyntaxError('String should only contain hex characters');\n    bytes[written++] = parseInt(hexits, 16);\n  }\n  return bytes\n}\n","sourceCodeStart":22,"sourceCodeEnd":45,"githubUrl":"https://github.com/beekeeper-studio/beekeeper-studio/blob/4e3e03e3223b2b71a1af8926d455f533b80af837/apps/ui-kit/lib/utils/binary.ts#L22-L45","documentation":"hexToUint8Array checks each two-character slice against a NOT_HEX regex as it decodes. A SyntaxError is thrown when any slice contains characters outside [0-9a-fA-F], since only hex digits can be converted to bytes.","triggerScenarios":"Passing a string containing non-hex characters such as '0x1a2b' (0x prefix), whitespace, '-', 'zz', or UUID formatting ('1a2b-3c4d') to hexToUint8Array.","commonSituations":"Hex values copied with '0x' prefixes or separators; database drivers returning bytea/hex with escapes; user-pasted keys containing spaces or typos; forgetting that binary output is uppercase-safe but not separator-safe.","solutions":["Sanitize input before calling: strip '0x' prefix, whitespace, and separators (e.g. string.replace(/0x|[\\s-]/g, '')).","Pre-validate with /^[0-9a-fA-F]+$/.test(hex) and show a validation error for user input.","Fix the data source emitting non-hex encodings (e.g. decode base64 or escapes first).","Wrap in try/catch to convert the SyntaxError into a friendly message."],"exampleFix":"// before\nconst bytes = hexToUint8Array(rawHex); // \"0x1a2b-3c\"\n// after\nconst cleanHex = rawHex.replace(/^0x|[\\s-]/g, '');\nif (!/^[0-9a-fA-F]+$/.test(cleanHex)) throw new TypeError('invalid hex');\nconst bytes = hexToUint8Array(cleanHex);","handlingStrategy":"validation","validationCode":"const clean = hex.replace(/^0x/[i], '').replace(/[\\s_-]/g, '');\nif (!/^[0-9a-fA-F]+$/.test(clean)) throw new TypeError('invalid hex characters');","typeGuard":"function isHexString(s: unknown): s is string {\n  return typeof s === 'string' && /^[0-9a-fA-F]+$/.test(s);\n}","tryCatchPattern":"try {\n  const bytes = hexToUint8Array(hex);\n} catch (e) {\n  if (e instanceof SyntaxError && /hex characters/.test(e.message)) {\n    throw new TypeError(`Non-hex input: ${JSON.stringify(hex)}`);\n  }\n  throw e;\n}","preventionTips":["Strip '0x' prefixes, whitespace, and separators before decoding","Pre-validate with a strict /^[0-9a-fA-F]+$/ regex on all user or external input","Confirm the data source encoding (base64, escaped bytea) and decode before treating it as hex"],"tags":["binary","hex","parsing","validation"],"backgroundTag":"invalid-hex-string","analyzedSha":"4e3e03e3223b2b71a1af8926d455f533b80af837","analyzedAt":"2026-08-31T23:21:23.379Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}