Skip to content
Open
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
23 changes: 23 additions & 0 deletions aws4.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ function RequestSigner(request, credentials) {

if (typeof request === 'string') request = url.parse(request)

request = this.sanitizeHost(request);
if (request.headers)
request.headers = this.sanitizeHost(request.headers);

var headers = request.headers = (request.headers || {}),
hostParts = this.matchHost(request.hostname || request.host || headers.Host || headers.host)

Expand Down Expand Up @@ -56,6 +60,25 @@ function RequestSigner(request, credentials) {
this.isCodeCommitGit = this.service === 'codecommit' && request.method === 'GIT'
}

RequestSigner.prototype.sanitizeHost = function(object) {
// Object will be headers || request
if (object.host) {
object.host = this.removeProtocol(object.host);
}
if (object.Host) {
object.Host = this.removeProtocol(object.Host);
}
if (object.hostname) {
object.hostname = this.removeProtocol(object.hostname);
}

return object;
}

RequestSigner.prototype.removeProtocol = function(url) {
return url.replace('http://', '').replace('https://', '');
}

RequestSigner.prototype.matchHost = function(host) {
var match = (host || '').match(/([^\.]+)\.(?:([^\.]*)\.)?amazonaws\.com$/)
var hostParts = (match || []).slice(1, 3)
Expand Down
14 changes: 14 additions & 0 deletions test/fast.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ describe('aws4', function() {
'SignedHeaders=date;host;x-amz-content-sha256;x-amz-date, ' +
'Signature=6fda8a58c01edfcb6773c15ad5a276a893ce52978a8f5cd1705fae14df78cfd4')
})
it('should remove protocol from host / hostname', function() {
var host = 'sqs.us-east-1.amazonaws.com';
var opts = aws4.sign({hostname: 'https://'+host, headers: {Date: date}})
opts.headers['Host'].should.equal(host)
opts.headers['X-Amz-Date'].should.equal(iso)
opts.headers.Authorization.should.equal(auth)
})
})

describe('#sign() with host', function() {
Expand All @@ -183,6 +190,13 @@ describe('aws4', function() {
'SignedHeaders=date;host;x-amz-content-sha256;x-amz-date, ' +
'Signature=6fda8a58c01edfcb6773c15ad5a276a893ce52978a8f5cd1705fae14df78cfd4')
})
it('should remove protocol from host', function() {
var host = 'sqs.us-east-1.amazonaws.com';
var opts = aws4.sign({host: 'https://'+host, headers: {Date: date}})
opts.headers['Host'].should.equal(host)
opts.headers['X-Amz-Date'].should.equal(iso)
opts.headers.Authorization.should.equal(auth)
})
})

describe('#sign() with body', function() {
Expand Down