Skip to content

Beautiful Open-Source Form Builder

BE-001 - Refactor Zapier integration authorization and enhance testing for rea… (#1143)

Chirag Chhatrala9f3d98810eb5
diff --git a/api/app/Http/Controllers/Integrations/Zapier/IntegrationController.php b/api/app/Http/Controllers/Integrations/Zapier/IntegrationController.php
index fc4f52a10..86d0f1a53 100644
--- a/api/app/Http/Controllers/Integrations/Zapier/IntegrationController.php
+++ b/api/app/Http/Controllers/Integrations/Zapier/IntegrationController.php
@@ -18,7 +18,7 @@ class IntegrationController
{
$form = $request->form();
- $this->authorize('view', $form);
+ $this->authorize('manageIntegrations', $form);
$form->integrations()
->create([
@@ -36,7 +36,7 @@ class IntegrationController
{
$form = $request->form();
- $this->authorize('view', $form);
+ $this->authorize('manageIntegrations', $form);
$form
->integrations()
@@ -50,7 +50,7 @@ class IntegrationController
{
$form = $request->form();
- $this->authorize('view', $form);
+ $this->authorize('manageIntegrations', $form);
$lastSubmission = $form->submissions()->latest()->first();
$submissionData = null;
diff --git a/api/app/Policies/FormPolicy.php b/api/app/Policies/FormPolicy.php
index 39447420a..213ac2326 100644
--- a/api/app/Policies/FormPolicy.php
+++ b/api/app/Policies/FormPolicy.php
@@ -59,19 +59,25 @@ class FormPolicy
return $userIsNotReadonly;
}
+ /**
+ * Determine whether the user can access the form as a writable workspace member.
+ */
+ private function canAccessWritableForm(User $user, Form $form): bool
+ {
+ return $user->ownsForm($form) && !$form->workspace->isReadonlyUser($user);
+ }
+
/**
* Determine whether the user can perform write operations on the model.
*/
private function canPerformWriteOperation(User $user, Form $form): bool
{
- $ownsAndWritable = $user->ownsForm($form) && !$form->workspace->isReadonlyUser($user);
-
// If using Sanctum token, ensure the token has write ability
if ($token = $user->currentAccessToken()) {
- return $token->can('forms-write') && $ownsAndWritable;
+ return $token->can('forms-write') && $this->canAccessWritableForm($user, $form);
}
- return $ownsAndWritable;
+ return $this->canAccessWritableForm($user, $form);
}
/**
@@ -113,8 +119,7 @@ class FormPolicy
*/
public function manageIntegrations(User $user, Form $form)
{
- // First check if user has write permission
- if (!$this->canPerformWriteOperation($user, $form)) {
+ if (!$this->canAccessWritableForm($user, $form)) {
return false;
}
diff --git a/api/tests/Feature/Zapier/IntegrationsTest.php b/api/tests/Feature/Zapier/IntegrationsTest.php
index d2b0c2e0c..602446dfb 100644
--- a/api/tests/Feature/Zapier/IntegrationsTest.php
+++ b/api/tests/Feature/Zapier/IntegrationsTest.php
@@ -10,6 +10,11 @@ use function Pest\Laravel\delete;
use function Pest\Laravel\post;
use function PHPUnit\Framework\assertEquals;
+function zapierIntegrationTokenAbilities(): array
+{
+ return ['manage-integrations'];
+}
+
test('create an integration', function () {
$user = User::factory()->create();
$workspace = createUserWorkspace($user);
@@ -18,7 +23,7 @@ test('create an integration', function () {
Sanctum::actingAs(
$user,
- ['manage-integrations']
+ zapierIntegrationTokenAbilities()
);
$this->withoutExceptionHandling();
@@ -36,6 +41,105 @@ test('create an integration', function () {
assertEquals($hookUrl, $integration->data->hook_url);
});
+test('writable workspace members can create integrations with only the manage integrations ability', function () {
+ $owner = User::factory()->create();
+ $workspace = createUserWorkspace($owner);
+ $form = createForm($owner, $workspace, ['title' => 'First form']);
+
+ $member = User::factory()->create();
+ $member->workspaces()->sync([$workspace->id => ['role' => 'user']], false);
+
+ Sanctum::actingAs($member, zapierIntegrationTokenAbilities());
+
+ post(route('zapier.webhooks.store'), [
+ 'form_id' => $form->id,
+ 'hookUrl' => $hookUrl = 'https://zapier.com/hook/test',
+ ])->assertOk();
+
+ assertDatabaseCount('form_integrations', 1);
+
+ $integration = FormIntegration::first();
+
+ assertEquals($form->id, $integration->form_id);
+ assertEquals($hookUrl, $integration->data->hook_url);
+});
+
+test('readonly workspace members cannot create integrations even with form write token ability', function () {
+ $owner = User::factory()->create();
+ $workspace = createUserWorkspace($owner);
+ $form = createForm($owner, $workspace, ['title' => 'First form']);
+
+ $readonly = User::factory()->create();
+ $readonly->workspaces()->sync([$workspace->id => ['role' => 'readonly']], false);
+
+ Sanctum::actingAs($readonly, ['forms-write', 'manage-integrations']);
+
+ post(route('zapier.webhooks.store'), [
+ 'form_id' => $form->id,
+ 'hookUrl' => 'https://zapier.com/hook/test',
+ ])->assertForbidden();
+
+ assertDatabaseCount('form_integrations', 0);
+});
+
+test('readonly workspace members cannot create integrations even with integration token abilities', function () {
+ $owner = User::factory()->create();
+ $workspace = createUserWorkspace($owner);
+ $form = createForm($owner, $workspace, ['title' => 'First form']);
+
+ $readonly = User::factory()->create();
+ $readonly->workspaces()->sync([$workspace->id => ['role' => 'readonly']], false);
+
+ Sanctum::actingAs($readonly, zapierIntegrationTokenAbilities());
+
+ post(route('zapier.webhooks.store'), [
+ 'form_id' => $form->id,
+ 'hookUrl' => 'https://zapier.com/hook/test',
+ ])->assertForbidden();
+
+ assertDatabaseCount('form_integrations', 0);
+});
+
+test('readonly workspace members cannot delete integrations even with integration token abilities', function () {
+ $owner = User::factory()->create();
+ $workspace = createUserWorkspace($owner);
+ $form = createForm($owner, $workspace, ['title' => 'First form']);
+
+ $integration = FormIntegration::factory()
+ ->for($form)
+ ->create([
+ 'data' => [
+ 'hook_url' => $hookUrl = 'https://zapier.com/hook/test',
+ ],
+ ]);
+
+ $readonly = User::factory()->create();
+ $readonly->workspaces()->sync([$workspace->id => ['role' => 'readonly']], false);
+
+ Sanctum::actingAs($readonly, zapierIntegrationTokenAbilities());
+
+ delete(route('zapier.webhooks.destroy'), [
+ 'form_id' => $form->id,
+ 'hookUrl' => $hookUrl,
+ ])->assertForbidden();
+
+ assertDatabaseCount('form_integrations', 1);
+});
+
+test('readonly workspace members cannot poll submissions even with integration token abilities', function () {
+ $owner = User::factory()->create();
+ $workspace = createUserWorkspace($owner);
+ $form = createForm($owner, $workspace, ['title' => 'First form']);
+
+ $readonly = User::factory()->create();
+ $readonly->workspaces()->sync([$workspace->id => ['role' => 'readonly']], false);
+
+ Sanctum::actingAs($readonly, zapierIntegrationTokenAbilities());
+
+ $this->getJson(route('zapier.webhooks.poll', ['form_id' => $form->id]))
+ ->assertForbidden();
+});
+
test('cannot create an integration without a corresponding ability', function () {
$user = User::factory()->create();
$workspace = createUserWorkspace($user);
@@ -62,7 +166,7 @@ test('cannot create an integration with a private hook url', function () {
Sanctum::actingAs(
$user,
- ['manage-integrations']
+ zapierIntegrationTokenAbilities()
);
post(route('zapier.webhooks.store'), [
@@ -103,7 +207,7 @@ test('delete an integration', function () {
Sanctum::actingAs(
$user,
- ['manage-integrations']
+ zapierIntegrationTokenAbilities()
);
$integration = FormIntegration::factory()
@@ -133,7 +237,7 @@ test('cannot delete an integration with an incorrect hook url', function () {
Sanctum::actingAs(
$user,
- ['manage-integrations']
+ zapierIntegrationTokenAbilities()
);
$integration = FormIntegration::factory()
@@ -198,7 +302,7 @@ test('poll for the latest submission', function () {
]
]);
- Sanctum::actingAs($user, ['view', 'manage-integrations']);
+ Sanctum::actingAs($user, zapierIntegrationTokenAbilities());
// Call the poll endpoint
$response = $this->getJson(route('zapier.webhooks.poll', ['form_id' => $form->id]));
@@ -248,7 +352,7 @@ test('make up a submission when polling without any submission', function () {
]
]);
- Sanctum::actingAs($user, ['view', 'manage-integrations']);
+ Sanctum::actingAs($user, zapierIntegrationTokenAbilities());
// Call the poll endpoint
$this->withoutExceptionHandling();