Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(base64): Reject too-short encodings #1991

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/base64/src/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ export const jsDecodeBase64 = (string, name = '<unknown>') => {
i += 1;
}

while (i < string.length && quantum % 8 !== 0) {
if (string[i] !== padding) {
while (quantum > 0) {
if (i === string.length || string[i] !== padding) {
throw Error(`Missing padding at offset ${i} of string ${name}`);
}
// We MAY reject non-zero padding bits, but choose not to.
// https://datatracker.ietf.org/doc/html/rfc4648#section-3.5
i += 1;
quantum += 6;
quantum -= 2;
}

if (i < string.length) {
Expand Down
37 changes: 37 additions & 0 deletions packages/base64/test/test-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,40 @@ test('bytes conversions', t => {
t.is(atob(btoa(str)), str, `${str} round trips with atob(btoa)`);
}
});

test('invalid encodings', t => {
const badInputs = [
['%', /Invalid base64 character %/],
['=', undefined], // this input is bad in multiple ways

['Z%', /Invalid base64 character %/],
['Z', /Missing padding at offset 1/],
['Z=', /Missing padding at offset 2/],
['Z=%', /Missing padding at offset 2/],
['Z==%', /Missing padding at offset 3/],
['Z==m', /Missing padding at offset 3/],

['Zg%', /Invalid base64 character %/],
['Zg', /Missing padding at offset 2/],
['Zg=', /Missing padding at offset 3/],
['Zg=%', /Missing padding at offset 3/],
['Zg==%', /trailing garbage %/],
['Zg==m', /trailing garbage m/],

['Zm8%', /Invalid base64 character %/],
['Zm8', /Missing padding at offset 3/],
// not invalid: 'Zm8='
['Zm8=%', /trailing garbage %/],
['Zm8==%', /trailing garbage =%/],
['Zm8==m', /trailing garbage =m/],

// non-zero padding bits (MAY reject): ['Qf==', ...],
];
for (const [badInput, message] of badInputs) {
t.throws(
() => decodeBase64(badInput),
message && { message },
`${badInput} is rejected`,
);
}
});
Loading