File tree Expand file tree Collapse file tree 2 files changed +22
-2
lines changed Expand file tree Collapse file tree 2 files changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ const time_utils = require('../../util/time_utils');
13
13
const http_utils = require ( '../../util/http_utils' ) ;
14
14
const signature_utils = require ( '../../util/signature_utils' ) ;
15
15
const config = require ( '../../../config' ) ;
16
+ const s3_utils = require ( './s3_utils' ) ;
16
17
17
18
const S3_MAX_BODY_LEN = 4 * 1024 * 1024 ;
18
19
@@ -341,10 +342,10 @@ function get_bucket_and_key(req) {
341
342
}
342
343
}
343
344
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 ) ) {
345
346
throw new S3Error ( S3Error . KeyTooLongError ) ;
346
347
}
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 ) ) {
348
349
throw new S3Error ( S3Error . InvalidBucketName ) ;
349
350
}
350
351
Original file line number Diff line number Diff line change @@ -724,6 +724,24 @@ function parse_restore_request_days(req) {
724
724
return days ;
725
725
}
726
726
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
+
727
745
exports . STORAGE_CLASS_STANDARD = STORAGE_CLASS_STANDARD ;
728
746
exports . STORAGE_CLASS_GLACIER = STORAGE_CLASS_GLACIER ;
729
747
exports . STORAGE_CLASS_GLACIER_IR = STORAGE_CLASS_GLACIER_IR ;
@@ -763,3 +781,4 @@ exports.parse_version_id = parse_version_id;
763
781
exports . get_object_owner = get_object_owner ;
764
782
exports . get_default_object_owner = get_default_object_owner ;
765
783
exports . set_response_supported_storage_classes = set_response_supported_storage_classes ;
784
+ exports . verify_string_byte_length = verify_string_byte_length ;
You can’t perform that action at this time.
0 commit comments