Skip to content
Astera Technology

Why did a malformed base 64 attachment value still decode?

Check the declared canonical encoding before accepting decoded bytes.

Download complete case notes and answer keys ↓

Read the source and its declared rule

The original contract permits canonical standard-alphabet base 64 with required padding. Q!UI= includes an unsupported symbol but the permissive Buffer path still yields AB. The correction checks grammar and re-encoding equality before accepting this bounded source.

Filled synthetic input
RecordInput JSON
Base input{"encoded":"Q!UI="}
Download editable CSV ↓

Compare the actual paths

Good output holds noncanonical Q!UI=. Decoded bytes alone cannot show that the source representation satisfied its agreed transfer contract.

Misleading and corrected outputs
PathOutput JSON
Misleading path{"accepted":true,"bytes":[65,66]}
Corrected path{"accepted":false,"error":"noncanonical-base64","original":"Q!UI="}
Download editable CSV ↓
Local Node.js program: 22.23.3 or 24.14.1
import {DatabaseSync} from 'node:sqlite';
import {TextDecoder} from 'node:util';
import {inflateSync} from 'node:zlib';
import {posix} from 'node:path';
import {createHash} from 'node:crypto';
const rows=(db,sql)=>db.prepare(sql).all().map(r=>({...r}));
const failed=(action)=>{try{action();return null;}catch(e){return {code:e.errcode??e.code??e.name};}};

import assert from 'node:assert/strict';
const run=function run(i,corrected=true){
    const bytes=Buffer.from(i.encoded,'base64');
    if(corrected&&(!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(i.encoded)||bytes.toString('base64')!==i.encoded))return {accepted:false,error:'noncanonical-base64',original:i.encoded};
    return {accepted:true,bytes:[...bytes]};
  };
const cases=[{"label":"misleading base","input":{"encoded":"Q!UI="},"expected":{"accepted":true,"bytes":[65,66]},"corrected":false},{"label":"corrected base","input":{"encoded":"Q!UI="},"expected":{"accepted":false,"error":"noncanonical-base64","original":"Q!UI="},"corrected":true},{"label":"canonical two-byte encoding","input":{"encoded":"QUI="},"expected":{"accepted":true,"bytes":[65,66]},"corrected":true},{"label":"canonical empty bytes","input":{"encoded":""},"expected":{"accepted":true,"bytes":[]},"corrected":true}];
for(const test of cases){
  const actual=run(test.input,test.corrected);
  assert.deepEqual(actual,test.expected,test.label);
  console.log(test.label,actual);
}
Download runnable example ↓

Check boundaries before the next load

QUI= yields [65,66]. The canonical empty encoding yields an empty byte array under this optional-payload rule.

Editable boundary records and expected outputs
CaseInput JSONExpected output JSON
Canonical two-byte encoding{"encoded":"QUI="}{"accepted":true,"bytes":[65,66]}
Canonical empty encoding{"encoded":""}{"accepted":true,"bytes":[]}
Download editable CSV ↓

Does producing AB make Q!UI= acceptable?

Show the worked answer

No. It violates the declared grammar. Hold the malformed value; canonical QUI= is accepted.

Related resources