diff --git a/aws4.js b/aws4.js index 0cff0f0..baeaee2 100644 --- a/aws4.js +++ b/aws4.js @@ -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) @@ -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) diff --git a/test/fast.js b/test/fast.js index fcb2734..72345c0 100644 --- a/test/fast.js +++ b/test/fast.js @@ -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() { @@ -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() {