Skip to content

Commit 89bbc31

Browse files
committed
Fix key and bucket length checks - make it byte count
Signed-off-by: Utkarsh Srivastava <[email protected]> (cherry picked from commit c9825c2)
1 parent 5ab1361 commit 89bbc31

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/endpoint/s3/s3_rest.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const time_utils = require('../../util/time_utils');
1313
const http_utils = require('../../util/http_utils');
1414
const signature_utils = require('../../util/signature_utils');
1515
const config = require('../../../config');
16+
const s3_utils = require('./s3_utils');
1617

1718
const S3_MAX_BODY_LEN = 4 * 1024 * 1024;
1819

@@ -341,10 +342,10 @@ function get_bucket_and_key(req) {
341342
}
342343
}
343344

344-
if (key?.length > config.S3_MAX_KEY_LENGTH) {
345+
if (key?.length && !s3_utils.verify_string_byte_length(key, config.S3_MAX_KEY_LENGTH)) {
345346
throw new S3Error(S3Error.KeyTooLongError);
346347
}
347-
if (bucket?.length > config.S3_MAX_BUCKET_NAME_LENGTH) {
348+
if (bucket?.length && !s3_utils.verify_string_byte_length(bucket, config.S3_MAX_BUCKET_NAME_LENGTH)) {
348349
throw new S3Error(S3Error.InvalidBucketName);
349350
}
350351

src/endpoint/s3/s3_utils.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,24 @@ function parse_restore_request_days(req) {
724724
return days;
725725
}
726726

727+
/**
728+
* Returns true if the byte length of the key
729+
* is within the range [0, max_length]
730+
* @param {string} key
731+
* @param {number} max_length
732+
* @returns
733+
*/
734+
function verify_string_byte_length(key, max_length) {
735+
// Fast path
736+
const MAX_UTF8_WIDTH = 4;
737+
if (key.length * MAX_UTF8_WIDTH <= max_length) {
738+
return true;
739+
}
740+
741+
// Slow path
742+
return Buffer.byteLength(key, 'utf8') <= max_length;
743+
}
744+
727745
exports.STORAGE_CLASS_STANDARD = STORAGE_CLASS_STANDARD;
728746
exports.STORAGE_CLASS_GLACIER = STORAGE_CLASS_GLACIER;
729747
exports.STORAGE_CLASS_GLACIER_IR = STORAGE_CLASS_GLACIER_IR;
@@ -763,3 +781,4 @@ exports.parse_version_id = parse_version_id;
763781
exports.get_object_owner = get_object_owner;
764782
exports.get_default_object_owner = get_default_object_owner;
765783
exports.set_response_supported_storage_classes = set_response_supported_storage_classes;
784+
exports.verify_string_byte_length = verify_string_byte_length;

0 commit comments

Comments
 (0)