Skip to content

Commit

Permalink
Merge pull request #25 from MeirionHughes/master
Browse files Browse the repository at this point in the history
fix: validate encode input and ensure zero-filled Buffer
  • Loading branch information
brianloveswords authored May 15, 2018
2 parents d6a814c + b94ec27 commit 4fbd954
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ before_install:
node_js:
- "6"
- "4"
- "0.12"
- "0.10"
6 changes: 3 additions & 3 deletions src/base64url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ function encode(input: string | Buffer, encoding: string = "utf8"): string {
if (Buffer.isBuffer(input)) {
return fromBase64(input.toString("base64"));
}
return fromBase64(new Buffer(input as string, encoding).toString("base64"));
return fromBase64(Buffer.from(input as string, encoding).toString("base64"));
};

function decode(base64url: string, encoding: string = "utf8"): string {
return new Buffer(toBase64(base64url), "base64").toString(encoding);
return Buffer.from(toBase64(base64url), "base64").toString(encoding);
}

function toBase64(base64url: string | Buffer): string {
Expand All @@ -30,7 +30,7 @@ function fromBase64(base64: string): string {
}

function toBuffer(base64url: string): Buffer {
return new Buffer(toBase64(base64url), "base64");
return Buffer.from(toBase64(base64url), "base64");
}

export interface Base64Url {
Expand Down
2 changes: 1 addition & 1 deletion src/pad-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function padString(input: string): string {
let position = stringLength;
let padLength = segmentLength - diff;
let paddedStringLength = stringLength + padLength;
let buffer = new Buffer(paddedStringLength);
let buffer = Buffer.alloc(paddedStringLength);

buffer.write(input);

Expand Down
16 changes: 16 additions & 0 deletions test/base64url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,19 @@ test('from base64url to buffer', function (t) {
t.same(result, testBuffer, 'should be able to convert to buffer');
t.end();
});

test('encode validates input', function (t) {
const b64url = base64url(testBuffer, 'binary');

var result = undefined;

try {
base64url.encode(1000);
} catch (err) {
result = err;
}

t.not(result, undefined, 'should validate encode input is string or Buffer');
t.end();
});

0 comments on commit 4fbd954

Please sign in to comment.