Skip to content

Commit e9808eb

Browse files
authored
Merge pull request #8573 from jackyalbo/jacky_cors
Turning CORS back on for master
2 parents 7f676da + 20a3944 commit e9808eb

File tree

4 files changed

+25
-47
lines changed

4 files changed

+25
-47
lines changed

src/endpoint/s3/s3_rest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ async function handle_request(req, res) {
107107
}
108108

109109
const op_name = parse_op_name(req);
110-
// const cors = req.params.bucket && await req.object_sdk.read_bucket_sdk_cors_info(req.params.bucket);
110+
const cors = req.params.bucket && await req.object_sdk.read_bucket_sdk_cors_info(req.params.bucket);
111111

112-
http_utils.set_cors_headers_s3(req, res, /* cors */ undefined);
112+
http_utils.set_cors_headers_s3(req, res, cors);
113113

114114
if (req.method === 'OPTIONS') {
115115
dbg.log1('OPTIONS!');

src/test/system_tests/ceph_s3_tests/s3-tests-lists/nsfs_s3_tests_black_list.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,6 @@ s3tests_boto3/functional/test_s3.py::test_multipart_upload_size_too_small
207207
s3tests_boto3/functional/test_s3.py::test_abort_multipart_upload
208208
s3tests_boto3/functional/test_s3.py::test_multipart_copy_improper_range
209209
s3tests_boto3/functional/test_s3.py::test_100_continue
210-
s3tests_boto3/functional/test_s3.py::test_set_cors
211-
s3tests_boto3/functional/test_s3.py::test_cors_origin_wildcard
212-
s3tests_boto3/functional/test_s3.py::test_cors_origin_response
213-
s3tests_boto3/functional/test_s3.py::test_cors_header_option
214210
s3tests_boto3/functional/test_s3.py::test_set_tagging
215211
s3tests_boto3/functional/test_s3.py::test_multipart_resend_first_finishes_last
216212
s3tests_boto3/functional/test_s3.py::test_versioned_object_acl
@@ -400,8 +396,6 @@ s3tests_boto3/functional/test_s3.py::test_sse_s3_encrypted_upload_1b
400396
s3tests_boto3/functional/test_s3.py::test_sse_s3_encrypted_upload_1kb
401397
s3tests_boto3/functional/test_s3.py::test_sse_s3_encrypted_upload_1mb
402398
s3tests_boto3/functional/test_s3.py::test_sse_s3_encrypted_upload_8mb
403-
s3tests_boto3/functional/test_s3.py::test_cors_presigned_get_object
404-
s3tests_boto3/functional/test_s3.py::test_cors_presigned_put_object
405399
s3tests_boto3/functional/test_s3.py::test_cors_presigned_get_object_tenant
406400
s3tests_boto3/functional/test_s3.py::test_cors_presigned_put_object_with_acl
407401
s3tests_boto3/functional/test_s3.py::test_cors_presigned_put_object_tenant

src/test/system_tests/ceph_s3_tests/s3-tests-lists/s3_tests_black_list.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,6 @@ s3tests_boto3/functional/test_s3.py::test_multipart_upload_size_too_small
209209
s3tests_boto3/functional/test_s3.py::test_abort_multipart_upload
210210
s3tests_boto3/functional/test_s3.py::test_multipart_copy_improper_range
211211
s3tests_boto3/functional/test_s3.py::test_100_continue
212-
s3tests_boto3/functional/test_s3.py::test_set_cors
213-
s3tests_boto3/functional/test_s3.py::test_cors_origin_wildcard
214-
s3tests_boto3/functional/test_s3.py::test_cors_origin_response
215-
s3tests_boto3/functional/test_s3.py::test_cors_header_option
216212
s3tests_boto3/functional/test_s3.py::test_set_tagging
217213
s3tests_boto3/functional/test_s3.py::test_multipart_resend_first_finishes_last
218214
s3tests_boto3/functional/test_s3.py::test_versioned_object_acl
@@ -354,7 +350,5 @@ s3tests_boto3/functional/test_sts.py::test_assume_role_with_web_identity_wrong_r
354350
s3tests_boto3/functional/test_sts.py::test_assume_role_with_web_identity_resource_tag_princ_tag
355351
s3tests_boto3/functional/test_sts.py::test_assume_role_with_web_identity_resource_tag_copy_obj
356352
s3tests_boto3/functional/test_sts.py::test_assume_role_with_web_identity_role_resource_tag
357-
s3tests_boto3/functional/test_s3.py::test_cors_presigned_get_object
358-
s3tests_boto3/functional/test_s3.py::test_cors_presigned_put_object
359353
s3tests_boto3/functional/test_s3.py::test_cors_presigned_put_object_with_acl
360354
s3tests_boto3/functional/test_s3.py::test_cors_presigned_put_object_tenant_with_acl

src/util/http_utils.js

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -655,42 +655,32 @@ function set_cors_headers(req, res, cors) {
655655
* @param {CORSRule[]} cors_rules
656656
*/
657657
function set_cors_headers_s3(req, res, cors_rules) {
658-
if (config.S3_CORS_ENABLED) {
658+
if (!config.S3_CORS_ENABLED || !cors_rules) return;
659+
660+
// based on https://docs.aws.amazon.com/AmazonS3/latest/userguide/cors.html
661+
const match_method = req.headers['access-control-request-method'] || req.method;
662+
const match_origin = req.headers.origin;
663+
const match_header = req.headers['access-control-request-headers']; // not a must
664+
const matched_rule = req.headers.origin && ( // find the first rule with origin and method match
665+
cors_rules.find(rule => {
666+
const allowed_origins_regex = rule.allowed_origins.map(r => RegExp(`^${r.replace(/\*/g, '.*')}$`));
667+
const allowed_headers_regex = rule.allowed_headers?.map(r => RegExp(`^${r.replace(/\*/g, '.*')}$`));
668+
return allowed_origins_regex.some(r => r.test(match_origin)) &&
669+
rule.allowed_methods.includes(match_method) &&
670+
// we can match if no request headers or if reuqest headers match the rule allowed headers
671+
(!match_header || allowed_headers_regex?.some(r => r.test(match_header)));
672+
}));
673+
if (matched_rule) {
674+
// https://docs.aws.amazon.com/AmazonS3/latest/API/RESTCommonResponseHeaders.html
659675
set_cors_headers(req, res, {
660-
allow_origin: config.S3_CORS_ALLOW_ORIGIN,
661-
allow_credentials: config.S3_CORS_ALLOW_CREDENTIAL,
662-
allow_methods: config.S3_CORS_ALLOW_METHODS,
663-
allow_headers: config.S3_CORS_ALLOW_HEADERS,
664-
expose_headers: config.STS_CORS_EXPOSE_HEADERS,
676+
allow_origin: matched_rule.allowed_origins.includes('*') ? '*' : req.headers.origin,
677+
allow_methods: matched_rule.allowed_methods.join(','),
678+
allow_headers: matched_rule.allowed_headers?.join(','),
679+
expose_headers: matched_rule.expose_headers?.join(','),
680+
allow_credentials: 'true',
681+
max_age: matched_rule?.max_age
665682
});
666683
}
667-
// CORS CURRENTLY BREAKS OBJECT BROWSER - WILL ONLY SUPPORT DEFAULT HEADERS FOR NOW
668-
// if (!config.S3_CORS_ENABLED || !cors_rules) return;
669-
670-
// // based on https://docs.aws.amazon.com/AmazonS3/latest/userguide/cors.html
671-
// const match_method = req.headers['access-control-request-method'] || req.method;
672-
// const match_origin = req.headers.origin;
673-
// const match_header = req.headers['access-control-request-headers']; // not a must
674-
// const matched_rule = req.headers.origin && ( // find the first rule with origin and method match
675-
// cors_rules.find(rule => {
676-
// const allowed_origins_regex = rule.allowed_origins.map(r => RegExp(`^${r.replace(/\*/g, '.*')}$`));
677-
// const allowed_headers_regex = rule.allowed_headers?.map(r => RegExp(`^${r.replace(/\*/g, '.*')}$`));
678-
// return allowed_origins_regex.some(r => r.test(match_origin)) &&
679-
// rule.allowed_methods.includes(match_method) &&
680-
// // we can match if no request headers or if reuqest headers match the rule allowed headers
681-
// (!match_header || allowed_headers_regex?.some(r => r.test(match_header)));
682-
// }));
683-
// if (matched_rule) {
684-
// // https://docs.aws.amazon.com/AmazonS3/latest/API/RESTCommonResponseHeaders.html
685-
// set_cors_headers(req, res, {
686-
// allow_origin: matched_rule.allowed_origins.includes('*') ? '*' : req.headers.origin,
687-
// allow_methods: matched_rule.allowed_methods.join(','),
688-
// allow_headers: matched_rule.allowed_headers?.join(','),
689-
// expose_headers: matched_rule.expose_headers?.join(','),
690-
// allow_credentials: 'true',
691-
// max_age: matched_rule?.max_age
692-
// });
693-
// }
694684
}
695685

696686
/**

0 commit comments

Comments
 (0)