From cd2fe1e440bc33f77e16297fa4d0027a7b2dda72 Mon Sep 17 00:00:00 2001 From: Eric Mustin Date: Wed, 18 Mar 2020 02:00:47 +0100 Subject: [PATCH] Fix for s3.createBucket mutates args (#3138) Fixes #3112 --- .changes/next-release/bugfix-s3-a752bb97.json | 5 ++++ lib/services/s3.js | 8 ++++-- test/services/s3.spec.js | 28 +++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 .changes/next-release/bugfix-s3-a752bb97.json diff --git a/.changes/next-release/bugfix-s3-a752bb97.json b/.changes/next-release/bugfix-s3-a752bb97.json new file mode 100644 index 0000000000..01f3fb1740 --- /dev/null +++ b/.changes/next-release/bugfix-s3-a752bb97.json @@ -0,0 +1,5 @@ +{ + "type": "bugfix", + "category": "s3", + "description": "createBucket mutates params argument when endpoint is configured by appending CreateBucketConfigurationon key, this side effect is now fixed" +} \ No newline at end of file diff --git a/lib/services/s3.js b/lib/services/s3.js index 69553e3c1c..e27aa112b4 100644 --- a/lib/services/s3.js +++ b/lib/services/s3.js @@ -1314,10 +1314,14 @@ AWS.util.update(AWS.S3.prototype, { params = {}; } var hostname = this.endpoint.hostname; + // copy params so that appending keys does not unintentioinallly + // mutate params object argument passed in by user + var copiedParams = AWS.util.copy(params); + if (hostname !== this.api.globalEndpoint && !params.CreateBucketConfiguration) { - params.CreateBucketConfiguration = { LocationConstraint: this.config.region }; + copiedParams.CreateBucketConfiguration = { LocationConstraint: this.config.region }; } - return this.makeRequest('createBucket', params, callback); + return this.makeRequest('createBucket', copiedParams, callback); }, /** diff --git a/test/services/s3.spec.js b/test/services/s3.spec.js index 9a9fe3a4cc..32d7a7625e 100644 --- a/test/services/s3.spec.js +++ b/test/services/s3.spec.js @@ -2391,6 +2391,34 @@ describe('AWS.S3', function() { done(); }); }); + + it('appends CreateBucketConfiguration key to copy of params, not original, when endpoint is configured', function(done) { + var loc = null; + var params = { + Bucket: 'name' + }; + + s3 = new AWS.S3({ + region: 'eu-west-1', + Bucket: 'name', + endpoint: 'https://foo.bar.baz:555/prefix' + }); + + s3.makeRequest = function(op, copiedParams, cb) { + expect(copiedParams).to['be'].a('object'); + loc = copiedParams.CreateBucketConfiguration.LocationConstraint; + if (typeof cb === 'function') { + return cb(); + } + }; + + helpers.mockHttpResponse(200, {}, ''); + s3.createBucket(params, function() { + expect(params.CreateBucketConfiguration).to.not.exist; + expect(loc).to.be.a.string; + done(); + }); + }); }); describe('deleteBucket', function() {