Skip to content

Commit

Permalink
fix: create correct v4 signed url with cname (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkwlui authored Sep 30, 2019
1 parent 3351c7b commit ace3b5e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2587,7 +2587,8 @@ class File extends ServiceObject<File> {
}

const extensionHeaders = Object.assign({}, config.extensionHeaders);
extensionHeaders.host = 'storage.googleapis.com';
const fqdn = new url.URL(config.cname || STORAGE_DOWNLOAD_BASE_URL);
extensionHeaders.host = fqdn.host;
if (config.method === 'POST') {
extensionHeaders['x-goog-resumable'] = 'start';
}
Expand Down Expand Up @@ -2625,7 +2626,7 @@ class File extends ServiceObject<File> {

const canonicalRequest = [
config.method,
config.resource,
config.cname ? `/${config.name}` : config.resource,
canonicalQueryParams,
extensionHeadersString,
signedHeaders,
Expand Down
23 changes: 23 additions & 0 deletions test/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3040,6 +3040,29 @@ describe('File', () => {
}
);
});

it('should generate v4 signed url with provided cname', done => {
const host = 'http://www.example.com';

file.getSignedUrl(
{
action: 'read',
cname: host,
version: 'v4',
expires: Date.now() + 2000,
},
(err: Error, signedUrl: string) => {
assert.ifError(err);
const expected =
'http://www.example.com/file-name.png?' +
'X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=client-email' +
'%2F20190318%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20190318T000000Z&' +
'X-Goog-Expires=2&X-Goog-SignedHeaders=host&X-Goog-Signature=b228276adbab';
assert.strictEqual(signedUrl, expected);
done();
}
);
});
});

describe('promptSaveAs', () => {
Expand Down

0 comments on commit ace3b5e

Please sign in to comment.