Beautiful Open-Source Form Builder
Fix submission file URLs moving shared form assets (#1370)
Julien Nahuma1f64848ac26
diff --git a/README.md b/README.mdindex d0b90455b..19d0e9cbd 100644--- a/README.md+++ b/README.md@@ -102,6 +102,10 @@ The form editor shows whether changes are saved. For an existing form, **Save ChClearing editor history uses the currently loaded form as the undo baseline and discards pending history updates. A subsequent edit can be undone back to that form without restoring an earlier loading state.+## Submission file references++File fields accept temporary upload references. To retain an existing upload through a URL, send its valid, unexpired signed download URL for the same form and application origin. Refresh submission data to obtain a new URL if it has expired. A prefilled file configured in the editor is copied into submission storage under a fresh name for each submission; its shared source remains available. Other shared form assets and arbitrary external URLs cannot be used as submission uploads. Submission processing only moves files from temporary upload storage; it never moves shared form images.+## Spreadsheet exportsSpreadsheet exports preserve numeric zero values, account identifiers with leading zeroes, and Unicode. Null and empty fields remain blank.diff --git a/api/app/Http/Requests/AnswerFormRequest.php b/api/app/Http/Requests/AnswerFormRequest.phpindex 1a129c1df..ce7e8b203 100644--- a/api/app/Http/Requests/AnswerFormRequest.php+++ b/api/app/Http/Requests/AnswerFormRequest.php@@ -247,7 +247,7 @@ class AnswerFormRequest extends FormRequestif (!empty($property['allowed_file_types'])) {$allowedFileTypes = explode(',', $property['allowed_file_types']);}- $this->requestRules[$property['id'] . '.*'] = [new StorageFile($this->getFieldMaxFileSize($property), $allowedFileTypes, $this->form)];+ $this->requestRules[$property['id'] . '.*'] = [new StorageFile($this->getFieldMaxFileSize($property), $allowedFileTypes, $this->form, $property)];return ['array'];case 'email':diff --git a/api/app/Jobs/Form/StoreFormSubmissionJob.php b/api/app/Jobs/Form/StoreFormSubmissionJob.phpindex 100b27829..329a0eebf 100644--- a/api/app/Jobs/Form/StoreFormSubmissionJob.php+++ b/api/app/Jobs/Form/StoreFormSubmissionJob.php@@ -3,7 +3,6 @@namespace App\Jobs\Form;use App\Events\Forms\FormSubmitted;-use App\Http\Controllers\Forms\FormController;use App\Http\Requests\AnswerFormRequest;use App\Service\Storage\FileUploadPathService;use App\Models\Forms\Form;@@ -12,7 +11,6 @@ use App\Service\Billing\Feature;use App\Service\Forms\FormAutoIncrementSequence;use App\Service\Forms\FormLogicPropertyResolver;use App\Service\Forms\SubmissionAttribution;-use App\Service\Storage\FilenameUrlEncoder;use App\Service\Storage\StorageFileNameParser;use Illuminate\Bus\Queueable;use Illuminate\Contracts\Queue\ShouldQueue;@@ -67,6 +65,7 @@ class StoreFormSubmissionJob implements ShouldQueueprivate ?string $submitterIp = null;private array $attribution = [];private array $storedMeta = [];+ private array $prefilledFileCopies = [];/*** Create a new job instance.@@ -189,7 +188,6 @@ class StoreFormSubmissionJob implements ShouldQueue$record = $query->first();if ($record) {- $this->isClientProvidedSubmissionId = true;return $record->id;}return null;@@ -203,9 +201,9 @@ class StoreFormSubmissionJob implements ShouldQueueprivate function storeSubmission(array &$formData){DB::transaction(function () use (&$formData) {- // Handle record updateif ($recordToUpdate = $this->resolveRecordToUpdate($this->submissionData)) {$this->submissionId = $recordToUpdate;+ $this->isClientProvidedSubmissionId = true;}if ($this->submissionId) {@@ -270,6 +268,11 @@ class StoreFormSubmissionJob implements ShouldQueue$existingMeta = $submission->meta ?? [];$metaChanged = false;+ if ($this->prefilledFileCopies !== []) {+ $existingMeta['prefilled_files'] = array_replace($existingMeta['prefilled_files'] ?? [], $this->prefilledFileCopies);+ $metaChanged = true;+ }+if ($isNewSubmission && !empty($this->attribution)) {$existingMeta['attribution'] = $this->attribution;$metaChanged = true;@@ -333,7 +336,7 @@ class StoreFormSubmissionJob implements ShouldQueue$processedFiles = [];foreach ($answerValue as $fileItem) {if (is_string($fileItem) && !empty($fileItem)) {- $singleStoredFile = $this->storeFile($fileItem);+ $singleStoredFile = $this->storeFile($fileItem, $field);if ($singleStoredFile) {$processedFiles[] = $singleStoredFile;}@@ -342,10 +345,10 @@ class StoreFormSubmissionJob implements ShouldQueue$finalData[$field['id']] = $processedFiles;} else {if (is_string($answerValue) && !empty($answerValue)) {- $singleFileResult = $this->storeFile($answerValue);+ $singleFileResult = $this->storeFile($answerValue, $field);$finalData[$field['id']] = $singleFileResult;} else {- $finalData[$field['id']] = $this->storeFile($answerValue); // Handles null/empty $answerValue by returning null+ $finalData[$field['id']] = $this->storeFile($answerValue, $field); // Handles null/empty $answerValue by returning null}}} else {@@ -399,32 +402,37 @@ class StoreFormSubmissionJob implements ShouldQueue* - file_name-{uuid}.{ext}* - {uuid}*/- private function storeFile($value, ?bool $isPublic = null)+ private function storeFile($value, ?array $field = null){if (is_null($value) || empty($value)) {return null;}- // Handle pre-existing full URLs (e.g., from prefill)- if (filter_var($value, FILTER_VALIDATE_URL) !== false && str_contains($value, parse_url(config('app.url'))['host'])) {- $fileName = explode('?', basename($value))[0];- if (FilenameUrlEncoder::isEncoded($fileName)) {- $fileName = FilenameUrlEncoder::decode($fileName);- }-- // Existing submission uploads are returned as signed URLs. Keep their- // canonical storage name when an edit submits that URL back to us.- if ($this->isSkipForUpload($fileName)) {- $parser = StorageFileNameParser::parse($fileName);-- return $parser->getMovedFileName() ?? $fileName;- }-- $path = FormController::ASSETS_UPLOAD_PATH . '/' . $fileName; // Assuming assets are in a defined path- $newPath = FileUploadPathService::getFileUploadPath($this->form->id, $fileName);- if (!Storage::move($path, $newPath)) {- throw new \RuntimeException('File storage failed.');+ // Re-check URLs here because partial submissions may skip request validation.+ // Submission processing must never move shared form assets.+ if (filter_var($value, FILTER_VALIDATE_URL) !== false) {+ if ($assetName = FileUploadPathService::resolvePrefilledAssetUrl($value, $field)) {+ if ($this->prefilledFileCopies === []) {+ $existingSubmissionId = $this->submissionId ?? $this->resolveRecordToUpdate($this->submissionData);+ if ($existingSubmissionId) {+ $existingSubmission = $this->form->submissions()->findOrFail($existingSubmissionId);+ $this->prefilledFileCopies = $existingSubmission->meta['prefilled_files'] ?? [];+ }+ }+ $copyKey = hash('sha256', json_encode([$field['id'], $assetName]));+ $existingCopy = $this->prefilledFileCopies[$copyKey] ?? null;+ if ($existingCopy && Storage::exists(FileUploadPathService::getFileUploadPath($this->form->id, $existingCopy))) {+ return $existingCopy;+ }+ $parser = StorageFileNameParser::parse($assetName);+ $parser->uuid = (string) Str::uuid();+ $newFileName = $parser->getMovedFileName();+ if (!Storage::copy('assets/forms/' . $assetName, FileUploadPathService::getFileUploadPath($this->form->id, $newFileName))) {+ throw new \RuntimeException('File storage failed.');+ }+ $this->prefilledFileCopies[$copyKey] = $newFileName;+ return $newFileName;}- return $fileName;+ return FileUploadPathService::resolveSubmissionFileUrl($value, $this->form->id);}$shouldSkip = $this->isSkipForUpload($value);diff --git a/api/app/Rules/StorageFile.php b/api/app/Rules/StorageFile.phpindex 3a0096663..b371acc0e 100644--- a/api/app/Rules/StorageFile.php+++ b/api/app/Rules/StorageFile.php@@ -13,7 +13,7 @@ class StorageFile implements ValidationRule{public string $error = 'Invalid file.';- public function __construct(public int $maxSize, public array $fileTypes = [], public ?Form $form = null)+ public function __construct(public int $maxSize, public array $fileTypes = [], public ?Form $form = null, public ?array $field = null){$this->fileTypes = array_values(array_filter(array_map(fn ($type) => strtolower(ltrim(trim($type), '.')),@@ -31,9 +31,15 @@ class StorageFile implements ValidationRule*/public function passes($attribute, $value): bool{- // If full path then no need to validate+ if (!is_string($value)) {+ return false;+ }++ // URLs may retain signed uploads or copy the field's configured prefill.if (filter_var($value, FILTER_VALIDATE_URL) !== false) {- return true;+ return $this->form !== null+ && (FileUploadPathService::resolveSubmissionFileUrl($value, $this->form->id) !== null+ || FileUploadPathService::resolvePrefilledAssetUrl($value, $this->field) !== null);}$fileNameParser = StorageFileNameParser::parse($value);diff --git a/api/app/Service/Storage/FileUploadPathService.php b/api/app/Service/Storage/FileUploadPathService.phpindex 9c506a4b6..69622b959 100644--- a/api/app/Service/Storage/FileUploadPathService.php+++ b/api/app/Service/Storage/FileUploadPathService.php@@ -3,6 +3,9 @@namespace App\Service\Storage;use Illuminate\Support\Str;+use Illuminate\Http\Request;+use Illuminate\Support\Facades\Storage;+use Illuminate\Support\Facades\URL;/*** Service for managing file upload paths in the application@@ -22,6 +25,69 @@ class FileUploadPathService*/private const TMP_FILE_UPLOAD_PATH = 'tmp/';+ /** Resolve only signed download URLs for an existing upload in this form. */+ public static function resolveSubmissionFileUrl(string $value, int|string $formId): ?string+ {+ if (!self::hasApplicationOrigin($value)) {+ return null;+ }++ $path = parse_url($value, PHP_URL_PATH) ?? '';+ $segment = rawurldecode(basename($path));+ $expectedPath = route('open.forms.submissions.file', [$formId, $segment], false);+ if ($path !== $expectedPath || !URL::hasValidSignature(Request::create($value), absolute: false)) {+ return null;+ }++ $fileName = FilenameUrlEncoder::isEncoded($segment) ? FilenameUrlEncoder::decode($segment) : $segment;+ if ($fileName === '' || in_array($fileName, ['.', '..'], true)+ || str_contains($fileName, '/') || str_contains($fileName, '\\') || str_contains($fileName, "\0")) {+ return null;+ }++ return Storage::exists(self::getFileUploadPath($formId, $fileName)) ? $fileName : null;+ }++ /** Only the persisted prefill for this field can authorize a shared asset copy. */+ public static function resolvePrefilledAssetUrl(string $value, ?array $field): ?string+ {+ if (($field['type'] ?? null) !== 'files'+ || !in_array($value, (array) ($field['prefill'] ?? []), true)+ || !self::hasApplicationOrigin($value)) {+ return null;+ }++ $path = parse_url($value, PHP_URL_PATH) ?? '';+ $fileName = rawurldecode(basename($path));+ if (parse_url($value, PHP_URL_QUERY) !== null+ || $path !== route('forms.assets.show', [$fileName], false)+ || str_contains($fileName, '/') || str_contains($fileName, '\\')+ || !StorageFileNameParser::parse($fileName)->uuid) {+ return null;+ }++ return Storage::exists('assets/forms/' . $fileName) ? $fileName : null;+ }++ private static function hasApplicationOrigin(string $value): bool+ {+ if (filter_var($value, FILTER_VALIDATE_URL) === false) {+ return false;+ }++ $parts = parse_url($value);+ $origin = parse_url(config('app.url') ?? '');+ if (!is_array($parts) || !is_array($origin)+ || isset($parts['user']) || isset($parts['pass']) || isset($parts['fragment'])+ || !in_array(strtolower($parts['scheme'] ?? ''), ['http', 'https'], true)+ || strtolower($parts['scheme'] ?? '') !== strtolower($origin['scheme'] ?? '')+ || strtolower($parts['host'] ?? '') !== strtolower($origin['host'] ?? '')+ || ($parts['port'] ?? null) !== ($origin['port'] ?? null)) {+ return false;+ }+ return true;+ }+/*** Generate the file upload path for a specific form*diff --git a/api/tests/Feature/Submissions/SubmissionFileIsolationTest.php b/api/tests/Feature/Submissions/SubmissionFileIsolationTest.phpnew file mode 100644index 000000000..faf028464--- /dev/null+++ b/api/tests/Feature/Submissions/SubmissionFileIsolationTest.php@@ -0,0 +1,209 @@+<?php++use App\Jobs\Form\StoreFormSubmissionJob;+use App\Rules\StorageFile;+use App\Service\Storage\FilenameUrlEncoder;+use App\Service\Storage\FileUploadPathService;+use Illuminate\Support\Facades\Storage;+use Illuminate\Support\Facades\URL;++beforeEach(function () {+ Storage::fake('local');+ config()->set('app.url', 'http://localhost');+ $user = $this->createUser();+ $workspace = $this->createUserWorkspace($user);+ $this->form = $this->createForm($user, $workspace, [+ 'properties' => [['id' => 'attachment', 'name' => 'Attachment', 'type' => 'files']],+ ]);+ $this->fileName = 'cover_550e8400-e29b-41d4-a716-446655440000.png';+ Storage::put('assets/forms/' . $this->fileName, 'shared cover');+});++it('rejects shared asset URLs in anonymous submissions', function (string $kind) {+ $url = $kind === 'query host'+ ? 'https://attacker.example/' . $this->fileName . '?h=localhost'+ : route('forms.assets.show', $this->fileName);++ $this->assertGuest();+ $this->postJson(route('forms.answer', $this->form->slug), [+ 'attachment' => [$url],+ ])->assertUnprocessable()->assertJsonValidationErrors('attachment.0');++ expect(Storage::get('assets/forms/' . $this->fileName))->toBe('shared cover');+ Storage::assertMissing(FileUploadPathService::getFileUploadPath($this->form->id, $this->fileName));+ expect($this->form->submissions()->count())->toBe(0);+})->with(['query host', 'real asset URL']);++it('never moves a shared asset even when the job is invoked without validation', function (string $kind) {+ $url = $kind === 'query host'+ ? 'https://attacker.example/' . $this->fileName . '?h=localhost'+ : route('forms.assets.show', $this->fileName);++ (new StoreFormSubmissionJob($this->form, ['attachment' => [$url], 'is_partial' => true]))->handle();++ expect(Storage::get('assets/forms/' . $this->fileName))->toBe('shared cover');+ Storage::assertMissing(FileUploadPathService::getFileUploadPath($this->form->id, $this->fileName));+ expect($this->form->submissions()->first()->data['attachment'])->toBe([]);+})->with(['query host', 'real asset URL']);++it('rejects untrusted existing-upload URLs in both validation and storage', function (string $kind) {+ $path = FileUploadPathService::getFileUploadPath($this->form->id, $this->fileName);+ Storage::put($path, 'existing upload');+ $parameters = [$this->form->id, FilenameUrlEncoder::encode($this->fileName)];+ $url = URL::publicSignedRoute('open.forms.submissions.file', $parameters);++ $url = match ($kind) {+ 'unsigned' => strtok($url, '?'),+ 'wrong host' => str_replace('http://localhost', 'http://attacker.example', $url),+ 'host suffix' => str_replace('http://localhost', 'http://localhost.attacker.example', $url),+ 'userinfo' => str_replace('http://localhost', 'http://[email protected]', $url),+ 'wrong port' => str_replace('http://localhost', 'http://localhost:8888', $url),+ 'wrong scheme' => str_replace('http://localhost', 'https://localhost', $url),+ 'tampered' => $url . '&extra=1',+ 'expired' => URL::publicSignedRoute('open.forms.submissions.file', $parameters, now()->subMinute()),+ 'other form' => URL::publicSignedRoute('open.forms.submissions.file', [$this->form->id + 1, $parameters[1]]),+ 'other route' => URL::publicSignedRoute('forms.assets.show', [$this->fileName]),+ 'missing file' => URL::publicSignedRoute('open.forms.submissions.file', [$this->form->id, FilenameUrlEncoder::encode('missing.png')]),+ };++ expect((new StorageFile(1000000, [], $this->form))->passes('attachment', $url))->toBeFalse();+ (new StoreFormSubmissionJob($this->form, ['attachment' => [$url], 'is_partial' => true]))->handle();++ expect($this->form->submissions()->first()->data['attachment'])->toBe([]);+ expect(Storage::get($path))->toBe('existing upload');+ expect(Storage::get('assets/forms/' . $this->fileName))->toBe('shared cover');+})->with(['unsigned', 'wrong host', 'host suffix', 'userinfo', 'wrong port', 'wrong scheme', 'tampered', 'expired', 'other form', 'other route', 'missing file']);++it('retains a valid signed upload without moving or renaming it', function (string $fileName) {+ $path = FileUploadPathService::getFileUploadPath($this->form->id, $fileName);+ Storage::put($path, 'existing upload');+ $url = URL::publicSignedRoute('open.forms.submissions.file', [+ $this->form->id, FilenameUrlEncoder::encode($fileName),+ ], now()->addMinutes(10));++ expect((new StorageFile(1000000, [], $this->form))->passes('attachment', $url))->toBeTrue();+ (new StoreFormSubmissionJob($this->form, ['attachment' => [$url], 'is_partial' => true]))->handle();++ expect($this->form->submissions()->first()->data['attachment'])->toBe([$fileName]);+ expect(Storage::get($path))->toBe('existing upload');+})->with(['receipt_550e8400-e29b-41d4-a716-446655440000.png', "reçu (client's copy)_550e8400-e29b-41d4-a716-446655440000.pdf"]);++it('still moves a validated temporary upload into the form submissions', function () {+ $uuid = '550e8400-e29b-41d4-a716-446655440001';+ $fileName = 'receipt_' . $uuid . '.pdf';+ Storage::put(FileUploadPathService::getTmpFileUploadPath($uuid), 'uploaded PDF');++ expect((new StorageFile(1000000, ['pdf'], $this->form))->passes('attachment', $fileName))->toBeTrue();+ (new StoreFormSubmissionJob($this->form, ['attachment' => [$fileName], 'is_partial' => true]))->handle();++ Storage::assertMissing(FileUploadPathService::getTmpFileUploadPath($uuid));+ expect(Storage::get(FileUploadPathService::getFileUploadPath($this->form->id, $fileName)))->toBe('uploaded PDF');+});++it('copies the configured prefilled file for each anonymous submission', function (bool $multiple) {+ $url = route('forms.assets.show', $this->fileName);+ $this->form->update(['properties' => [[+ 'id' => 'attachment', 'name' => 'Attachment', 'type' => 'files',+ 'multiple' => $multiple, 'prefill' => $multiple ? [$url] : $url,+ ]]]);++ $this->assertGuest();+ for ($i = 0; $i < 2; $i++) {+ $this->postJson(route('forms.answer', $this->form->slug), [+ 'attachment' => [$url],+ ])->assertSuccessful();+ }++ $names = $this->form->submissions()->get()->map(fn ($submission) => $submission->data['attachment'][0]);+ expect($names)->toHaveCount(2);+ expect($names->unique())->toHaveCount(2);+ foreach ($names as $name) {+ expect($name)->not->toBe($this->fileName);+ expect(Storage::get(FileUploadPathService::getFileUploadPath($this->form->id, $name)))->toBe('shared cover');+ }+ expect(Storage::get('assets/forms/' . $this->fileName))->toBe('shared cover');+})->with([false, true]);++it('copies configured prefills when the job bypasses request validation', function () {+ $url = route('forms.assets.show', $this->fileName);+ $this->form->update(['properties' => [[+ 'id' => 'attachment', 'name' => 'Attachment', 'type' => 'files', 'prefill' => [$url],+ ]]]);++ (new StoreFormSubmissionJob($this->form, ['attachment' => [$url], 'is_partial' => true]))->handle();++ $name = $this->form->submissions()->first()->data['attachment'][0];+ expect($name)->not->toBe($this->fileName);+ expect(Storage::get(FileUploadPathService::getFileUploadPath($this->form->id, $name)))->toBe('shared cover');+ expect(Storage::get('assets/forms/' . $this->fileName))->toBe('shared cover');+});++it('does not authorize a prefilled asset from a different field', function () {+ $url = route('forms.assets.show', $this->fileName);+ $this->form->update(['properties' => [+ ['id' => 'attachment', 'name' => 'Attachment', 'type' => 'files'],+ ['id' => 'other', 'name' => 'Other file', 'type' => 'files', 'prefill' => [$url]],+ ]]);++ $this->postJson(route('forms.answer', $this->form->slug), [+ 'attachment' => [$url],+ ])->assertUnprocessable()->assertJsonValidationErrors('attachment.0');++ (new StoreFormSubmissionJob($this->form, ['attachment' => [$url], 'is_partial' => true]))->handle();+ expect($this->form->submissions()->first()->data['attachment'])->toBe([]);+ expect(Storage::get('assets/forms/' . $this->fileName))->toBe('shared cover');+ Storage::assertMissing(FileUploadPathService::getFileUploadPath($this->form->id, $this->fileName));+});++it('reuses the prefilled copy through partial autosaves and completion', function () {+ $user = $this->actingAsBusinessUser();+ $workspace = $this->createUserWorkspace($user);+ $url = route('forms.assets.show', $this->fileName);+ $fieldId = '550e8400-e29b-41d4-a716-446655440002';+ $form = $this->createForm($user, $workspace, [+ 'enable_partial_submissions' => true,+ 'properties' => [['id' => $fieldId, 'name' => 'Attachment', 'type' => 'files', 'prefill' => [$url]]],+ ]);+ $this->actingAsGuest();++ $response = $this->postJson(route('forms.answer', $form->slug), [+ $fieldId => [$url], 'is_partial' => true,+ ])->assertSuccessful();+ $hash = $response->json('submission_hash');+ expect($hash)->not->toBeEmpty();+ $submission = $form->submissions()->first();+ $originalCopy = $submission->data[$fieldId][0];++ foreach ([true, true, false] as $isPartial) {+ $this->postJson(route('forms.answer', $form->slug), [+ $fieldId => [$url], 'is_partial' => $isPartial, 'submission_hash' => $hash,+ ])->assertSuccessful();+ expect($submission->fresh()->data[$fieldId])->toBe([$originalCopy]);+ expect(Storage::allFiles(FileUploadPathService::getFileUploadPath($form->id)))->toHaveCount(1);+ }++ expect($form->submissions()->count())->toBe(1);+ expect($submission->fresh()->status)->toBe(\App\Models\Forms\FormSubmission::STATUS_COMPLETED);+ expect(Storage::get('assets/forms/' . $this->fileName))->toBe('shared cover');+});++it('reuses the prefilled copy when matching an existing submission by field values', function () {+ $user = $this->actingAsBusinessUser();+ $workspace = $this->createUserWorkspace($user);+ $url = route('forms.assets.show', $this->fileName);+ $form = $this->createForm($user, $workspace, [+ 'database_fields_update' => ['email'],+ 'properties' => [+ ['id' => 'email', 'name' => 'Email', 'type' => 'email'],+ ['id' => 'attachment', 'name' => 'Attachment', 'type' => 'files', 'prefill' => [$url]],+ ],+ ]);++ for ($i = 0; $i < 3; $i++) {+ (new StoreFormSubmissionJob($form, ['email' => '[email protected]', 'attachment' => [$url]]))->handle();+ }++ expect($form->submissions()->count())->toBe(1);+ expect(Storage::allFiles(FileUploadPathService::getFileUploadPath($form->id)))->toHaveCount(1);+ expect(Storage::get('assets/forms/' . $this->fileName))->toBe('shared cover');+});