Skip to content

Commit 3c757e7

Browse files
committed
add default limit of 200,000 objects max for empty bucket
1 parent 1d91784 commit 3c757e7

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ type StorageConfigType = {
9797
responseSMaxAge: number
9898
anonKeyAsync: Promise<string>
9999
serviceKeyAsync: Promise<string>
100+
emptyBucketMax: number
100101
storageBackendType: StorageBackendType
101102
tenantId: string
102103
requestUrlLengthLimit: number
@@ -311,6 +312,10 @@ export function getConfig(options?: { reload?: boolean }): StorageConfigType {
311312
),
312313
// Storage
313314
storageBackendType: getOptionalConfigFromEnv('STORAGE_BACKEND') as StorageBackendType,
315+
emptyBucketMax: parseInt(
316+
getOptionalConfigFromEnv('STORAGE_EMPTY_BUCKET_MAX') || '200000',
317+
10
318+
),
314319

315320
// Storage - File
316321
storageFilePath: getOptionalConfigFromEnv(

src/internal/errors/codes.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ export const ERRORS = {
4949
message: `The bucket you tried to delete is not empty`,
5050
originalError: e,
5151
}),
52+
UnableToEmptyBucket: (bucket: string, e?: Error) =>
53+
new StorageBackendError({
54+
code: ErrorCode.InvalidRequest,
55+
resource: bucket,
56+
httpStatusCode: 409,
57+
message: `Unable to empty the bucket because it contains too many objects`,
58+
originalError: e,
59+
}),
5260
NoSuchBucket: (bucket: string, e?: Error) =>
5361
new StorageBackendError({
5462
code: ErrorCode.NoSuchBucket,

src/storage/storage.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ObjectStorage } from './object'
88
import { InfoRenderer } from '@storage/renderer/info'
99
import { ObjectAdminDeleteBatch } from './events'
1010

11-
const { requestUrlLengthLimit } = getConfig()
11+
const { requestUrlLengthLimit, emptyBucketMax } = getConfig()
1212

1313
/**
1414
* Storage
@@ -182,6 +182,11 @@ export class Storage {
182182
async emptyBucket(bucketId: string) {
183183
await this.findBucket(bucketId, 'name')
184184

185+
const count = await this.countObjects(bucketId)
186+
if (count > emptyBucketMax) {
187+
throw ERRORS.UnableToEmptyBucket(bucketId)
188+
}
189+
185190
while (true) {
186191
const objects = await this.db.listObjects(
187192
bucketId,

0 commit comments

Comments
 (0)