Skip to content

Commit c9825c2

Browse files
committed
Fix key and bucket length checks - make it byte count
Signed-off-by: Utkarsh Srivastava <[email protected]>
1 parent b225590 commit c9825c2

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

@@ -384,10 +385,10 @@ function get_bucket_and_key(req) {
384385
}
385386
}
386387

387-
if (key?.length > config.S3_MAX_KEY_LENGTH) {
388+
if (key?.length && !s3_utils.verify_string_byte_length(key, config.S3_MAX_KEY_LENGTH)) {
388389
throw new S3Error(S3Error.KeyTooLongError);
389390
}
390-
if (bucket?.length > config.S3_MAX_BUCKET_NAME_LENGTH) {
391+
if (bucket?.length && !s3_utils.verify_string_byte_length(bucket, config.S3_MAX_BUCKET_NAME_LENGTH)) {
391392
throw new S3Error(S3Error.InvalidBucketName);
392393
}
393394

src/endpoint/s3/s3_utils.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,24 @@ function key_marker_to_cont_tok(key_marker, objects_arr, is_truncated) {
785785
return Buffer.from(j).toString('base64');
786786
}
787787

788+
/**
789+
* Returns true if the byte length of the key
790+
* is within the range [0, max_length]
791+
* @param {string} key
792+
* @param {number} max_length
793+
* @returns
794+
*/
795+
function verify_string_byte_length(key, max_length) {
796+
// Fast path
797+
const MAX_UTF8_WIDTH = 4;
798+
if (key.length * MAX_UTF8_WIDTH <= max_length) {
799+
return true;
800+
}
801+
802+
// Slow path
803+
return Buffer.byteLength(key, 'utf8') <= max_length;
804+
}
805+
788806
exports.STORAGE_CLASS_STANDARD = STORAGE_CLASS_STANDARD;
789807
exports.STORAGE_CLASS_GLACIER = STORAGE_CLASS_GLACIER;
790808
exports.STORAGE_CLASS_GLACIER_IR = STORAGE_CLASS_GLACIER_IR;
@@ -828,5 +846,6 @@ exports.set_response_supported_storage_classes = set_response_supported_storage_
828846
exports.cont_tok_to_key_marker = cont_tok_to_key_marker;
829847
exports.key_marker_to_cont_tok = key_marker_to_cont_tok;
830848
exports.parse_sse_c = parse_sse_c;
849+
exports.verify_string_byte_length = verify_string_byte_length;
831850
exports.OBJECT_ATTRIBUTES = OBJECT_ATTRIBUTES;
832851
exports.OBJECT_ATTRIBUTES_UNSUPPORTED = OBJECT_ATTRIBUTES_UNSUPPORTED;

0 commit comments

Comments
 (0)