Skip to content

fix: add bucket name length constraint #685

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

Merged
merged 6 commits into from
May 12, 2025
Merged
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
15 changes: 15 additions & 0 deletions migrations/tenant/0037-add-bucket-name-length-trigger.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
create or replace function storage.enforce_bucket_name_length()
returns trigger as $$
begin
if length(new.name) > 100 then
raise exception 'bucket name "%" is too long (% characters). Max is 100.', new.name, length(new.name);
end if;
return new;
end;
$$ language plpgsql;


drop trigger if exists enforce_bucket_name_length_trigger on storage.buckets;
create trigger enforce_bucket_name_length_trigger
before insert or update of name on storage.buckets
for each row execute function storage.enforce_bucket_name_length();
1 change: 1 addition & 0 deletions src/internal/database/migrations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export const DBMigration = {
'optimize-search-function-v1': 34,
'add-insert-trigger-prefixes': 35,
'optimise-existing-functions': 36,
'add-bucket-name-length-trigger': 37,
}
8 changes: 6 additions & 2 deletions src/storage/limits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ export function isValidKey(key: string): boolean {
*/
export function isValidBucketName(bucketName: string): boolean {
// only allow s3 safe characters and characters which require special handling for now
// the slash restriction come from bucket naming rules
// and the rest of the validation rules are based on S3 object key validation.
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
// excluding / for bucketName
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html
return (
bucketName.length > 0 && /^(\w|!|-|\.|\*|'|\(|\)| |&|\$|@|=|;|:|\+|,|\?)*$/.test(bucketName)
bucketName.length > 0 &&
bucketName.length < 101 &&
/^(\w|!|-|\.|\*|'|\(|\)| |&|\$|@|=|;|:|\+|,|\?)*$/.test(bucketName)
)
}

Expand Down
15 changes: 15 additions & 0 deletions src/test/bucket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,21 @@ describe('testing POST bucket', () => {
})
expect(response.statusCode).toBe(400)
})

test('user is not able to create a bucket with a name longer than 100 characters', async () => {
const longBucketName = 'a'.repeat(101)
const response = await app().inject({
method: 'POST',
url: `/bucket`,
headers: {
authorization: `Bearer ${process.env.AUTHENTICATED_KEY}`,
},
payload: {
name: longBucketName,
},
})
expect(response.statusCode).toBe(400)
})
})

/*
Expand Down
Loading