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

Do not add standard ports to host prior to signing. Fixes #40 #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
17 changes: 16 additions & 1 deletion aws4.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ function RequestSigner(request, credentials) {
headers.Host = request.hostname || request.host || this.createHost()

// If a port is specified explicitly, use it as is
if (request.port)
// As long as its not a default for the expected HTTP type (80 or 443)
if (request.port && !this.isStandardPort(request.port))
headers.Host += ':' + request.port
}
if (!request.hostname && !request.host)
Expand Down Expand Up @@ -321,3 +322,17 @@ aws4.RequestSigner = RequestSigner
aws4.sign = function(request, credentials) {
return new RequestSigner(request, credentials).sign()
}


/**
* Checks if port is a known "standard" port for HTTP calls (80 or 443 at this time)
* @param port - the port to check, either string or number
* @returns {Boolean}
*/
RequestSigner.prototype.isStandardPort = function (port) {
if (!port) return false

if (typeof port === 'string') port = parseInt(port, 10)

return port === 443 || port === 80
}
66 changes: 66 additions & 0 deletions test/fast.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,48 @@ describe('aws4', function() {
var signer = new RequestSigner({service: 'codecommit', method: 'GIT', host: 'example.com'})
signer.canonicalString().should.match(/\n$/)
})

it('should recognise number 80 as a standard port', function() {
var signer = new RequestSigner('https://email.us-west-2.amazonaws.com')
signer.isStandardPort(80).should.be.true();
})

it('should recognise string 80 as a standard port', function() {
var signer = new RequestSigner('https://email.us-west-2.amazonaws.com')
signer.isStandardPort('80').should.be.true();
})

it('should recognise number 443 as a standard port', function() {
var signer = new RequestSigner('https://email.us-west-2.amazonaws.com')
signer.isStandardPort(443).should.be.true();
})

it('should recognise string 443 as a standard port', function() {
var signer = new RequestSigner('https://email.us-west-2.amazonaws.com')
signer.isStandardPort('443').should.be.true();
})

it('should reject falsy as a standard port', function() {
var signer = new RequestSigner('https://email.us-west-2.amazonaws.com')
signer.isStandardPort().should.be.false();
signer.isStandardPort(null).should.be.false();
signer.isStandardPort(false).should.be.false();
})

it('should reject a number as a standard port', function() {
var signer = new RequestSigner('https://email.us-west-2.amazonaws.com')
signer.isStandardPort(9000).should.be.false();
})

it('should reject a string as a standard port', function() {
var signer = new RequestSigner('https://email.us-west-2.amazonaws.com')
signer.isStandardPort('9000').should.be.false();
})

it('should reject a non-numeric string as a standard port', function() {
var signer = new RequestSigner('https://email.us-west-2.amazonaws.com')
signer.isStandardPort('word').should.be.false();
})
})

describe('#sign() with no credentials', function() {
Expand Down Expand Up @@ -183,6 +225,30 @@ describe('aws4', function() {
'SignedHeaders=date;host;x-amz-content-sha256;x-amz-date, ' +
'Signature=6fda8a58c01edfcb6773c15ad5a276a893ce52978a8f5cd1705fae14df78cfd4')
})
it('should handle custom port as string matching http default correctly', function() {
var opts = aws4.sign({host: 'localhost', port: '80', service: 's3', headers: {Date: date}})
opts.headers['X-Amz-Date'].should.equal(iso)
opts.headers.Authorization.should.equal(
'AWS4-HMAC-SHA256 Credential=ABCDEF/20121226/us-east-1/s3/aws4_request, ' +
'SignedHeaders=date;host;x-amz-content-sha256;x-amz-date, ' +
'Signature=84e1967e2785821bc4a523fb4365c9f90007834e018ec6e1e9ae1b838fa90046')
})
it('should handle custom port as number matching http default correctly', function() {
var opts = aws4.sign({host: 'localhost', port: 80, service: 's3', headers: {Date: date}})
opts.headers['X-Amz-Date'].should.equal(iso)
opts.headers.Authorization.should.equal(
'AWS4-HMAC-SHA256 Credential=ABCDEF/20121226/us-east-1/s3/aws4_request, ' +
'SignedHeaders=date;host;x-amz-content-sha256;x-amz-date, ' +
'Signature=84e1967e2785821bc4a523fb4365c9f90007834e018ec6e1e9ae1b838fa90046')
})
it('should handle custom port matching https default correctly', function() {
var opts = aws4.sign({host: 'localhost', port: '443', service: 's3', headers: {Date: date}})
opts.headers['X-Amz-Date'].should.equal(iso)
opts.headers.Authorization.should.equal(
'AWS4-HMAC-SHA256 Credential=ABCDEF/20121226/us-east-1/s3/aws4_request, ' +
'SignedHeaders=date;host;x-amz-content-sha256;x-amz-date, ' +
'Signature=84e1967e2785821bc4a523fb4365c9f90007834e018ec6e1e9ae1b838fa90046')
})
})

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