Skip to content

Beautiful Open-Source Form Builder

Enhance form summary controller and testing for workspace validation (#1144)

Chirag Chhatrala99d41293a2c2
diff --git a/api/app/Http/Controllers/Forms/FormSummaryController.php b/api/app/Http/Controllers/Forms/FormSummaryController.php
index bc3a8db02..fb14843ce 100644
--- a/api/app/Http/Controllers/Forms/FormSummaryController.php
+++ b/api/app/Http/Controllers/Forms/FormSummaryController.php
@@ -19,6 +19,7 @@ class FormSummaryController extends Controller
public function getSummary(FormSummaryRequest $request, Workspace $workspace, Form $form): JsonResponse
{
+ $this->ensureFormBelongsToWorkspace($form, $workspace);
$this->authorize('view', $form);
$summary = $this->summaryService->generateSummary(
@@ -33,6 +34,7 @@ class FormSummaryController extends Controller
public function getFieldValues(FormSummaryRequest $request, Workspace $workspace, Form $form, string $fieldId): JsonResponse
{
+ $this->ensureFormBelongsToWorkspace($form, $workspace);
$this->authorize('view', $form);
$values = $this->summaryService->getFieldTextValues(
@@ -50,4 +52,11 @@ class FormSummaryController extends Controller
return response()->json($values);
}
+
+ private function ensureFormBelongsToWorkspace(Form $form, Workspace $workspace): void
+ {
+ if ($form->workspace_id !== $workspace->id) {
+ abort(404);
+ }
+ }
}
diff --git a/api/tests/Feature/Forms/FormSummaryTest.php b/api/tests/Feature/Forms/FormSummaryTest.php
index 0a0e4ac2d..de5d561fc 100644
--- a/api/tests/Feature/Forms/FormSummaryTest.php
+++ b/api/tests/Feature/Forms/FormSummaryTest.php
@@ -280,6 +280,22 @@ describe('Form Summary', function () {
$this->getJson(route('open.workspaces.form.summary', [$otherWorkspace, $otherForm]))
->assertStatus(402);
});
+
+ it('returns 404 when workspace and form do not match on summary endpoints', function () {
+ $workspaceA = $this->createUserWorkspace($this->user);
+ $workspaceB = $this->createUserWorkspace($this->user);
+ $formInWorkspaceA = $this->createForm($this->user, $workspaceA);
+ $nameProperty = collect($formInWorkspaceA->properties)->firstWhere('name', 'Name');
+
+ $this->getJson(route('open.workspaces.form.summary', [$workspaceB, $formInWorkspaceA]))
+ ->assertStatus(404);
+
+ $this->getJson(route('open.workspaces.form.summary.field-values', [
+ $workspaceB,
+ $formInWorkspaceA,
+ $nameProperty['id'],
+ ]))->assertStatus(404);
+ });
});
describe('Form Summary Field Values (Load More)', function () {