Skip to content

Commit

Permalink
Merge pull request #8629 from achouhan09/dup-id-fix
Browse files Browse the repository at this point in the history
Added a check for duplicate Id in bucket lifecycle rules
  • Loading branch information
achouhan09 authored Jan 8, 2025
2 parents 4d677c8 + 7409b0b commit 02f26cb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/endpoint/s3/ops/s3_put_bucket_lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function parse_lifecycle_field(field, field_parser = parseInt) {
* http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTlifecycle.html
*/
async function put_bucket_lifecycle(req) {
const id_set = new Set();
const lifecycle_rules = _.map(req.body.LifecycleConfiguration.Rule, rule => {
const current_rule = {
filter: {},
Expand All @@ -100,6 +101,13 @@ async function put_bucket_lifecycle(req) {
current_rule.id = uuid();
}

// Check for duplicate ID in the rules
if (id_set.has(current_rule.id)) {
dbg.error('Rule ID must be unique. Found same ID for more than one rule: ', current_rule.id);
throw new S3Error({ ...S3Error.InvalidArgument, message: 'Rule ID must be unique. Found same ID for more than one rule'});
}
id_set.add(current_rule.id);

if (rule.Status?.length !== 1) {
dbg.error('Rule should have status', rule);
throw new S3Error(S3Error.InvalidArgument);
Expand Down
41 changes: 41 additions & 0 deletions src/test/lifecycle/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,36 @@ function id_lifecycle_configuration(Bucket, Key) {
};
}

function duplicate_id_lifecycle_configuration(Bucket, Key) {
const ID1 = 'rule_id';
const ID2 = ID1; // set duplicate ID
return {
Bucket,
LifecycleConfiguration: {
Rules: [{
ID1,
Expiration: {
Days: 17,
},
Filter: {
Prefix: Key,
},
Status: 'Enabled',
},
{
ID2,
Expiration: {
Days: 18,
},
Filter: {
Prefix: Key,
},
Status: 'Enabled',
}, ],
},
};
}

async function put_get_lifecycle_configuration(Bucket, putLifecycleParams, s3) {
const putLifecycleResult = await s3.putBucketLifecycleConfiguration(putLifecycleParams);
console.log('put lifecycle params:', putLifecycleParams, 'result', putLifecycleResult);
Expand Down Expand Up @@ -528,3 +558,14 @@ exports.test_rule_id_length = async function(Bucket, Key, s3) {
assert(error.code === 'InvalidArgument', `Expected InvalidArgument: id length exceeding ${s3_const.MAX_RULE_ID_LENGTH} characters`);
}
};

exports.test_rule_duplicate_id = async function(Bucket, Key, s3) {
const putLifecycleParams = duplicate_id_lifecycle_configuration(Bucket, Key);

try {
await s3.putBucketLifecycleConfiguration(putLifecycleParams);
assert.fail('Expected error for duplicate rule ID, but request was successful');
} catch (error) {
assert(error.code === 'InvalidArgument', 'Expected InvalidArgument: duplicate ID found in the rules');
}
};
1 change: 1 addition & 0 deletions src/test/system_tests/test_lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ async function main() {
await commonTests.test_filter_size(Bucket, s3);
await commonTests.test_and_prefix_size(Bucket, Key, s3);
await commonTests.test_rule_id_length(Bucket, Key, s3);
await commonTests.test_rule_duplicate_id(Bucket, Key, s3);

const getObjectParams = {
Bucket,
Expand Down

0 comments on commit 02f26cb

Please sign in to comment.