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: validate encode input and ensure zero-filled Buffer #25

Merged
merged 3 commits into from
May 15, 2018
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
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

depreciated since node 4. Warning in Node 10. likely unsupported in Node 11

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();
});