From d3b76f0c6f85cdb6b5c67c11455986abbe2c6b7b Mon Sep 17 00:00:00 2001 From: Will Toozs Date: Tue, 9 Jul 2024 11:27:40 +0200 Subject: [PATCH] fixup: bucketName source + error if no key field --- lib/api/api.js | 8 +++- lib/api/objectPost.js | 6 ++- .../aws-node-sdk/test/object/post.js | 48 +++++++++++++++++++ 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/lib/api/api.js b/lib/api/api.js index 74c3dc2ad0..b5c55ed03f 100644 --- a/lib/api/api.js +++ b/lib/api/api.js @@ -217,9 +217,13 @@ const api = { formDataParser.on('file', (fieldname, file, filename, encoding, mimetype) => { fileEventData = { fieldname, file, filename, encoding, mimetype }; - return next(null); + // no key field == error + if ('key' in request.formData) { + return next(null); + } + return next(errors.InvalidArgument.customizeDescription('Bucket POST must contain' + + " a field named 'key'. If it is specified, please check the order of the fields.")); }); - formDataParser.on('finish', () => { // No file field == error if (!fileEventData) { diff --git a/lib/api/objectPost.js b/lib/api/objectPost.js index 95b0db335f..eb08510ca2 100644 --- a/lib/api/objectPost.js +++ b/lib/api/objectPost.js @@ -37,14 +37,16 @@ function objectPost(authInfo, request, streamingV4Params, log, callback) { const { headers, method, + formData, + bucketName, } = request; let parsedContentLength = 0; const passThroughStream = new PassThrough(); const requestType = request.apiMethods || 'objectPost'; const valParams = { authInfo, - bucketName: request.formData.bucket, - objectKey: request.formData.key, + bucketName, + objectKey: formData.key, requestType, request, }; diff --git a/tests/functional/aws-node-sdk/test/object/post.js b/tests/functional/aws-node-sdk/test/object/post.js index 965d29801a..703220a9b0 100644 --- a/tests/functional/aws-node-sdk/test/object/post.js +++ b/tests/functional/aws-node-sdk/test/object/post.js @@ -388,6 +388,54 @@ describe('POST object', () => { }); }); + it('should handle error when key is missing', done => { + const { bucketName, url } = testContext; + // Prep fields then remove the key field + let fields = calculateFields(ak, sk, bucketName); + fields = fields.filter(e => e.name !== 'key'); + + const formData = new FormData(); + + fields.forEach(field => { + formData.append(field.name, field.value); + }); + + formData.append('file', fs.createReadStream(path.join(__dirname, filename))); + + formData.getLength((err, length) => { + if (err) { + return done(err); + } + + return axios.post(url, formData, { + headers: { + ...formData.getHeaders(), + 'Content-Length': length, + }, + }) + .then(() => { + done(new Error('Request should not succeed without key field')); + }) + .catch(err => { + assert.ok(err.response, 'Error should be returned by axios'); + + // Parse the XML error response + xml2js.parseString(err.response.data, (err, result) => { + if (err) { + return done(err); + } + + const error = result.Error; + assert.equal(error.Code[0], 'InvalidArgument'); + assert.equal(error.Message[0], + 'Bucket POST must contain a field named ' + + "'key'. If it is specified, please check the order of the fields."); + return done(); + }); + }); + }); + }); + it('should upload an object with key slash', done => { const { bucketName, url } = testContext; const slashKey = '/';