From 8442685a82a877891be5b6c7e9e6d0e53b637705 Mon Sep 17 00:00:00 2001 From: Michael Hart Date: Tue, 21 May 2024 15:29:01 +1000 Subject: [PATCH] Copy headers (shallow) so the original is not modified Fixes #164 Also update mocha to 10.4 --- aws4.js | 2 +- package.json | 2 +- test/fast.js | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/aws4.js b/aws4.js index b0cfb7c..6737574 100644 --- a/aws4.js +++ b/aws4.js @@ -46,7 +46,7 @@ function RequestSigner(request, credentials) { if (typeof request === 'string') request = url.parse(request) - var headers = request.headers = (request.headers || {}), + var headers = request.headers = Object.assign({}, (request.headers || {})), hostParts = (!this.service || !this.region) && this.matchHost(request.hostname || request.host || headers.Host || headers.host) this.request = request diff --git a/package.json b/package.json index f23d0db..4e710f9 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "integration": "node ./test/slow.js" }, "devDependencies": { - "mocha": "^10.2.0", + "mocha": "^10.4.0", "should": "^13.2.3" } } diff --git a/test/fast.js b/test/fast.js index 171c115..1a88db4 100644 --- a/test/fast.js +++ b/test/fast.js @@ -274,6 +274,27 @@ describe('aws4', function() { }) }) + describe('#sign() with copies', function() { + it('should modify request/opts in place and return same object', function() { + var opts = {service: 'sqs'} + var newOpts = aws4.sign(opts) + opts.should.equal(newOpts) + }) + + it('should not modify existing headers', function() { + var headers = { + 'Content-Type': 'application/x-amz-json-1.0', + 'X-Amz-Target': 'DynamoDB_20120810.ListTables', + } + var opts = aws4.sign({ + service: 'dynamodb', + headers: headers, + body: '{}', + }) + opts.headers.should.not.equal(headers) + }) + }) + describe('#sign() with signQuery', function() { it('should work with standard services', function() { var opts = aws4.sign({