Skip to content

Commit a33d97d

Browse files
authored
fix: add bucket name length trigger (#685)
* feat: add bucket name length constraint * fix: remove DBError for postgres check constraint * refactor: change bucket name max length to 100 * refactor: add bucket name length trigger * fix: update bucket name length test to reflect max length of 100 characters * revert: db migration types
1 parent c0ba02a commit a33d97d

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
create or replace function storage.enforce_bucket_name_length()
2+
returns trigger as $$
3+
begin
4+
if length(new.name) > 100 then
5+
raise exception 'bucket name "%" is too long (% characters). Max is 100.', new.name, length(new.name);
6+
end if;
7+
return new;
8+
end;
9+
$$ language plpgsql;
10+
11+
12+
drop trigger if exists enforce_bucket_name_length_trigger on storage.buckets;
13+
create trigger enforce_bucket_name_length_trigger
14+
before insert or update of name on storage.buckets
15+
for each row execute function storage.enforce_bucket_name_length();

src/internal/database/migrations/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ export const DBMigration = {
3535
'optimize-search-function-v1': 34,
3636
'add-insert-trigger-prefixes': 35,
3737
'optimise-existing-functions': 36,
38+
'add-bucket-name-length-trigger': 37,
3839
}

src/storage/limits.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ export function isValidKey(key: string): boolean {
5858
*/
5959
export function isValidBucketName(bucketName: string): boolean {
6060
// only allow s3 safe characters and characters which require special handling for now
61+
// the slash restriction come from bucket naming rules
62+
// and the rest of the validation rules are based on S3 object key validation.
6163
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
62-
// excluding / for bucketName
64+
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html
6365
return (
64-
bucketName.length > 0 && /^(\w|!|-|\.|\*|'|\(|\)| |&|\$|@|=|;|:|\+|,|\?)*$/.test(bucketName)
66+
bucketName.length > 0 &&
67+
bucketName.length < 101 &&
68+
/^(\w|!|-|\.|\*|'|\(|\)| |&|\$|@|=|;|:|\+|,|\?)*$/.test(bucketName)
6569
)
6670
}
6771

src/test/bucket.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,21 @@ describe('testing POST bucket', () => {
219219
})
220220
expect(response.statusCode).toBe(400)
221221
})
222+
223+
test('user is not able to create a bucket with a name longer than 100 characters', async () => {
224+
const longBucketName = 'a'.repeat(101)
225+
const response = await app().inject({
226+
method: 'POST',
227+
url: `/bucket`,
228+
headers: {
229+
authorization: `Bearer ${process.env.AUTHENTICATED_KEY}`,
230+
},
231+
payload: {
232+
name: longBucketName,
233+
},
234+
})
235+
expect(response.statusCode).toBe(400)
236+
})
222237
})
223238

224239
/*

0 commit comments

Comments
 (0)