Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MCG] Using put-bucket-policy with wrong syntax under Resource results in InternalError instead of MalformedPolicy #8783

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/endpoint/s3/s3_bucket_policy_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const OP_NAME_TO_ACTION = Object.freeze({

const qm_regex = /\?/g;
const ar_regex = /\*/g;
const esc_regex = /[-/^$+?.()|[\]{}]/g;

const predicate_map = {
'StringEquals': (request_value, policy_value) => request_value === policy_value,
Expand Down Expand Up @@ -277,8 +278,14 @@ async function validate_s3_policy(policy, bucket_name, get_account_handler) {
throw new RpcError('MALFORMED_POLICY', 'Invalid principal in policy', { detail: statement.Principal });
}
for (const resource of _.flatten([statement.Resource || statement.NotResource])) {
console.log(`************* VINAYAK RESOURCE = ${resource}`);
const resource_bucket_part = resource.split('/')[0];
const resource_regex = RegExp(`^${resource_bucket_part.replace(qm_regex, '.?').replace(ar_regex, '.*')}$`);
const resource_regex = RegExp(
`^${resource_bucket_part
.replace(esc_regex, '\\$&')
.replace(qm_regex, '.?')
.replace(ar_regex, '.*')}$`
);
if (!resource_regex.test('arn:aws:s3:::' + bucket_name)) {
throw new RpcError('MALFORMED_POLICY', 'Policy has invalid resource', { detail: resource });
}
Expand Down
21 changes: 21 additions & 0 deletions src/test/unit_tests/test_bucketspace_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,27 @@ mocha.describe('bucketspace_fs', function() {
}
});

mocha.it('put_bucket_policy, Wrong Resouce list syntax', async function() {
const policy = {
Version: '2012-10-17',
Statement: [{
Sid: 'id-22',
Effect: 'Allow',
Principal: { AWS: 'user10' },
Action: ['s3:*'],
Resource: "['arn:aws:s3:::*']"
}]
};
const param = { name: test_bucket, policy: policy };
try {
await bucketspace_fs.put_bucket_policy(param);
assert.fail('should have failed with invalid principal in policy');
} catch (err) {
assert.equal(err.rpc_code, 'MALFORMED_POLICY');
assert.equal(err.message, 'Invalid principal in policy');
}
});

mocha.it('put_bucket_policy other account array', async function() {
const policy = {
Version: '2012-10-17',
Expand Down