{"record":{"id":"d52fbffd1e1a0789","repo":"zloirock/core-js","slug":"unexpected-character","errorCode":null,"errorMessage":"Unexpected character","messagePattern":"Unexpected character","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"packages/core-js/internals/uint8-from-base64.js","lineNumber":139,"sourceCode":"          if (lastChunkHandling === 'stop-before-partial') {\n            break;\n          }\n          throw new SyntaxError('Malformed padding: only one =');\n        }\n        if (at(string, index) === '=') {\n          ++index;\n          index = skipAsciiWhitespace(string, index);\n        }\n      }\n      if (index < stringLength) {\n        throw new SyntaxError('Unexpected character after padding');\n      }\n      written = writeBytes(bytes, decodeBase64Chunk(chunk, alphabet, lastChunkHandling === 'strict'), written);\n      read = stringLength;\n      break;\n    }\n    if (!hasOwn(alphabet, chr)) {\n      throw new SyntaxError('Unexpected character');\n    }\n    var remainingBytes = maxLength - written;\n    if (remainingBytes === 1 && chunk.length === 2 || remainingBytes === 2 && chunk.length === 3) {\n      // special case: we can fit exactly the number of bytes currently represented by chunk, so we were just checking for `=`\n      break;\n    }\n\n    chunk += chr;\n    if (chunk.length === 4) {\n      written = writeBytes(bytes, decodeBase64Chunk(chunk, alphabet, false), written);\n      chunk = '';\n      read = index;\n      if (written === maxLength) {\n        break;\n      }\n    }\n  }\n  if (!into) bytes.length = written;","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/zloirock/core-js/blob/84e45fba098dd3a177d5cf2247d06ab8e98d3790/packages/core-js/internals/uint8-from-base64.js#L121-L157","documentation":"fromBase64 only accepts valid base64 characters from the chosen alphabet (standard or base64url, plus '=' padding, with whitespace allowed). When the current character is not found in the alphabet map (hasOwn check fails), the library throws this SyntaxError, per the spec's requirement to reject invalid characters rather than skip them.","triggerScenarios":"Calling Uint8Array.fromBase64 with characters outside the alphabet: '-' or '_' when using the standard alphabet, '+' or '/' with alphabet: 'base64url', or any stray character like quotes, '<', ',', or a leading BOM/data-URI prefix such as 'data:text/plain;base64,'.","commonSituations":"Passing a full data URL instead of the bare base64 payload, base64url-encoded tokens decoded without { alphabet: 'base64url' }, JSON/CSV delimiters accidentally included, or whitespace characters not covered when 'strict' handling disallows them mid-string.","solutions":["Strip the 'data:...;base64,' prefix and decode only the substring after the comma.","Pass { alphabet: 'base64url' } if the input uses '-' and '_' characters.","Sanitize the input: remove quotes, commas, and other delimiters; keep only A-Za-z0-9+/= (or -_ for base64url) plus allowed whitespace.","Wrap in try/catch for SyntaxError and log the offending character position for debugging."],"exampleFix":"// before\nconst bytes = Uint8Array.fromBase64(token); // token uses '-' and '_': Unexpected character\n// after\nconst bytes = Uint8Array.fromBase64(token, { alphabet: 'base64url', lastChunkHandling: 'strict' });","handlingStrategy":"type-guard","validationCode":"const BASE64_RE = /^[A-Za-z0-9+/=\\s]+$/;\nconst BASE64URL_RE = /^[A-Za-z0-9\\-_=\\s]+$/;\nfunction isBase64AlphabetSafe(s, url = false) {\n  if (typeof s !== 'string') return false;\n  const re = url ? BASE64URL_RE : BASE64_RE;\n  if (!re.test(s)) return false;\n  const hasUrl = /[-_]/.test(s), hasStd = /[+/]/.test(s);\n  return !(hasUrl && hasStd); // mixed alphabets are never valid\n}","typeGuard":"function isDecodableBase64String(s) {\n  return typeof s === 'string' &&\n    (/^[A-Za-z0-9+/=\\s]*$/.test(s) || /^[A-Za-z0-9\\-_=\\s]*$/.test(s));\n}","tryCatchPattern":"let bytes;\ntry {\n  bytes = Uint8Array.fromBase64(bare, { alphabet: isBase64Url(bare) ? 'base64url' : 'base64' });\n} catch (e) {\n  if (e instanceof SyntaxError && e.message === 'Unexpected character') {\n    throw new TypeError('Input contains characters outside the base64 alphabet');\n  }\n  throw e;\n}","preventionTips":["Strip data-URI prefixes ('data:...;base64,') before decoding.","Detect and pass alphabet: 'base64url' when the string contains '-' or '_'.","Sanitize delimiters (quotes, commas) from JSON/CSV extracts.","Pre-validate with a strict regex before calling fromBase64 on untrusted input."],"tags":["base64","syntax-error","invalid-characters","encoding"],"backgroundTag":"invalid-base64-characters","analyzedSha":"84e45fba098dd3a177d5cf2247d06ab8e98d3790","analyzedAt":"2026-08-30T20:36:10.323Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}