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');
1313const http_utils = require ( '../../util/http_utils' ) ;
1414const signature_utils = require ( '../../util/signature_utils' ) ;
1515const config = require ( '../../../config' ) ;
16+ const s3_utils = require ( './s3_utils' ) ;
1617
1718const 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
Original file line number Diff line number Diff 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+
727745exports . STORAGE_CLASS_STANDARD = STORAGE_CLASS_STANDARD ;
728746exports . STORAGE_CLASS_GLACIER = STORAGE_CLASS_GLACIER ;
729747exports . STORAGE_CLASS_GLACIER_IR = STORAGE_CLASS_GLACIER_IR ;
@@ -763,3 +781,4 @@ exports.parse_version_id = parse_version_id;
763781exports . get_object_owner = get_object_owner ;
764782exports . get_default_object_owner = get_default_object_owner ;
765783exports . 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