{"record":{"id":"0372215b4900bb38","repo":"zloirock/core-js","slug":"padding-is-too-early","errorCode":null,"errorMessage":"Padding is too early","messagePattern":"Padding is too early","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"packages/core-js/internals/uint8-from-base64.js","lineNumber":116,"sourceCode":"          break;\n        }\n        if (lastChunkHandling === 'loose') {\n          if (chunk.length === 1) {\n            throw new SyntaxError('Malformed padding: exactly one additional character');\n          }\n          written = writeBytes(bytes, decodeBase64Chunk(chunk, alphabet, false), written);\n        } else {\n          throw new SyntaxError('Missing padding');\n        }\n      }\n      read = stringLength;\n      break;\n    }\n    var chr = at(string, index);\n    ++index;\n    if (chr === '=') {\n      if (chunk.length < 2) {\n        throw new SyntaxError('Padding is too early');\n      }\n      index = skipAsciiWhitespace(string, index);\n      if (chunk.length === 2) {\n        if (index === stringLength) {\n          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);","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/zloirock/core-js/blob/84e45fba098dd3a177d5cf2247d06ab8e98d3790/packages/core-js/internals/uint8-from-base64.js#L98-L134","documentation":"Uint8Array.fromBase64() validates padding position per the base64 spec. A '=' terminator may only appear when the current 4-character chunk already encodes 2 or 3 bytes; if '=' is found in position 0 or 1 of a chunk (chunk.length < 2), the encoder that produced the string was broken and the data cannot be decoded, so the library throws this SyntaxError.","triggerScenarios":"Calling Uint8Array.fromBase64 (or Uint8Array.fromBase64Into) with a string where '=' appears as the first or second character of a base64 chunk, e.g. fromBase64('AB=C') or fromBase64('=ABC'), with lastChunkHandling 'loose' or 'strict' (in 'loose' any chunk, in 'strict' only terminal chunks are checked before this branch).","commonSituations":"Truncated or manually edited base64 strings, base64 strings that were sliced at arbitrary offsets destroying 4-character chunk alignment, hand-rolled encoders that emit padding incorrectly, or copying a string with characters missing from the middle.","solutions":["Check the input for '=' appearing within the first two positions of any 4-character group and fix or regenerate the base64 string.","Ensure the string length (ignoring ASCII whitespace) is a multiple of 4 and chunks are not misaligned from cutting/pasting.","Re-encode the original binary data with a correct base64 encoder instead of repairing the damaged string.","Wrap the call in try/catch for SyntaxError and surface a validation message to the user."],"exampleFix":"// before\nUint8Array.fromBase64('A=B CDEF'); // SyntaxError: Padding is too early\n// after\nconst input = 'ABC=DEF'; // '=' only after >= 2 chars of a chunk\nconst bytes = Uint8Array.fromBase64(input, { lastChunkHandling: 'strict' });","handlingStrategy":"validation","validationCode":"function hasValidPaddingPlacement(s) {\n  const compact = s.replace(/[\\t\\n\\f\\r ]/g, '');\n  const idx = compact.indexOf('=');\n  if (idx === -1) return true;\n  return idx % 4 >= 2; // '=' may only appear at position 2 or 3 of a chunk\n}\n// if (!hasValidPaddingPlacement(input)) throw new Error('bad base64 padding');","typeGuard":"function isDecodableBase64(s) {\n  return typeof s === 'string' && s.replace(/[\\t\\n\\f\\r ]/g, '').replace(/=/g, 'A').length % 4 === 0;\n}","tryCatchPattern":"try {\n  const bytes = Uint8Array.fromBase64(input, { lastChunkHandling: 'strict' });\n} catch (e) {\n  if (e instanceof SyntaxError) throw new TypeError('Invalid base64: padding appears too early');\n  throw e;\n}","preventionTips":["Validate padding position (index % 4 >= 2) before decoding untrusted input.","Never slice base64 strings at arbitrary offsets; chunks must stay 4-aligned.","Re-encode from source data rather than patching damaged strings.","Round-trip test encoders against atob/btoa to catch malformed padding early."],"tags":["base64","syntax-error","validation","padding"],"backgroundTag":"malformed-base64-padding","analyzedSha":"84e45fba098dd3a177d5cf2247d06ab8e98d3790","analyzedAt":"2026-08-30T20:36:10.323Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}