Skip to content

Commit

Permalink
Use Host header for AWS SigV4 signing (#97)
Browse files Browse the repository at this point in the history
* resolves #96

* added tests to reproduce issue #96

* executed webpack
  • Loading branch information
rgrygorovych authored Apr 2, 2024
1 parent 358d316 commit 79a5893
Show file tree
Hide file tree
Showing 24 changed files with 73 additions and 24 deletions.
2 changes: 1 addition & 1 deletion dist/aws.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/aws.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/event-bridge.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/event-bridge.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/kinesis.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/kinesis.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/kms.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/kms.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lambda.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lambda.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/s3.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/s3.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/secrets-manager.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/secrets-manager.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/signature.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/signature.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/sqs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/sqs.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ssm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ssm.js.map

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions src/internal/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ export class SignatureV4 {
// Standard headers like content-type are optional.
// For HTTP/2 requests, you must include the :authority header instead of
// the host header. Different services might require other headers."
request.headers[constants.HOST_HEADER] = request.endpoint.hostname
if (!request.headers[constants.HOST_HEADER]) {
request.headers[constants.HOST_HEADER] = request.endpoint.hostname
}


// Filter out headers that will be generated and managed by the signing process.
// If the user provide any of those as part of the HTTPRequest's headers, they
Expand Down Expand Up @@ -214,7 +217,9 @@ export class SignatureV4 {
// Standard headers like content-type are optional.
// For HTTP/2 requests, you must include the :authority header instead of
// the host header. Different services might require other headers."
request.headers[constants.HOST_HEADER] = originalRequest.endpoint.hostname
if (!request.headers[constants.HOST_HEADER]) {
request.headers[constants.HOST_HEADER] = originalRequest.endpoint.hostname
}

// If the user provided a session token, include it in the signed url query string.
if (this.credentials.sessionToken) {
Expand Down
44 changes: 44 additions & 0 deletions tests/internal/signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ export function signatureV4TestSuite() {
)
})

describe('#sign should sign requests to target endpoint different from host (proxy use case)', () => {
const { headers } = signer.sign(
{
method: 'POST',
endpoint: new Endpoint('https://target-foo.us-bar-1.amazonaws.com'),
path: '/',
headers: {
host: 'foo.us-bar-1.amazonaws.com',
},
},
{
signingDate: new Date('2000-01-01T00:00:00Z'),
}
)

expect(headers[AUTHORIZATION_HEADER]).to.equal(
'AWS4-HMAC-SHA256 Credential=foo/20000101/us-bar-1/foo/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=1e3b24fcfd7655c0c245d99ba7b6b5ca6174eab903ebfbda09ce457af062ad30'
)
})

describe('#sign should support overriding region and service in the signer instance', () => {
const signer = new SignatureV4({
credentials: credentials,
Expand Down Expand Up @@ -409,6 +429,30 @@ export function signatureV4TestSuite() {
})
})

describe('should presign requests to target endpoint different from host (proxy use case)', () => {
const { query } = signer.presign(
{
method: 'POST',
endpoint: new Endpoint('https://target-foo.us-bar-1.amazonaws.com'),
path: '/',
headers: {
host: 'foo.us-bar-1.amazonaws.com',
},
},
presigningOptions
)

expect(query).to.deep.equal({
[AMZ_ALGORITHM_QUERY_PARAM]: SIGNING_ALGORITHM_IDENTIFIER,
[AMZ_CREDENTIAL_QUERY_PARAM]: 'foo/20000101/us-bar-1/foo/aws4_request',
[AMZ_DATE_QUERY_PARAM]: '20000101T000000Z',
[AMZ_EXPIRES_QUERY_PARAM]: presigningOptions.expiresIn.toString(),
[AMZ_SIGNED_HEADERS_QUERY_PARAM]: HOST_HEADER,
[AMZ_SIGNATURE_QUERY_PARAM]:
'46f0091f3e84cbd4552a184f43830a4f8b42fd18ceaefcdc2c225be1efd9e00e',
})
})

describe('should sign request without hoisting some headers', () => {
const options = JSON.parse(JSON.stringify(presigningOptions))
options.unhoistableHeaders = new Set(['x-amz-not-hoisted'])
Expand Down

0 comments on commit 79a5893

Please sign in to comment.