diff --git a/constants.js b/constants.js index 1337f593e8..cd2110bbe6 100644 --- a/constants.js +++ b/constants.js @@ -255,6 +255,10 @@ const constants = { 'STREAMING-UNSIGNED-PAYLOAD-TRAILER', 'STREAMING-AWS4-HMAC-SHA256-PAYLOAD', ]), + // Content-Encoding element added by clients for V4 streaming uploads, + // which is removed before storing the object, as AWS does. See: + // http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-streaming.html + awsChunkedContentEncoding: 'aws-chunked', ipv4Regex: /^(\d{1,3}\.){3}\d{1,3}(\/(3[0-2]|[12]?\d))?$/, ipv6Regex: /^([\da-f]{1,4}:){7}[\da-f]{1,4}$/i, // The AWS assumed Role resource type diff --git a/lib/api/apiUtils/object/createAndStoreObject.js b/lib/api/apiUtils/object/createAndStoreObject.js index 542b9f5296..382753124b 100644 --- a/lib/api/apiUtils/object/createAndStoreObject.js +++ b/lib/api/apiUtils/object/createAndStoreObject.js @@ -8,7 +8,7 @@ const services = require('../../../services'); const { dataStore } = require('./storeObject'); const locationConstraintCheck = require('./locationConstraintCheck'); const { versioningPreprocessing, overwritingVersioning, decodeVID } = require('./versioning'); -const removeAWSChunked = require('./removeAWSChunked'); +const removeFromCommaSeparatedList = require('../../../utilities/removeFromCommaSeparatedList'); const getReplicationInfo = require('./getReplicationInfo'); const { config } = require('../../../Config'); const validateWebsiteHeader = require('./websiteServing') @@ -144,7 +144,7 @@ function createAndStoreObject(bucketName, bucketMD, objectKey, objMD, authInfo, metadataStoreParams.contentDisposition = request.headers['content-disposition']; metadataStoreParams.contentEncoding = - removeAWSChunked(request.headers['content-encoding']); + removeFromCommaSeparatedList(request.headers['content-encoding'], constants.awsChunkedContentEncoding); metadataStoreParams.expires = request.headers.expires; metadataStoreParams.tagging = request.headers['x-amz-tagging']; const defaultObjectLockConfiguration diff --git a/lib/api/apiUtils/object/removeAWSChunked.js b/lib/api/apiUtils/object/removeAWSChunked.js deleted file mode 100644 index 6319f0f553..0000000000 --- a/lib/api/apiUtils/object/removeAWSChunked.js +++ /dev/null @@ -1,15 +0,0 @@ -/** - * Checks for V4 streaming value 'aws-chunked' and removes it if present in - * Content-Encoding to be compatible with AWS behavior. See: - * http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-streaming.html - * @param {string} sourceHeader - Content-Encoding header from request headers - * @return {string} new value w. 'aws-chunked'/'aws-chunked,' substring removed - */ -function removeAWSChunked(sourceHeader) { - if (sourceHeader === undefined) { - return undefined; - } - return sourceHeader.replace(/aws-chunked,?/, ''); -} - -module.exports = removeAWSChunked; diff --git a/lib/api/objectCopy.js b/lib/api/objectCopy.js index bc25192ba3..56678bbaaf 100644 --- a/lib/api/objectCopy.js +++ b/lib/api/objectCopy.js @@ -14,7 +14,7 @@ const { data } = require('../data/wrapper'); const kms = require('../kms/wrapper'); const services = require('../services'); const { pushMetric } = require('../utapi/utilities'); -const removeAWSChunked = require('./apiUtils/object/removeAWSChunked'); +const removeFromCommaSeparatedList = require('../utilities/removeFromCommaSeparatedList'); const { standardMetadataValidateBucketAndObj } = require('../metadata/metadataUtils'); const validateWebsiteHeader = require('./apiUtils/object/websiteServing').validateWebsiteHeader; const { config } = require('../Config'); @@ -247,7 +247,10 @@ function _prepMetadata( contentMD5: sourceObjMD['content-md5'], cacheControl: headersToStoreSource['cache-control'], contentDisposition: headersToStoreSource['content-disposition'], - contentEncoding: removeAWSChunked(headersToStoreSource['content-encoding']), + // The source value may hold a separator left behind by an older + // aws-chunked removal, such as 'gzip,' + contentEncoding: removeFromCommaSeparatedList( + headersToStoreSource['content-encoding'], constants.awsChunkedContentEncoding, ''), dataStoreName: destLocationConstraintName, expires: headersToStoreSource.expires, overrideMetadata, diff --git a/lib/services.js b/lib/services.js index b85fcdaa8d..5de23039e1 100644 --- a/lib/services.js +++ b/lib/services.js @@ -13,7 +13,7 @@ const { config } = require('./Config'); const { data } = require('./data/wrapper'); const metadata = require('./metadata/wrapper'); const { setObjectLockInformation } = require('./api/apiUtils/object/objectLockHelpers'); -const removeAWSChunked = require('./api/apiUtils/object/removeAWSChunked'); +const removeFromCommaSeparatedList = require('./utilities/removeFromCommaSeparatedList'); const { parseTagFromQuery } = s3middleware.tagging; const usersBucket = constants.usersBucket; @@ -573,7 +573,8 @@ const services = { multipartObjectMD.uploadId = params.uploadId; multipartObjectMD['cache-control'] = params.headers['cache-control']; multipartObjectMD['content-disposition'] = params.headers['content-disposition']; - multipartObjectMD['content-encoding'] = removeAWSChunked(params.headers['content-encoding']); + multipartObjectMD['content-encoding'] = removeFromCommaSeparatedList( + params.headers['content-encoding'], constants.awsChunkedContentEncoding); multipartObjectMD['content-type'] = params.headers['content-type']; multipartObjectMD.expires = params.headers.expires; multipartObjectMD['x-amz-storage-class'] = params.storageClass; // TODO: removed CLDSRV-639 diff --git a/lib/utilities/collectResponseHeaders.js b/lib/utilities/collectResponseHeaders.js index a754300c62..8af39967f4 100644 --- a/lib/utilities/collectResponseHeaders.js +++ b/lib/utilities/collectResponseHeaders.js @@ -2,6 +2,7 @@ const { getVersionIdResHeader } = require('../api/apiUtils/object/versioning'); const checkUserMetadataSize = require('../api/apiUtils/object/checkUserMetadataSize'); const { getAmzRestoreResHeader } = require('../api/apiUtils/object/coldStorage'); +const removeFromCommaSeparatedList = require('./removeFromCommaSeparatedList'); const { config } = require('../Config'); const { getKeyIdFromArn } = require('arsenal/build/lib/network/KMSInterface'); @@ -68,8 +69,12 @@ function collectResponseHeaders(objectMD, corsHeaders, versioningCfg, responseMetaHeaders['Content-Disposition'] = objectMD['content-disposition']; } - if (objectMD['content-encoding']) { - responseMetaHeaders['Content-Encoding'] = objectMD['content-encoding']; + // Empty list elements are dropped from any stored value, so objects stored + // with a separator left behind by an older aws-chunked removal, such as + // 'gzip,', are returned as 'gzip' (RFC 9110, section 5.6.1) + const contentEncoding = removeFromCommaSeparatedList(objectMD['content-encoding'], ''); + if (contentEncoding) { + responseMetaHeaders['Content-Encoding'] = contentEncoding; } if (objectMD.expires) { responseMetaHeaders.Expires = objectMD.expires; diff --git a/lib/utilities/removeFromCommaSeparatedList.js b/lib/utilities/removeFromCommaSeparatedList.js new file mode 100644 index 0000000000..91a6427247 --- /dev/null +++ b/lib/utilities/removeFromCommaSeparatedList.js @@ -0,0 +1,21 @@ +/** + * Removes the given elements from a comma-separated header value, along with + * their separator, wherever they appear. The other elements are kept as sent. + * Pass '' to remove empty elements, which recipients must ignore + * (RFC 9110, section 5.6.1). + * @param {string} value - comma-separated header value + * @param {...string} elements - elements to remove, compared after trimming + * @return {string} the value without those elements, trimmed + */ +function removeFromCommaSeparatedList(value, ...elements) { + if (typeof value !== 'string') { + return value; + } + return value + .split(',') + .filter(item => !elements.includes(item.trim())) + .join(',') + .trim(); +} + +module.exports = removeFromCommaSeparatedList; diff --git a/tests/functional/aws-node-sdk/test/object/getMPU_compatibleHeaders.js b/tests/functional/aws-node-sdk/test/object/getMPU_compatibleHeaders.js index 22d1636c0d..5c380bbaec 100644 --- a/tests/functional/aws-node-sdk/test/object/getMPU_compatibleHeaders.js +++ b/tests/functional/aws-node-sdk/test/object/getMPU_compatibleHeaders.js @@ -21,7 +21,7 @@ describe('GET multipart upload object [Cache-Control, Content-Disposition, ' + let uploadId; const cacheControl = 'max-age=86400'; const contentDisposition = 'attachment; filename="fname.ext";'; - const contentEncoding = 'aws-chunked,gzip'; + const contentEncoding = 'gzip,aws-chunked'; // AWS Node SDK requires Date object, ISO-8601 string, or // a UNIX timestamp for Expires header const expires = new Date(); diff --git a/tests/functional/aws-node-sdk/test/object/objectCopy.js b/tests/functional/aws-node-sdk/test/object/objectCopy.js index c7b6ceedcc..59f4e27485 100644 --- a/tests/functional/aws-node-sdk/test/object/objectCopy.js +++ b/tests/functional/aws-node-sdk/test/object/objectCopy.js @@ -343,7 +343,7 @@ describe('Object Copy', () => { // Should remove V4 streaming value 'aws-chunked' // to be compatible with AWS behavior assert.strictEqual(res.ContentEncoding, - 'base64,' + 'base64' ); assert.strictEqual(res.Expires.toGMTString(), originalExpires.toGMTString()); @@ -484,7 +484,7 @@ describe('Object Copy', () => { newContentDisposition); // Should remove V4 streaming value 'aws-chunked' // to be compatible with AWS behavior - assert.strictEqual(res.ContentEncoding, 'gzip,'); + assert.strictEqual(res.ContentEncoding, 'gzip'); assert.strictEqual(res.Expires.toGMTString(), newExpires.toGMTString()); done(); @@ -538,7 +538,7 @@ describe('Object Copy', () => { assert.strictEqual(res.ContentDisposition, originalContentDisposition); assert.strictEqual(res.ContentEncoding, - 'base64,'); + 'base64'); assert.strictEqual(res.Expires.toGMTString(), originalExpires.toGMTString()); done(); diff --git a/tests/functional/aws-node-sdk/test/object/objectHead_compatibleHeaders.js b/tests/functional/aws-node-sdk/test/object/objectHead_compatibleHeaders.js index b4603858aa..0c889a87a9 100644 --- a/tests/functional/aws-node-sdk/test/object/objectHead_compatibleHeaders.js +++ b/tests/functional/aws-node-sdk/test/object/objectHead_compatibleHeaders.js @@ -62,7 +62,7 @@ describe('HEAD object, compatibility headers [Cache-Control, ' + // Should remove V4 streaming value 'aws-chunked' // to be compatible with AWS behavior assert.strictEqual(res.ContentEncoding, - 'gzip,'); + 'gzip'); assert.strictEqual(res.Expires.toGMTString(), expires.toGMTString()); return done(); diff --git a/tests/functional/aws-node-sdk/test/versioning/objectCopy.js b/tests/functional/aws-node-sdk/test/versioning/objectCopy.js index 7158aa0a42..6cbe27ec21 100644 --- a/tests/functional/aws-node-sdk/test/versioning/objectCopy.js +++ b/tests/functional/aws-node-sdk/test/versioning/objectCopy.js @@ -263,7 +263,7 @@ describe('Object Version Copy', () => { const res = await s3.send(new GetObjectCommand({ Bucket: destBucketName, Key: destObjName })); assert.strictEqual(res.CacheControl, originalCacheControl); assert.strictEqual(res.ContentDisposition, originalContentDisposition); - assert.strictEqual(res.ContentEncoding, 'base64,'); + assert.strictEqual(res.ContentEncoding, 'base64'); assert.strictEqual(res.Expires.toGMTString(), originalExpires.toGMTString()); }); @@ -332,7 +332,7 @@ describe('Object Version Copy', () => { const res = await s3.send(new GetObjectCommand({ Bucket: destBucketName, Key: destObjName })); assert.strictEqual(res.CacheControl, newCacheControl); assert.strictEqual(res.ContentDisposition, newContentDisposition); - assert.strictEqual(res.ContentEncoding, 'gzip,'); + assert.strictEqual(res.ContentEncoding, 'gzip'); assert.strictEqual(res.Expires.toGMTString(), newExpires.toGMTString()); }); @@ -361,7 +361,7 @@ describe('Object Version Copy', () => { const res = await s3.send(new GetObjectCommand({ Bucket: destBucketName, Key: destObjName })); assert.strictEqual(res.CacheControl, originalCacheControl); assert.strictEqual(res.ContentDisposition, originalContentDisposition); - assert.strictEqual(res.ContentEncoding, 'base64,'); + assert.strictEqual(res.ContentEncoding, 'base64'); assert.strictEqual(res.Expires.toGMTString(), originalExpires.toGMTString()); }); diff --git a/tests/unit/api/multipartUpload.js b/tests/unit/api/multipartUpload.js index 75bdc1b2e2..49036afb68 100644 --- a/tests/unit/api/multipartUpload.js +++ b/tests/unit/api/multipartUpload.js @@ -162,7 +162,7 @@ async function _uploadMpuObject(params = {}) { const _completeMultipartUpload = (...params) => util.promisify(cb => completeMultipartUpload(...params, (err, xml, headers) => cb(err, { xml, headers })))(); - const headers = { ...initiateRequest.headers }; + const headers = { ...initiateRequest.headers, ...params.headers }; if (params.location) { headers[constants.objectLocationConstraintHeader] = params.location; } @@ -737,6 +737,13 @@ describe('Multipart Upload API', () => { }); }); + it('should store Content-Encoding without aws-chunked or a leftover separator', async () => { + await _bucketPut(authInfo, bucketPutRequest, log); + await _uploadMpuObject({ headers: { 'content-encoding': 'gzip,aws-chunked' } }); + const md = metadata.keyMaps.get(bucketName).get(objectKey); + assert.strictEqual(md['content-encoding'], 'gzip'); + }); + it('should return an error if a complete multipart upload' + ' request contains malformed xml', done => { async.waterfall([ diff --git a/tests/unit/api/objectCopy.js b/tests/unit/api/objectCopy.js index 683adc1a5f..891d18586a 100644 --- a/tests/unit/api/objectCopy.js +++ b/tests/unit/api/objectCopy.js @@ -503,6 +503,59 @@ describe('objectCopy overheadField', () => { }); }); +describe('objectCopy Content-Encoding', () => { + before(done => { + cleanup(); + // a value with a leftover separator, as stored before aws-chunked + // removal was fixed + const testPutObjectRequest = versioningTestUtils.createPutObjectRequest( + sourceBucketName, objectKey, objData[0]); + testPutObjectRequest.headers['content-encoding'] = 'gzip,'; + async.series( + [ + callback => bucketPut(authInfo, putDestBucketRequest, log, callback), + callback => bucketPut(authInfo, putSourceBucketRequest, log, callback), + callback => objectPut(authInfo, testPutObjectRequest, undefined, log, callback), + ], + done, + ); + }); + + after(() => { + cleanup(); + }); + + [ + { + desc: 'remove the leftover separator of the source with the COPY directive', + headers: {}, + expected: 'gzip', + }, + { + desc: 'remove aws-chunked from the request value with the REPLACE directive', + headers: { 'x-amz-metadata-directive': 'REPLACE', 'content-encoding': 'br,aws-chunked' }, + expected: 'br', + }, + { + desc: 'remove a leftover separator from the request value with the REPLACE directive', + headers: { 'x-amz-metadata-directive': 'REPLACE', 'content-encoding': 'br,' }, + expected: 'br', + }, + ].forEach(({ desc, headers, expected }) => { + it(`should ${desc}`, done => { + const testObjectCopyRequest = _createObjectCopyRequest(destBucketName, headers); + objectCopy(authInfo, testObjectCopyRequest, sourceBucketName, objectKey, undefined, log, err => { + assert.ifError(err); + metadata.getObjectMD(destBucketName, objectKey, {}, log, (err, md) => { + assert.ifError(err); + assert.strictEqual(md['content-encoding'], expected); + done(); + }); + }); + }); + }); +}); + describe('objectCopy in ingestion bucket', () => { const dataClient = data.client; const prevDataImplName = data.implName; diff --git a/tests/unit/api/objectPut.js b/tests/unit/api/objectPut.js index 3f4c458d60..2150d69cd0 100644 --- a/tests/unit/api/objectPut.js +++ b/tests/unit/api/objectPut.js @@ -306,6 +306,34 @@ describe('objectPut API', () => { }); }); + [ + { sent: 'gzip,aws-chunked', stored: 'gzip' }, + { sent: 'aws-chunked,gzip', stored: 'gzip' }, + { sent: 'aws-chunked', stored: '' }, + ].forEach(({ sent, stored }) => { + it(`should store Content-Encoding ${JSON.stringify(sent)} as ${JSON.stringify(stored)}`, done => { + const testPutObjectRequest = new DummyRequest({ + bucketName, + namespace, + objectKey: objectName, + headers: { 'content-encoding': sent }, + url: `/${bucketName}/${objectName}`, + calculatedHash: 'vnR+tLdVF79rPPfF+7YvOg==', + }, postBody); + + bucketPut(authInfo, testPutBucketRequest, log, () => { + objectPut(authInfo, testPutObjectRequest, undefined, log, err => { + assert.ifError(err); + metadata.getObjectMD(bucketName, objectName, {}, log, (err, md) => { + assert.ifError(err); + assert.strictEqual(md['content-encoding'], stored); + done(); + }); + }); + }); + }); + }); + it('should successfully put an object with legal hold ON', done => { const request = new DummyRequest({ bucketName, diff --git a/tests/unit/utils/collectResponseHeaders.js b/tests/unit/utils/collectResponseHeaders.js index 75cd3c134f..826d579045 100644 --- a/tests/unit/utils/collectResponseHeaders.js +++ b/tests/unit/utils/collectResponseHeaders.js @@ -73,4 +73,21 @@ describe('Middleware: Collect Response Headers', () => { assert.strictEqual(headers['x-amz-scal-transition-in-progress'], undefined); assert.strictEqual(headers['x-amz-meta-scal-s3-transition-in-progress'], true); }); + + [ + { stored: 'gzip', expected: 'gzip' }, + { stored: 'gzip, br', expected: 'gzip, br' }, + { stored: 'gzip,', expected: 'gzip' }, + { stored: 'base64, ', expected: 'base64' }, + { stored: ' gzip', expected: 'gzip' }, + { stored: ',', expected: undefined }, + { stored: '', expected: undefined }, + { stored: undefined, expected: undefined }, + ].forEach(({ stored, expected }) => { + it(`should return Content-Encoding ${JSON.stringify(expected)} ` + + `when ${JSON.stringify(stored)} is stored`, () => { + const headers = collectResponseHeaders({ 'content-encoding': stored }); + assert.strictEqual(headers['Content-Encoding'], expected); + }); + }); }); diff --git a/tests/unit/utils/removeFromCommaSeparatedList.js b/tests/unit/utils/removeFromCommaSeparatedList.js new file mode 100644 index 0000000000..72bbb33718 --- /dev/null +++ b/tests/unit/utils/removeFromCommaSeparatedList.js @@ -0,0 +1,63 @@ +const assert = require('assert'); + +const removeFromCommaSeparatedList = require('../../../lib/utilities/removeFromCommaSeparatedList'); +const { awsChunkedContentEncoding } = require('../../../constants'); + +describe('removeFromCommaSeparatedList', () => { + describe('with aws-chunked', () => { + [ + { input: undefined, expected: undefined }, + { input: null, expected: null }, + { input: '', expected: '' }, + { input: 'gzip', expected: 'gzip' }, + { input: 'gzip, br', expected: 'gzip, br' }, + { input: 'gzip,', expected: 'gzip,' }, + { input: 'AWS-Chunked,gzip', expected: 'AWS-Chunked,gzip' }, + { input: 'aws-chunked', expected: '' }, + { input: 'aws-chunked,gzip', expected: 'gzip' }, + { input: 'gzip,aws-chunked', expected: 'gzip' }, + { input: 'gzip,aws-chunked,br', expected: 'gzip,br' }, + { input: 'gzip, aws-chunked', expected: 'gzip' }, + { input: 'aws-chunked, gzip', expected: 'gzip' }, + { input: 'gzip, aws-chunked, br', expected: 'gzip, br' }, + { input: 'aws-chunked,gzip,aws-chunked', expected: 'gzip' }, + ].forEach(({ input, expected }) => { + it(`should turn ${JSON.stringify(input)} into ${JSON.stringify(expected)}`, () => { + assert.strictEqual(removeFromCommaSeparatedList(input, awsChunkedContentEncoding), expected); + }); + }); + }); + + describe('with empty elements', () => { + [ + { input: undefined, expected: undefined }, + { input: null, expected: null }, + { input: '', expected: '' }, + { input: 'gzip', expected: 'gzip' }, + { input: 'gzip, br', expected: 'gzip, br' }, + { input: 'gzip,', expected: 'gzip' }, + { input: 'gzip, ', expected: 'gzip' }, + { input: ' gzip', expected: 'gzip' }, + { input: ',gzip', expected: 'gzip' }, + { input: 'gzip,,br', expected: 'gzip,br' }, + { input: ',', expected: '' }, + ].forEach(({ input, expected }) => { + it(`should turn ${JSON.stringify(input)} into ${JSON.stringify(expected)}`, () => { + assert.strictEqual(removeFromCommaSeparatedList(input, ''), expected); + }); + }); + }); + + describe('with aws-chunked and empty elements', () => { + [ + { input: 'gzip,', expected: 'gzip' }, + { input: 'gzip,aws-chunked,', expected: 'gzip' }, + { input: 'gzip,,aws-chunked', expected: 'gzip' }, + { input: 'gzip, br', expected: 'gzip, br' }, + ].forEach(({ input, expected }) => { + it(`should turn ${JSON.stringify(input)} into ${JSON.stringify(expected)}`, () => { + assert.strictEqual(removeFromCommaSeparatedList(input, awsChunkedContentEncoding, ''), expected); + }); + }); + }); +});