Beautiful Open-Source Form Builder
Enhance FormResource and WorkspaceResource for guest access control
Julien Nahum0e999243dbe9
diff --git a/api/app/Http/Resources/FormResource.php b/api/app/Http/Resources/FormResource.phpindex 9e21c4db0..c3ce456c9 100644--- a/api/app/Http/Resources/FormResource.php+++ b/api/app/Http/Resources/FormResource.php@@ -52,7 +52,9 @@ class FormResource extends JsonResource'is_pro' => $this->workspaceIsPro(),'is_trialing' => $this->workspaceIsTrialing(),'workspace_id' => $this->workspace_id,- 'workspace' => new WorkspaceResource($this->workspace),+ 'workspace' => $this->userIsFormOwner()+ ? new WorkspaceResource($this->workspace)+ : (new WorkspaceResource($this->workspace))->restrictForGuest(),'is_closed' => $this->is_closed,'size' => $this->size,'is_password_protected' => false,diff --git a/api/app/Http/Resources/WorkspaceResource.php b/api/app/Http/Resources/WorkspaceResource.phpindex 53786577f..4da60fba6 100644--- a/api/app/Http/Resources/WorkspaceResource.php+++ b/api/app/Http/Resources/WorkspaceResource.php@@ -8,6 +8,21 @@ class WorkspaceResource extends JsonResource{public static $wrap = null;+ /**+ * When true, only expose minimal public fields (for guests/non-members).+ */+ private bool $restrictForGuest = false;++ /**+ * Enable guest-restricted output for this resource instance.+ */+ public function restrictForGuest(): self+ {+ $this->restrictForGuest = true;++ return $this;+ }+/*** Transform the resource into an array.*@@ -16,6 +31,14 @@ class WorkspaceResource extends JsonResource*/public function toArray($request){+ if ($this->restrictForGuest) {+ // Minimal public shape: avoid leaking workspace details+ return [+ 'id' => $this->resource->id,+ 'max_file_size' => $this->resource->max_file_size / 1000000,+ ];+ }+return array_merge(parent::toArray($request), ['max_file_size' => $this->max_file_size / 1000000,'is_readonly' => $this->isReadonlyUser($request->user()),diff --git a/api/tests/Feature/Forms/FormTest.php b/api/tests/Feature/Forms/FormTest.phpindex fbc09398b..f6a1bee77 100644--- a/api/tests/Feature/Forms/FormTest.php+++ b/api/tests/Feature/Forms/FormTest.php@@ -47,6 +47,25 @@ it('can fetch a form', function () {]);});+it('does not leak workspace details on public form fetch', function () {+ $user = \App\Models\User::factory()->create();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace);++ $response = $this->getJson(route('forms.show', $form->slug))+ ->assertSuccessful()+ ->assertJsonPath('id', $form->id)+ ->assertJsonPath('workspace_id', $workspace->id)+ ->assertJsonPath('workspace.id', $workspace->id)+ ->assertJsonPath('workspace.max_file_size', $workspace->max_file_size / 1000000);++ $response->assertJsonMissingPath('workspace.users');+ $response->assertJsonMissingPath('workspace.settings');+ $response->assertJsonMissingPath('workspace.is_admin');+ $response->assertJsonMissingPath('workspace.is_readonly');+ $response->assertJsonMissingPath('workspace.users_count');+});+it('can update a form', function () {$user = $this->actingAsUser();$workspace = $this->createUserWorkspace($user);