zloirock/core-js · error · SyntaxError

Padding is too early

Error message

Padding is too early

What it means

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.

Source

Thrown at packages/core-js/internals/uint8-from-base64.js:116

          break;
        }
        if (lastChunkHandling === 'loose') {
          if (chunk.length === 1) {
            throw new SyntaxError('Malformed padding: exactly one additional character');
          }
          written = writeBytes(bytes, decodeBase64Chunk(chunk, alphabet, false), written);
        } else {
          throw new SyntaxError('Missing padding');
        }
      }
      read = stringLength;
      break;
    }
    var chr = at(string, index);
    ++index;
    if (chr === '=') {
      if (chunk.length < 2) {
        throw new SyntaxError('Padding is too early');
      }
      index = skipAsciiWhitespace(string, index);
      if (chunk.length === 2) {
        if (index === stringLength) {
          if (lastChunkHandling === 'stop-before-partial') {
            break;
          }
          throw new SyntaxError('Malformed padding: only one =');
        }
        if (at(string, index) === '=') {
          ++index;
          index = skipAsciiWhitespace(string, index);
        }
      }
      if (index < stringLength) {
        throw new SyntaxError('Unexpected character after padding');
      }
      written = writeBytes(bytes, decodeBase64Chunk(chunk, alphabet, lastChunkHandling === 'strict'), written);

View on GitHub (pinned to 84e45fba09)

Solutions

  1. Check the input for '=' appearing within the first two positions of any 4-character group and fix or regenerate the base64 string.
  2. Ensure the string length (ignoring ASCII whitespace) is a multiple of 4 and chunks are not misaligned from cutting/pasting.
  3. Re-encode the original binary data with a correct base64 encoder instead of repairing the damaged string.
  4. Wrap the call in try/catch for SyntaxError and surface a validation message to the user.

Example fix

// before
Uint8Array.fromBase64('A=B CDEF'); // SyntaxError: Padding is too early
// after
const input = 'ABC=DEF'; // '=' only after >= 2 chars of a chunk
const bytes = Uint8Array.fromBase64(input, { lastChunkHandling: 'strict' });
Defensive patterns

Strategy: validation

Validate before calling

function hasValidPaddingPlacement(s) {
  const compact = s.replace(/[\t\n\f\r ]/g, '');
  const idx = compact.indexOf('=');
  if (idx === -1) return true;
  return idx % 4 >= 2; // '=' may only appear at position 2 or 3 of a chunk
}
// if (!hasValidPaddingPlacement(input)) throw new Error('bad base64 padding');

Type guard

function isDecodableBase64(s) {
  return typeof s === 'string' && s.replace(/[\t\n\f\r ]/g, '').replace(/=/g, 'A').length % 4 === 0;
}

Try / catch

try {
  const bytes = Uint8Array.fromBase64(input, { lastChunkHandling: 'strict' });
} catch (e) {
  if (e instanceof SyntaxError) throw new TypeError('Invalid base64: padding appears too early');
  throw e;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of zloirock/core-js@84e45fba09 (2026-08-30). Data as JSON: /api/errors/0372215b4900bb38. Report an issue: GitHub.