A free open source IT asset/license management system
Add deny-list for webhooks on company
snipea09c362eb34d
diff --git a/app/Http/Controllers/Api/CompaniesController.php b/app/Http/Controllers/Api/CompaniesController.phpindex 3ea48f3efe..efce61f891 100644--- a/app/Http/Controllers/Api/CompaniesController.php+++ b/app/Http/Controllers/Api/CompaniesController.php@@ -137,6 +137,11 @@ class CompaniesController extends Controllerpublic function store(ImageUploadRequest $request): JsonResponse{$this->authorize('create', Company::class);++ if ($denied = $this->rejectUnauthorizedWebhookFields($request)) {+ return $denied;+ }+$company = new Company;$company->fill($request->all());$company->created_by = auth()->id();@@ -191,6 +196,11 @@ class CompaniesController extends Controller$this->authorize('update', Company::class);$company = Company::findOrFail($id);$this->authorize('update', $company);++ if ($denied = $this->rejectUnauthorizedWebhookFields($request)) {+ return $denied;+ }+$company->fill($request->all());$company = $request->handleImages($company);@@ -323,4 +333,37 @@ class CompaniesController extends Controllerreturn (new SelectlistTransformer)->transformSelectlist(Helper::paginateCollectionOrAll($sorted));}++ /**+ * Fail-loud guard for GHSA-x9rj-mmg2-wcfv. Webhook configuration on+ * a company drives outbound HTTP POSTs on every subsequent asset+ * checkout / checkin, so the Livewire integration-settings form+ * gates it on isSuperUser(). This REST path must enforce the same+ * control: if the caller sent any of Company::GATED_WEBHOOK_FIELDS+ * and is not a superuser, fail with an error naming the fields that+ * were refused rather than silently stripping them (silent stripping+ * returned success and misrepresented what persisted, which was the+ * exact anti-pattern the User::GATED_AUTH_FIELDS fix called out).+ * Returns null when nothing to reject so callers can early-return+ * on the first truthy response.+ */+ private function rejectUnauthorizedWebhookFields(Request $request): ?JsonResponse+ {+ $requested = array_values(array_intersect(+ Company::GATED_WEBHOOK_FIELDS,+ array_keys($request->all())+ ));++ if (empty($requested) || auth()->user()->isSuperUser()) {+ return null;+ }++ return response()->json(Helper::formatStandardApiResponse(+ 'error',+ null,+ trans('admin/companies/message.webhook_fields_require_superuser', [+ 'fields' => implode(', ', $requested),+ ])+ ));+ }}diff --git a/app/Models/Company.php b/app/Models/Company.phpindex 7914666afe..54175617ce 100644--- a/app/Models/Company.php+++ b/app/Models/Company.php@@ -43,6 +43,21 @@ final class Company extends SnipeModelprotected $table = 'companies';+ /**+ * Fields that configure a per-company outbound webhook. These are the+ * same fields the Livewire IntegrationSettingsForm gates on+ * isSuperUser(), so the REST API must enforce the same control. Any+ * controller / importer / job that mass-assigns from user input needs+ * to reject the write when the user lacks superuser, rather than+ * silently dropping the fields.+ */+ public const GATED_WEBHOOK_FIELDS = [+ 'webhook_selected',+ 'webhook_endpoint',+ 'webhook_channel',+ 'webhook_botname',+ ];+// Declare the rules for the model validation. The ExternalUrl rule// for webhook_endpoint is layered on in getRules() below because PHP// rejects `new` in a property-default array initializer.diff --git a/resources/lang/en-US/admin/companies/message.php b/resources/lang/en-US/admin/companies/message.phpindex f61171a1b5..22541618cc 100644--- a/resources/lang/en-US/admin/companies/message.php+++ b/resources/lang/en-US/admin/companies/message.php@@ -19,4 +19,5 @@ return ['bulk_success' => 'Company deleted successfully.|:count companies were deleted successfully.','partial_success' => 'Company deleted successfully. See additional information below. | :count companies were deleted successfully. See additional information below.',],+ 'webhook_fields_require_superuser' => 'Webhook configuration fields (:fields) can only be modified by a superuser.',];diff --git a/tests/Feature/Companies/Api/WebhookEndpointSsrfTest.php b/tests/Feature/Companies/Api/WebhookEndpointSsrfTest.phpindex 54dfd6d885..18bf201ee2 100644--- a/tests/Feature/Companies/Api/WebhookEndpointSsrfTest.php+++ b/tests/Feature/Companies/Api/WebhookEndpointSsrfTest.php@@ -8,14 +8,18 @@ use PHPUnit\Framework\Attributes\DataProvider;use Tests\TestCase;/**- * Regression test for GHSA-x9rj-mmg2-wcfv. Before the fix the Company- * API's $company->fill($request->all()) mass-assigned webhook_endpoint- * without running the ExternalUrl SSRF rule, so any user with- * companies.create or companies.edit could point the per-company- * webhook at loopback / RFC1918 / cloud metadata and the server would- * POST to that URL on every subsequent asset checkout / checkin for- * the company. Fix layers webhook_* rules onto Company::$rules so- * ValidatingTrait fires the ExternalUrl check on every save surface.+ * Regression tests for GHSA-x9rj-mmg2-wcfv. Covers the two paired+ * controls that close the per-company webhook mass-assignment hole:+ *+ * 1. ExternalUrl rule layered onto Company::$rules via getRules().+ * Rejects loopback / RFC1918 / cloud metadata / non-HTTP schemes+ * at save time. These tests run AS A SUPERUSER so the request+ * clears the gate below and actually exercises the URL rule.+ *+ * 2. Authz gate in Api\CompaniesController that rejects any mass-+ * assign request from a non-superuser if the body touches any+ * Company::GATED_WEBHOOK_FIELDS. Mirrors the Livewire integration+ * settings form's superuser requirement.*/class WebhookEndpointSsrfTest extends TestCase{@@ -40,7 +44,7 @@ class WebhookEndpointSsrfTest extends TestCase#[DataProvider('internalTargetProvider')]public function test_api_store_rejects_internal_or_non_http_webhook_endpoint(string $endpoint): void{- $caller = User::factory()->createCompanies()->create();+ $caller = User::factory()->superuser()->create();$this->actingAsForApi($caller)->postJson(route('api.companies.store'), [@@ -57,7 +61,7 @@ class WebhookEndpointSsrfTest extends TestCasepublic function test_api_update_rejects_internal_or_non_http_webhook_endpoint(string $endpoint): void{$company = Company::factory()->create(['webhook_endpoint' => null]);- $caller = User::factory()->editCompanies()->create();+ $caller = User::factory()->superuser()->create();$this->actingAsForApi($caller)->patchJson(route('api.companies.update', $company), [@@ -70,9 +74,9 @@ class WebhookEndpointSsrfTest extends TestCase$this->assertSame(null, $company->refresh()->webhook_endpoint);}- public function test_api_accepts_a_public_https_webhook_endpoint(): void+ public function test_api_accepts_a_public_https_webhook_endpoint_from_superuser(): void{- $caller = User::factory()->createCompanies()->create();+ $caller = User::factory()->superuser()->create();$this->actingAsForApi($caller)->postJson(route('api.companies.store'), [@@ -90,7 +94,7 @@ class WebhookEndpointSsrfTest extends TestCasepublic function test_api_rejects_unknown_webhook_selected_value(): void{- $caller = User::factory()->createCompanies()->create();+ $caller = User::factory()->superuser()->create();$this->actingAsForApi($caller)->postJson(route('api.companies.store'), [@@ -103,18 +107,111 @@ class WebhookEndpointSsrfTest extends TestCase$this->assertDatabaseMissing('companies', ['name' => 'bogus webhook_selected']);}- public function test_api_accepts_null_webhook_endpoint_so_companies_without_webhooks_still_save(): void+ public function test_api_allows_non_superuser_to_create_company_with_no_webhook_fields(): void{- // Null webhook_endpoint is the common case (most companies do not- // run a webhook). The rule must not force a non-null value on- // every company write.+ // Companies without webhooks are the common case. The gate must+ // not block a routine create for a companies.create holder when+ // the body carries nothing in GATED_WEBHOOK_FIELDS.$caller = User::factory()->createCompanies()->create();$this->actingAsForApi($caller)->postJson(route('api.companies.store'), ['name' => 'no webhook',- 'webhook_endpoint' => null,])->assertStatusMessageIs('success');}++ /**+ * @return array<string, array{0: array<string, mixed>}>+ */+ public static function gatedWebhookPayloadProvider(): array+ {+ return [+ 'webhook_endpoint only' => [['webhook_endpoint' => 'https://hooks.slack.com/x']],+ 'webhook_selected only' => [['webhook_selected' => 'slack']],+ 'webhook_channel only' => [['webhook_channel' => '#general']],+ 'webhook_botname only' => [['webhook_botname' => 'bot']],+ 'all four fields' => [[+ 'webhook_endpoint' => 'https://hooks.slack.com/x',+ 'webhook_selected' => 'slack',+ 'webhook_channel' => '#general',+ 'webhook_botname' => 'bot',+ ]],+ ];+ }++ /**+ * @param array<string, mixed> $gatedFields+ */+ #[DataProvider('gatedWebhookPayloadProvider')]+ public function test_api_store_rejects_webhook_fields_from_non_superuser(array $gatedFields): void+ {+ $caller = User::factory()->createCompanies()->create();++ $this->actingAsForApi($caller)+ ->postJson(route('api.companies.store'), array_merge(+ ['name' => 'blocked webhook write '.bin2hex(random_bytes(4))],+ $gatedFields,+ ))+ ->assertStatusMessageIs('error')+ ->assertJsonPath('messages', fn (string $m) => str_contains($m, 'superuser'));++ // Verify the row never got created. A silent-strip implementation+ // would have persisted the Company with the gated fields nulled,+ // which this assertion catches.+ $this->assertDatabaseMissing('companies', ['name' => 'blocked webhook write']);+ }++ /**+ * @param array<string, mixed> $gatedFields+ */+ #[DataProvider('gatedWebhookPayloadProvider')]+ public function test_api_update_rejects_webhook_fields_from_non_superuser(array $gatedFields): void+ {+ $company = Company::factory()->create([+ 'webhook_endpoint' => null,+ 'webhook_selected' => null,+ 'webhook_channel' => null,+ 'webhook_botname' => null,+ ]);+ $caller = User::factory()->editCompanies()->create();++ $this->actingAsForApi($caller)+ ->patchJson(route('api.companies.update', $company), array_merge(+ ['name' => $company->name],+ $gatedFields,+ ))+ ->assertStatusMessageIs('error')+ ->assertJsonPath('messages', fn (string $m) => str_contains($m, 'superuser'));++ // Verify every gated field in the payload stayed null on the+ // persisted row. A silent-strip implementation would also pass+ // the status-message check above but would have landed a+ // partial update with the gated fields nulled.+ $refreshed = $company->refresh();+ foreach (array_keys($gatedFields) as $field) {+ $this->assertNull($refreshed->{$field}, "field {$field} should not have been written");+ }+ }++ public function test_api_update_allows_non_superuser_to_edit_non_webhook_fields(): void+ {+ // Non-superuser caller with companies.edit can still rename+ // a company, set its phone number, change tag_color. The gate+ // is specifically on GATED_WEBHOOK_FIELDS, not on the whole+ // update surface.+ $company = Company::factory()->create(['name' => 'before', 'phone' => '555-0000']);+ $caller = User::factory()->editCompanies()->create();++ $this->actingAsForApi($caller)+ ->patchJson(route('api.companies.update', $company), [+ 'name' => 'after',+ 'phone' => '555-9999',+ ])+ ->assertStatusMessageIs('success');++ $refreshed = $company->refresh();+ $this->assertSame('after', $refreshed->name);+ $this->assertSame('555-9999', $refreshed->phone);+ }}