Skip to content

Commit

Permalink
fix(base64): validate base64 strings (#2779)
Browse files Browse the repository at this point in the history
This updates the node and browser base64 parsers to validate the
input string before parsing.
  • Loading branch information
JordonPhillips authored Sep 14, 2021
1 parent ec03fa0 commit 2b336a3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/util-base64-browser/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@ describe("fromBase64", () => {
it("should convert double padded base64 strings to Uint8Arrays", () => {
expect(fromBase64(b64DoublePadded)).toEqual(doublePadded);
});

describe("should reject invalid base64 strings", () => {
it.each(["Rg", "Rg=", "[][]", "-_=="])("rejects '%s'", (value) => {
expect(() => fromBase64(value)).toThrowError();
});
});
});
5 changes: 5 additions & 0 deletions packages/util-base64-browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export function fromBase64(input: string): Uint8Array {
let bitLength = 0;
for (let j = i, limit = i + 3; j <= limit; j++) {
if (input[j] !== "=") {
// If we don't check for this, we'll end up using undefined in a bitwise
// operation, in which it will be treated as 0.
if (!(input[j] in alphabetByEncoding)) {
throw new TypeError(`Invalid character ${input[j]} in base64 string.`);
}
bits |= alphabetByEncoding[input[j]] << ((limit - j) * bitsPerLetter);
bitLength += bitsPerLetter;
} else {
Expand Down
6 changes: 6 additions & 0 deletions packages/util-base64-node/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ describe("fromBase64", () => {
it("should throw when given a number", () => {
expect(() => fromBase64(0xdeadbeefface as any)).toThrow();
});

describe("should reject invalid base64 strings", () => {
it.each(["Rg", "Rg=", "[][]", "-_=="])("rejects '%s'", (value) => {
expect(() => fromBase64(value)).toThrowError();
});
});
});
16 changes: 16 additions & 0 deletions packages/util-base64-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import { fromArrayBuffer, fromString } from "@aws-sdk/util-buffer-from";

const BASE64_REGEX = /^[A-Za-z0-9+/]*={0,2}$/;

/**
* Converts a base-64 encoded string to a Uint8Array of bytes using Node.JS's
* `buffer` module.
*
* @param input The base-64 encoded string
*/
export function fromBase64(input: string): Uint8Array {
// Node's buffer module allows padding to be omitted, but we want to enforce
// it. So here we ensure that the input represents a number of bits divisible
// by 8. Each character represents 6 bits, so after reducing the fraction we
// end up mulitplying by 3/4 and checking for a remainder.
if ((input.length * 3) % 4 !== 0) {
throw new TypeError(`Incorrect padding on base64 string.`);
}

// Node will just ingore invalid characters, so we need to make sure they're
// properly rejected.
if (!BASE64_REGEX.exec(input)) {
throw new TypeError(`Invalid base64 string.`);
}

const buffer = fromString(input, "base64");

return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
Expand Down

0 comments on commit 2b336a3

Please sign in to comment.