Skip to content

A free open source IT asset/license management system

Fixed x9rj-mmg2 - tighten guards on per-company webhooks

snipe6610c17a7154
diff --git a/app/Models/Company.php b/app/Models/Company.php
index 85d3d5eb38..7914666afe 100644
--- a/app/Models/Company.php
+++ b/app/Models/Company.php
@@ -11,6 +11,7 @@ use App\Models\Traits\Loggable;
use App\Models\Traits\Searchable;
use App\Presenters\CompanyPresenter;
use App\Presenters\Presentable;
+use App\Rules\ExternalUrl;
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -28,6 +29,7 @@ use Watson\Validating\ValidatingTrait;
* Model for Companies.
*
* @version v1.8
+ *
* @mixin IdeHelperCompany
*/
final class Company extends SnipeModel
@@ -41,15 +43,31 @@ final class Company extends SnipeModel
protected $table = 'companies';
- // Declare the rules for the model validation
+ // 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.
protected $rules = [
'name' => 'required|max:255|unique_undeleted_in_scope:parent_id',
'fax' => 'min:7|max:35|nullable',
'phone' => 'min:7|max:35|nullable',
'email' => 'email|max:150|nullable',
'parent_id' => 'nullable|integer|exists:companies,id|parent_must_be_top_level:companies,id|must_have_no_children:companies,id|parent_within_scope',
+ 'webhook_selected' => 'nullable|string|in:slack,general,google,microsoft',
+ 'webhook_channel' => 'nullable|string',
+ 'webhook_botname' => 'nullable|string|max:255',
];
+ /**
+ * PHP 8.2 doesn't allow `new` in a property default, so the ExternalUrl rule is added on here.
+ */
+ public function getRules()
+ {
+ $rules = $this->rules;
+ $rules['webhook_endpoint'] = ['nullable', 'url', new ExternalUrl('app.webhook_allow_internal_targets')];
+
+ return $rules;
+ }
+
protected $casts = [
'parent_id' => 'integer',
];
diff --git a/tests/Feature/Companies/Api/WebhookEndpointSsrfTest.php b/tests/Feature/Companies/Api/WebhookEndpointSsrfTest.php
new file mode 100644
index 0000000000..54dfd6d885
--- /dev/null
+++ b/tests/Feature/Companies/Api/WebhookEndpointSsrfTest.php
@@ -0,0 +1,120 @@
+<?php
+
+namespace Tests\Feature\Companies\Api;
+
+use App\Models\Company;
+use App\Models\User;
+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.
+ */
+class WebhookEndpointSsrfTest extends TestCase
+{
+ /**
+ * @return array<string, array{0: string}>
+ */
+ public static function internalTargetProvider(): array
+ {
+ return [
+ 'loopback v4' => ['http://127.0.0.1/attacker'],
+ 'loopback v6' => ['http://[::1]/attacker'],
+ 'aws imds' => ['http://169.254.169.254/latest/meta-data/'],
+ 'gcp metadata' => ['http://metadata.google.internal/computeMetadata/v1/'],
+ 'rfc1918 10' => ['http://10.0.0.5/attacker'],
+ 'rfc1918 172' => ['http://172.16.0.1/attacker'],
+ 'rfc1918 192' => ['http://192.168.1.1/attacker'],
+ 'non-http scheme ftp' => ['ftp://example.com/attacker'],
+ 'non-http scheme gopher' => ['gopher://example.com/attacker'],
+ ];
+ }
+
+ #[DataProvider('internalTargetProvider')]
+ public function test_api_store_rejects_internal_or_non_http_webhook_endpoint(string $endpoint): void
+ {
+ $caller = User::factory()->createCompanies()->create();
+
+ $this->actingAsForApi($caller)
+ ->postJson(route('api.companies.store'), [
+ 'name' => 'SSRF test '.bin2hex(random_bytes(4)),
+ 'webhook_selected' => 'slack',
+ 'webhook_endpoint' => $endpoint,
+ ])
+ ->assertStatusMessageIs('error');
+
+ $this->assertDatabaseMissing('companies', ['webhook_endpoint' => $endpoint]);
+ }
+
+ #[DataProvider('internalTargetProvider')]
+ public 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();
+
+ $this->actingAsForApi($caller)
+ ->patchJson(route('api.companies.update', $company), [
+ 'name' => $company->name,
+ 'webhook_selected' => 'slack',
+ 'webhook_endpoint' => $endpoint,
+ ])
+ ->assertStatusMessageIs('error');
+
+ $this->assertSame(null, $company->refresh()->webhook_endpoint);
+ }
+
+ public function test_api_accepts_a_public_https_webhook_endpoint(): void
+ {
+ $caller = User::factory()->createCompanies()->create();
+
+ $this->actingAsForApi($caller)
+ ->postJson(route('api.companies.store'), [
+ 'name' => 'legit webhook',
+ 'webhook_selected' => 'slack',
+ 'webhook_endpoint' => 'https://hooks.slack.com/services/T000/B000/XXXXX',
+ ])
+ ->assertStatusMessageIs('success');
+
+ $this->assertDatabaseHas('companies', [
+ 'name' => 'legit webhook',
+ 'webhook_endpoint' => 'https://hooks.slack.com/services/T000/B000/XXXXX',
+ ]);
+ }
+
+ public function test_api_rejects_unknown_webhook_selected_value(): void
+ {
+ $caller = User::factory()->createCompanies()->create();
+
+ $this->actingAsForApi($caller)
+ ->postJson(route('api.companies.store'), [
+ 'name' => 'bogus webhook_selected',
+ 'webhook_selected' => 'definitely-not-a-real-webhook-type',
+ 'webhook_endpoint' => 'https://hooks.slack.com/services/T000/B000/XXXXX',
+ ])
+ ->assertStatusMessageIs('error');
+
+ $this->assertDatabaseMissing('companies', ['name' => 'bogus webhook_selected']);
+ }
+
+ public function test_api_accepts_null_webhook_endpoint_so_companies_without_webhooks_still_save(): 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.
+ $caller = User::factory()->createCompanies()->create();
+
+ $this->actingAsForApi($caller)
+ ->postJson(route('api.companies.store'), [
+ 'name' => 'no webhook',
+ 'webhook_endpoint' => null,
+ ])
+ ->assertStatusMessageIs('success');
+ }
+}