Beautiful Open-Source Form Builder
Prevent attachment thumbnails from exhausting the API rate limit (#1320)
Julien Nahum0bf8fe2991b7
diff --git a/api/app/Providers/RouteServiceProvider.php b/api/app/Providers/RouteServiceProvider.phpindex a550f0ab3..4dd8638fa 100644--- a/api/app/Providers/RouteServiceProvider.php+++ b/api/app/Providers/RouteServiceProvider.php@@ -93,6 +93,20 @@ class RouteServiceProvider extends ServiceProviderreturn Limit::perMinute(180)->by($request->user()?->id ?: $request->ip());});+ // A submissions page may show hundreds of attachment thumbnails. Keep+ // signed downloads out of the general API bucket, but still bound them+ // per client and form (including unauthenticated image requests).+ RateLimiter::for('submission-files', function (Request $request) {+ $form = $request->route('form');+ $formId = $form instanceof \App\Models\Forms\Form ? $form->getKey() : $form;+ $key = hash('sha256', $request->ip() . '|' . (string) $formId);++ return [+ Limit::perMinute(600)->by('submission-files:minute:' . $key),+ Limit::perHour(3000)->by('submission-files:hour:' . $key),+ ];+ });+RateLimiter::for('ai-formula-generation', function (Request $request) {$identifier = $request->user()? 'user:' . $request->user()->getAuthIdentifier()diff --git a/api/routes/api.php b/api/routes/api.phpindex f716fa077..a99aa3f64 100644--- a/api/routes/api.php+++ b/api/routes/api.php@@ -266,8 +266,8 @@ Route::group(['middleware' => 'auth.multi'], function () {->middleware('throttle:export-status')->name('export.status');Route::get('/file/{filename}', [FormSubmissionController::class, 'submissionFile'])- ->middleware('signed:relative')- ->withoutMiddleware(['auth.multi'])+ ->middleware(['signed:relative', 'throttle:submission-files'])+ ->withoutMiddleware(['auth.multi', 'throttle:100,1'])->name('file');Route::delete('/{submission_id}', [FormSubmissionController::class, 'destroy'])->name('destroy');Route::post('/multi', [FormSubmissionController::class, 'destroyMulti'])->name('destroy-multi');diff --git a/api/tests/Feature/Forms/FileDownloadSecurityTest.php b/api/tests/Feature/Forms/FileDownloadSecurityTest.phpindex 671718197..29e81233c 100644--- a/api/tests/Feature/Forms/FileDownloadSecurityTest.php+++ b/api/tests/Feature/Forms/FileDownloadSecurityTest.php@@ -87,3 +87,64 @@ it('does not eager load workspace users for signed submission file downloads', fexpect($queries->contains(fn (string $sql) => str_contains($sql, 'user_workspace')))->toBeFalse();});+++it('keeps signed attachment requests separate from the general API quota', function () {+ $this->withMiddleware(\Illuminate\Routing\Middleware\ThrottleRequests::class);+ $router = app('router');+ $middleware = $router->getMiddlewareGroups()['api'];+ array_unshift($middleware, 'throttle:100,1');+ $router->middlewareGroup('api', $middleware);++ $user = $this->actingAsProUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace);+ Storage::fake();+ $fileName = 'attachment.heic';+ Storage::put(FileUploadPathService::getFileUploadPath($form->id, $fileName), 'image bytes');+ $url = URL::temporarySignedRoute('open.forms.submissions.file', now()->addHour(), [+ $form->id, FilenameUrlEncoder::encode($fileName),+ ], absolute: false);++ // A real page with 100 rows and multiple photos can exceed 100 previews.+ for ($i = 0; $i < 175; $i++) {+ $this->get($url)->assertOk();+ }+ for ($i = 0; $i < 100; $i++) {+ $this->getJson(route('open.forms.submissions.index', ['form' => $form]))->assertSuccessful();+ }+ $this->getJson(route('open.forms.submissions.index', ['form' => $form]))->assertStatus(429);+ $this->get($url)->assertOk();++ // Raising attachment capacity must not remove expiry/signature checks.+ $expired = URL::temporarySignedRoute('open.forms.submissions.file', now()->subMinute(), [+ $form->id, FilenameUrlEncoder::encode($fileName),+ ], absolute: false);+ $this->get($expired)->assertForbidden();+ $this->get(strtok($url, '?'))->assertForbidden();+});++it('still rate limits signed attachment downloads per form and client', function () {+ $this->withMiddleware(\Illuminate\Routing\Middleware\ThrottleRequests::class);+ $user = $this->actingAsUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace);+ Storage::fake();+ $fileName = 'attachment.jpg';+ Storage::put(FileUploadPathService::getFileUploadPath($form->id, $fileName), 'image bytes');+ $url = URL::temporarySignedRoute('open.forms.submissions.file', now()->addHour(), [+ $form->id, FilenameUrlEncoder::encode($fileName),+ ], absolute: false);++ for ($i = 0; $i < 600; $i++) {+ $this->get($url)->assertOk();+ }+ $this->get($url)->assertStatus(429)->assertHeader('Retry-After');++ $otherForm = $this->createForm($user, $workspace);+ Storage::put(FileUploadPathService::getFileUploadPath($otherForm->id, $fileName), 'image bytes');+ $otherUrl = URL::temporarySignedRoute('open.forms.submissions.file', now()->addHour(), [+ $otherForm->id, FilenameUrlEncoder::encode($fileName),+ ], absolute: false);+ $this->get($otherUrl)->assertOk();+});diff --git a/docs/configuration/external-file-links.mdx b/docs/configuration/external-file-links.mdxindex 07248abcf..91479936f 100644--- a/docs/configuration/external-file-links.mdx+++ b/docs/configuration/external-file-links.mdx@@ -56,3 +56,13 @@ appear with their filename instead of exposing the raw API URL.example, a team that processes weekend submissions on Monday can select72 hours so links sent on Friday remain available through Monday.</Warning>+++## Download limits++Signed submission-file downloads use a separate quota of 600 requests per minute+and 3,000 per hour for each client IP and form. Image thumbnails count toward this+quota, but do not consume the general API quota.++Download links still require a valid, unexpired signature. A `429 Too Many+Attempts` response includes a `Retry-After` header; wait that long before retrying.