Skip to content

Commit 04307d7

Browse files
authored
Merge pull request #8901 from jackyalbo/jacky-cors-fixes
Fixing issue with allowed methods
2 parents b737f5e + cdce0b2 commit 04307d7

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

src/endpoint/s3/ops/s3_put_bucket_cors.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,30 @@
22
'use strict';
33

44
const _ = require('lodash');
5+
const { S3Error } = require('../s3_errors');
56

67
/**
78
* http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTcors.html
89
*/
910
async function put_bucket_cors(req) {
10-
const cors_rules = req.body.CORSConfiguration.CORSRule.map(rule =>
11-
_.omitBy({
11+
const allowedList = ['GET', 'PUT', 'POST', 'DELETE', 'HEAD'];
12+
const cors_rules = req.body.CORSConfiguration.CORSRule.map(rule => {
13+
const unsupported_method = rule.AllowedMethod.find(item => !allowedList.includes(item));
14+
if (unsupported_method) {
15+
throw new S3Error({
16+
...S3Error.InvalidRequest,
17+
message: `Found unsupported HTTP method in CORS config. Unsupported method is ${unsupported_method}`
18+
});
19+
}
20+
return _.omitBy({
1221
allowed_headers: rule.AllowedHeader,
1322
allowed_methods: rule.AllowedMethod,
1423
allowed_origins: rule.AllowedOrigin,
1524
expose_headers: rule.ExposeHeader,
1625
id: rule.ID?.[0],
1726
max_age_seconds: rule.MaxAgeSeconds && parseInt(rule.MaxAgeSeconds, 10),
18-
}, _.isUndefined)
19-
);
27+
}, _.isUndefined);
28+
});
2029
await req.object_sdk.put_bucket_cors({
2130
name: req.params.bucket,
2231
cors_rules

src/test/unit_tests/test_s3_ops.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
const _ = require('lodash');
77
// setup coretest first to prepare the env
88
const coretest = require('./coretest');
9-
coretest.setup({ pools_to_create: coretest.POOL_LIST });
9+
coretest.setup({ pools_to_create: [coretest.POOL_LIST[1]] });
1010
const config = require('../../../config');
1111
const { S3 } = require('@aws-sdk/client-s3');
1212
const { NodeHttpHandler } = require("@smithy/node-http-handler");
@@ -509,17 +509,41 @@ mocha.describe('s3_ops', function() {
509509
assert.deepEqual(res.CORSRules, params.CORSConfiguration.CORSRules);
510510
});
511511

512+
mocha.it('should fail on unsupported AllowedMethods', async function() {
513+
const unsupported_method = "JACKY";
514+
const params = {
515+
Bucket: "cors-bucket",
516+
CORSConfiguration: {
517+
CORSRules: [{
518+
AllowedOrigins: ["http://www.example.com"],
519+
AllowedMethods: ["PUT", "POST", unsupported_method, "DELETE"],
520+
MaxAgeSeconds: 1500,
521+
}]
522+
}
523+
};
524+
try {
525+
await s3.putBucketCors(params);
526+
assert.fail(`should reject put bucket cors with unsupported method ${unsupported_method}`);
527+
} catch (err) {
528+
assert.strictEqual(err.Code, 'InvalidRequest',
529+
`Found unsupported HTTP method in CORS config. Unsupported method is ${unsupported_method}`
530+
);
531+
assert.strictEqual(err.$metadata.httpStatusCode, 400);
532+
}
533+
});
534+
512535
mocha.after(async function() {
513536
await s3.deleteBucket({ Bucket: "cors-bucket" });
514537
});
515538
});
516539

517-
async function test_object_ops(bucket_name, bucket_type, caching, remote_endpoint_options) {
540+
async function test_object_ops(bucket_name, bucket_type, caching, remote_endpoint_options, skip) {
518541

519542
const is_azure_namespace = is_namespace_blob_bucket(bucket_type, remote_endpoint_options && remote_endpoint_options.endpoint_type);
520543
const is_azure_mock = is_namespace_blob_mock(bucket_type, remote_endpoint_options && remote_endpoint_options.endpoint_type);
521544

522545
mocha.before(async function() {
546+
if (skip) this.skip();
523547
this.timeout(100000);
524548
source_bucket = bucket_name + '-source';
525549
other_platform_bucket = bucket_name + '-other-platform';
@@ -1389,6 +1413,7 @@ mocha.describe('s3_ops', function() {
13891413
});
13901414

13911415
mocha.after(async function() {
1416+
if (skip) return;
13921417
this.timeout(100000);
13931418
if (bucket_type === "regular") {
13941419
await s3.deleteBucket({ Bucket: source_bucket });
@@ -1429,24 +1454,25 @@ mocha.describe('s3_ops', function() {
14291454
});
14301455

14311456
mocha.describe('azure-namespace-bucket-object-ops', function() {
1457+
const skip = !process.env.BLOB_HOST && (!process.env.NEWAZUREPROJKEY || !process.env.NEWAZUREPROJSECRET);
14321458
const options = {
14331459
endpoint: process.env.NEWAZUREPROJKEY ? 'https://blob.core.windows.net' : azure_mock_endpoint,
14341460
endpoint_type: 'AZURE',
14351461
identity: process.env.NEWAZUREPROJKEY || azure_mock_account,
14361462
secret: process.env.NEWAZUREPROJSECRET || azure_mock_key
14371463
};
1438-
test_object_ops(BKT6, 'namespace', undefined, options);
1464+
test_object_ops(BKT6, 'namespace', undefined, options, skip);
14391465
});
14401466

14411467
mocha.describe('aws-namespace-bucket-object-ops', function() {
1442-
if (!process.env.NEWAWSPROJKEY || !process.env.NEWAWSPROJSECRET) return;
1468+
const skip = !process.env.NEWAWSPROJKEY || !process.env.NEWAWSPROJSECRET;
14431469
const options = {
14441470
endpoint: 'https://s3.amazonaws.com',
14451471
endpoint_type: 'AWS',
14461472
identity: process.env.NEWAWSPROJKEY,
14471473
secret: process.env.NEWAWSPROJSECRET
14481474
};
1449-
test_object_ops(BKT7, 'namespace', undefined, options);
1475+
test_object_ops(BKT7, 'namespace', undefined, options, skip);
14501476
});
14511477
});
14521478

0 commit comments

Comments
 (0)