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

Append newline to end of extensionHeaders if necessary #1317

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 10 additions & 1 deletion lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -1252,13 +1252,22 @@ File.prototype.getSignedUrl = function(options, callback) {
return;
}

var extensionHeadersString = '';
if (options.extensionHeaders != null) {
for (var headerName in options.extensionHeaders) {
extensionHeadersString += format('{name}:{value}\n', {
name: headerName,
value: options.extensionHeaders[headerName],
})
}
}
var sign = crypto.createSign('RSA-SHA256');
sign.update([
options.action,
(options.contentMd5 || ''),
(options.contentType || ''),
expiresInSeconds,
(options.extensionHeaders || '') + options.resource
extensionHeadersString + options.resource,
].join('\n'));
var signature = sign.sign(credentials.private_key, 'base64');

Expand Down
33 changes: 33 additions & 0 deletions test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var stream = require('stream');
var through = require('through2');
var tmp = require('tmp');
var url = require('url');
var crypto = require('crypto');

var Bucket = require('../../lib/storage/bucket.js');
var ServiceObject = require('../../lib/common/service-object.js');
Expand Down Expand Up @@ -1913,6 +1914,38 @@ describe('File', function() {
}, /cannot be in the past/);
});
});

describe('extensionHeaders', function (done) {
it('should add headers to signature', function(done) {
var extensionHeaders = {
'x-goog-acl': 'public-read',
'x-foo': 'bar',
};

var sign = crypto.createSign('RSA-SHA256');
var expires = Date.now() + 5;
var expiresInSeconds = Math.round(expires / 1000);
var name = encodeURIComponent(directoryFile.name);
var resource = '/' + directoryFile.bucket.name + '/' + name;
sign.update([
'GET',
'',
'',
expiresInSeconds,
'x-goog-acl:public-read\nx-foo:bar\n' + resource
].join('\n'));
var expSignature = sign.sign(credentials.private_key, 'base64');

directoryFile.getSignedUrl({
action: 'read',
expires: expires,
extensionHeaders: extensionHeaders,
}, function(err, signedUrl) {
assert(signedUrl.indexOf(encodeURIComponent(expSignature)) > -1);
done();
});
});
});
});

describe('makePrivate', function() {
Expand Down