Skip to content

Commit

Permalink
fix: getSigned(Policy|Url) throws if expiration is invalid Date (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus authored and JustinBeckwith committed Feb 22, 2019
1 parent f6acc87 commit 1c3bd03
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2045,6 +2045,10 @@ class File extends ServiceObject<File> {
const callback = args.callback;
const expires = new Date((options as GetSignedPolicyOptions).expires);

if (isNaN(expires.getTime())) {
throw new Error('The expiration date provided was invalid.');
}

if (expires.valueOf() < Date.now()) {
throw new Error('An expiration date cannot be in the past.');
}
Expand Down Expand Up @@ -2262,6 +2266,10 @@ class File extends ServiceObject<File> {
void|Promise<GetSignedUrlResponse> {
const expiresInMSeconds = new Date(cfg.expires).valueOf();

if (isNaN(expiresInMSeconds)) {
throw new Error('The expiration date provided was invalid.');
}

if (expiresInMSeconds < Date.now()) {
throw new Error('An expiration date cannot be in the past.');
}
Expand Down
25 changes: 25 additions & 0 deletions test/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,18 @@ describe('File', () => {
});
});

it('should throw if a date is invalid', () => {
const expires = new Date('31-12-2019');

assert.throws(() => {
file.getSignedPolicy(
{
expires,
},
() => {});
}, /The expiration date provided was invalid\./);
});

it('should throw if a date from the past is given', () => {
const expires = Date.now() - 5;

Expand Down Expand Up @@ -2554,6 +2566,19 @@ describe('File', () => {
});
});

it('should throw if a date is invalid', () => {
const expires = new Date('31-12-2019');

assert.throws(() => {
file.getSignedUrl(
{
action: 'read',
expires,
},
() => {});
}, /The expiration date provided was invalid\./);
});

it('should throw if a date from the past is given', () => {
const expires = Date.now() - 5;

Expand Down

0 comments on commit 1c3bd03

Please sign in to comment.