Beautiful Open-Source Form Builder
Fix public upload rate limit buckets (#1110)
Julien Nahumf17ccd276368
diff --git a/api/.env.docker b/api/.env.dockerindex c4eb2b19c..3bfe8ffce 100644--- a/api/.env.docker+++ b/api/.env.docker@@ -32,6 +32,8 @@ AWS_BUCKET=JWT_TTL=1440JWT_SECRET=JWT_SKIP_IP_UA_VALIDATION=true+PUBLIC_UPLOADS_RATE_LIMIT_PER_MINUTE=30+PUBLIC_UPLOADS_RATE_LIMIT_PER_HOUR=300OPEN_AI_API_KEY=@@ -44,4 +46,4 @@ RE_CAPTCHA_SECRET_KEY=TELEGRAM_BOT_ID=TELEGRAM_BOT_TOKEN=-SHOW_OFFICIAL_TEMPLATES=true\ No newline at end of file+SHOW_OFFICIAL_TEMPLATES=truediff --git a/api/.env.example b/api/.env.exampleindex 1e0c95d04..faca2f301 100644--- a/api/.env.example+++ b/api/.env.example@@ -55,6 +55,8 @@ MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"JWT_TTL=10080JWT_REMEMBER_TTL=43200JWT_SECRET=+PUBLIC_UPLOADS_RATE_LIMIT_PER_MINUTE=30+PUBLIC_UPLOADS_RATE_LIMIT_PER_HOUR=300STRIPE_KEY=STRIPE_SECRET=diff --git a/api/app/Providers/RouteServiceProvider.php b/api/app/Providers/RouteServiceProvider.phpindex 85d4609ae..4eae07606 100644--- a/api/app/Providers/RouteServiceProvider.php+++ b/api/app/Providers/RouteServiceProvider.php@@ -50,6 +50,21 @@ class RouteServiceProvider extends ServiceProviderRateLimiter::for('api', function (Request $request) {return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());});++ RateLimiter::for('public-uploads', function (Request $request) {+ $identifier = $request->user()+ ? 'user:' . $request->user()->getAuthIdentifier()+ : 'ip:' . $request->ip();+ $route = $request->route()?->getName() ?? $request->path();+ $key = $route . ':' . $identifier;++ return [+ Limit::perMinute(max(1, config('opnform.public_uploads.rate_limit.per_minute', 30)))+ ->by('public-uploads:minute:' . $key),+ Limit::perHour(max(1, config('opnform.public_uploads.rate_limit.per_hour', 300)))+ ->by('public-uploads:hour:' . $key),+ ];+ });}protected function registerGlobalRouteParamConstraints()diff --git a/api/config/opnform.php b/api/config/opnform.phpindex 11f0e639a..fd2a61b6c 100644--- a/api/config/opnform.php+++ b/api/config/opnform.php@@ -10,6 +10,12 @@ return ['custom_code' => ['enable_self_hosted' => env('CUSTOM_CODE_ENABLE_SELF_HOSTED', false),],+ 'public_uploads' => [+ 'rate_limit' => [+ 'per_minute' => (int) env('PUBLIC_UPLOADS_RATE_LIMIT_PER_MINUTE', 30),+ 'per_hour' => (int) env('PUBLIC_UPLOADS_RATE_LIMIT_PER_HOUR', 300),+ ],+ ],'webhooks' => ['allow_private_urls' => env('WEBHOOKS_ALLOW_PRIVATE_URLS', false),],diff --git a/api/routes/api.php b/api/routes/api.phpindex 570a8d18d..3c0a66e5d 100644--- a/api/routes/api.php+++ b/api/routes/api.php@@ -204,7 +204,7 @@ Route::group(['middleware' => 'auth.multi'], function () {Route::post('/assets/upload',[FormController::class, 'uploadAsset']- )->middleware(['throttle:10,1', 'throttle:30,60'])->withoutMiddleware(['auth.multi'])->name('assets.upload');+ )->middleware('throttle:public-uploads')->withoutMiddleware(['auth.multi'])->name('assets.upload');Route::get('/{form}/uploaded-file/{filename}',[FormController::class, 'viewFile']@@ -413,7 +413,7 @@ Route::post(Route::post('/upload-file',[\App\Http\Controllers\Content\FileUploadController::class, 'upload']-)->middleware(['throttle:10,1', 'throttle:30,60'])->name('upload-file');+)->middleware('throttle:public-uploads')->name('upload-file');Route::get('local/temp/{path}', function (Request $request, string $path) {if (!$request->hasValidSignature()) {diff --git a/api/tests/Feature/Forms/UploadFileSecurityTest.php b/api/tests/Feature/Forms/UploadFileSecurityTest.phpindex 0d465a942..6f65c9e1b 100644--- a/api/tests/Feature/Forms/UploadFileSecurityTest.php+++ b/api/tests/Feature/Forms/UploadFileSecurityTest.php@@ -77,30 +77,30 @@ it('sanitizes svg files when moving them to public assets', function () {->toContain('<svg');});-it('throttles the unauthenticated upload endpoints', function () {+it('throttles the unauthenticated upload endpoints without triple-counting a request', function () {Storage::fake();+ config([+ 'opnform.public_uploads.rate_limit.per_minute' => 10,+ 'opnform.public_uploads.rate_limit.per_hour' => 30,+ ]);$this->withMiddleware(ThrottleRequests::class);$this->withServerVariables(['REMOTE_ADDR' => '192.0.2.10']);- $uploadFileThrottled = false;- for ($attempt = 0; $attempt < 15; $attempt++) {+ for ($attempt = 0; $attempt < 10; $attempt++) {$response = $this->post('/upload-file', ['file' => UploadedFile::fake()->createWithContent('note-' . $attempt . '.txt', 'ok'),]);- if ($response->status() === 429) {- $uploadFileThrottled = true;- break;- }-$response->assertCreated();}- expect($uploadFileThrottled)->toBeTrue();++ $this->post('/upload-file', [+ 'file' => UploadedFile::fake()->createWithContent('note-throttled.txt', 'ok'),+ ])->assertStatus(429);$this->withServerVariables(['REMOTE_ADDR' => '192.0.2.11']);- $assetUploadThrottled = false;- for ($attempt = 0; $attempt < 15; $attempt++) {+ for ($attempt = 0; $attempt < 10; $attempt++) {$uuid = (string) Str::uuid();Storage::put('tmp/' . $uuid, 'safe file');@@ -109,12 +109,40 @@ it('throttles the unauthenticated upload endpoints', function () {'type' => 'files',]);- if ($response->status() === 429) {- $assetUploadThrottled = true;- break;- }-$response->assertOk();}- expect($assetUploadThrottled)->toBeTrue();++ $uuid = (string) Str::uuid();+ Storage::put('tmp/' . $uuid, 'safe file');++ $this->postJson('/open/forms/assets/upload', [+ 'url' => 'asset_' . $uuid . '.txt',+ 'type' => 'files',+ ])->assertStatus(429);+});++it('keeps public upload endpoint rate limit buckets separate for the same client', function () {+ Storage::fake();+ config([+ 'opnform.public_uploads.rate_limit.per_minute' => 10,+ 'opnform.public_uploads.rate_limit.per_hour' => 30,+ ]);+ $this->withMiddleware(ThrottleRequests::class);+ $this->withServerVariables(['REMOTE_ADDR' => '192.0.2.12']);++ for ($attempt = 0; $attempt < 10; $attempt++) {+ $response = $this->post('/upload-file', [+ 'file' => UploadedFile::fake()->createWithContent('note-' . $attempt . '.txt', 'ok'),+ ]);++ $response->assertCreated();+ }++ $uuid = (string) Str::uuid();+ Storage::put('tmp/' . $uuid, 'safe file');++ $this->postJson('/open/forms/assets/upload', [+ 'url' => 'asset_' . $uuid . '.txt',+ 'type' => 'files',+ ])->assertOk();});diff --git a/docs/configuration/environment-variables.mdx b/docs/configuration/environment-variables.mdxindex a8d1c7a0f..5a5d88929 100644--- a/docs/configuration/environment-variables.mdx+++ b/docs/configuration/environment-variables.mdx@@ -36,6 +36,8 @@ There are dedicated configuration pages available for more detailed setup instru| `JWT_TTL` | Time to live for JSON Web Tokens (JWT). || `JWT_SECRET` | Secret key used to sign JWTs. || `JWT_SKIP_IP_UA_VALIDATION` | Set to `true` to disable JWT IP and User Agent validation (defaults to `false`). Useful for dynamic IPs. |+| `PUBLIC_UPLOADS_RATE_LIMIT_PER_MINUTE` | Maximum public upload requests allowed per client IP or authenticated user per route each minute. Defaults to `30`. |+| `PUBLIC_UPLOADS_RATE_LIMIT_PER_HOUR` | Maximum public upload requests allowed per client IP or authenticated user per route each hour. Defaults to `300`. || `OIDC_FORCE_LOGIN` | When set to `true`, password-based authentication is disabled and users must authenticate via OIDC SSO. See [Force Login Mode](../configuration/oidc-sso#7-force-login-mode) for details. || `CUSTOM_CODE_ENABLE_SELF_HOSTED` | Enables execution of user-provided custom code on self-hosted deployments. Defaults to `false` for safety reasons (XSS etc.). |