Skip to content

Commit 1983271

Browse files
authored
Merge pull request #308 from bcgov/chore/enforce-object-key-length
Reject PUT /object if combined bucket and file path exceeds S3 maximum
2 parents 46ccb40 + 489644c commit 1983271

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

app/src/components/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ module.exports = Object.freeze({
6060
/** Maximum Content Length supported by S3 CopyObjectCommand */
6161
MAXFILEOBJECTLENGTH: 5 * 1024 * 1024 * 1024 * 1024, // 5 TB
6262

63+
/** Maximum object key length supported by S3 */
64+
MAXOBJECTKEYLENGTH: 1024, // 1024 B
65+
6366
/** Allowable values for the Metadata Directive parameter */
6467
MetadataDirective: {
6568
/** The original metadata is copied to the new version as-is where applicable. */

app/src/controllers/object.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const {
88
MAXCOPYOBJECTLENGTH,
99
MAXFILEOBJECTLENGTH,
1010
MetadataDirective,
11-
TaggingDirective
11+
TaggingDirective,
12+
MAXOBJECTKEYLENGTH
1213
} = require('../components/constants');
1314
const errorToProblem = require('../components/errorToProblem');
1415
const log = require('../components/log')(module.filename);
@@ -292,6 +293,14 @@ const controller = {
292293

293294
let s3Response;
294295
try {
296+
// Short circuit if object key length exceeds maximum allowed by S3
297+
if (Buffer.byteLength(joinPath(bucketKey, req.currentUpload.filename), 'utf-8') > MAXOBJECTKEYLENGTH) {
298+
throw new Problem(422, 'Bucket key or filename too long', req.originalUrl, {
299+
detail: 'Bucket key and object filename combined exceed the maximum length allowed by S3',
300+
bucketId: bucketId
301+
});
302+
}
303+
295304
// Preflight S3 Object check
296305
await storageService.headObject({
297306
filePath: joinPath(bucketKey, req.currentUpload.filename),

0 commit comments

Comments
 (0)