Skip to content

A free open source IT asset/license management system

Use interface for Importable types

snipefb38a2173494
diff --git a/app/Livewire/Importer.php b/app/Livewire/Importer.php
index aba9eb4448..a04b688366 100644
--- a/app/Livewire/Importer.php
+++ b/app/Livewire/Importer.php
@@ -12,6 +12,7 @@ use App\Models\Component as ComponentModel;
use App\Models\Consumable;
use App\Models\CustomField;
use App\Models\Import;
+use App\Models\Importable;
use App\Models\License;
use App\Models\Location;
use App\Models\Manufacturer;
@@ -21,6 +22,7 @@ use App\Models\User;
use Illuminate\Support\Facades\Storage;
use League\Csv\Reader;
use Livewire\Attributes\Computed;
+use Livewire\Attributes\Locked;
use Livewire\Component;
use Livewire\WithPagination;
@@ -163,6 +165,10 @@ class Importer extends Component
// / components / licenses use "item_name" for their name column;
// asset models / locations / suppliers / manufacturers / categories
// use "name" directly.
+ // requiredForType() cross-checks each class string against the
+ // Importable marker interface before `new`-ing. Defense in depth
+ // against future Livewire regressions that might bypass Locked.
+ #[Locked]
public $required_field_model_map = [
'asset' => [Asset::class, ['item_name' => 'name']],
'assetModel' => [AssetModel::class, ['name' => 'name']],
@@ -176,7 +182,7 @@ class Importer extends Component
// (2) update an existing License's capacity, (3) assign a user or
// asset to one of an existing License's free seats. The importer
// figures out which path each row takes at process time. seats is
- // only needed for (1) and (2); (3) doesn't reference it. Since the
+ // only needed for (1) and (2), (3) doesn't reference it. Since the
// wizard can't know per-row which path a caller intends, we don't
// enforce seats at the wizard level. Server-side validation still
// enforces `seats` on the create path per License::$rules. See
@@ -1080,7 +1086,17 @@ class Importer extends Component
}
[$modelClass, $translation] = $this->required_field_model_map[$type];
- $rules = (new $modelClass)->getRules() ?? [];
+
+ // Defense in depth for GHSA-r43j-45c3-9vvw. #[Locked] on the
+ // property above should already reject client writes, but if a
+ // Livewire regression ever bypasses Locked, this interface check
+ // keeps the `new $modelClass` dispatch bounded to the set of
+ // models that opt in by implementing the Importable marker.
+ if (! is_string($modelClass) || ! class_exists($modelClass) || ! is_subclass_of($modelClass, Importable::class)) {
+ return [];
+ }
+
+ $rules = (new $modelClass)->getRules();
$required = [];
foreach ($translation as $importerKey => $modelAttr) {
diff --git a/app/Models/Accessory.php b/app/Models/Accessory.php
index 40aec318b3..631ad1f3dc 100755
--- a/app/Models/Accessory.php
+++ b/app/Models/Accessory.php
@@ -29,9 +29,10 @@ use Watson\Validating\ValidatingTrait;
* Model for Accessories.
*
* @version v1.0
+ *
* @mixin IdeHelperAccessory
*/
-class Accessory extends SnipeModel
+class Accessory extends SnipeModel implements Importable
{
use Acceptable;
use AdjustsQuantity;
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index acc0a18814..967b061544 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -36,6 +36,7 @@ use Watson\Validating\ValidatingTrait;
* Model for Assets.
*
* @version v1.0
+ *
* @property ?int $location_id
* @property \Carbon\Carbon|string|null $next_audit_date
* @property \Carbon\Carbon|string|null $last_audit_date
@@ -44,9 +45,10 @@ use Watson\Validating\ValidatingTrait;
* @property \Carbon\Carbon|string|null $last_checkin
* @property bool $requestable
* @property Asset|Location|User|null $assigned Current checkout target. morphTo relation named `assigned` (declared on method `assignedTo()`). Eloquent resolves the dynamic property by the morphTo name arg, not the method name, so this `@property` is distinct from `@property ... $assignedTo` written by ide-helper.
+ *
* @mixin IdeHelperAsset
*/
-class Asset extends Depreciable
+class Asset extends Depreciable implements Importable
{
protected $presenter = AssetPresenter::class;
diff --git a/app/Models/AssetModel.php b/app/Models/AssetModel.php
index ee72331e56..3a90011466 100755
--- a/app/Models/AssetModel.php
+++ b/app/Models/AssetModel.php
@@ -25,9 +25,10 @@ use Watson\Validating\ValidatingTrait;
* attributes that are common among the same type of asset.
*
* @version v1.0
+ *
* @mixin IdeHelperAssetModel
*/
-class AssetModel extends SnipeModel
+class AssetModel extends SnipeModel implements Importable
{
use HasFactory;
use HasImageUpload;
diff --git a/app/Models/Category.php b/app/Models/Category.php
index f2241514a2..cb907f02c7 100755
--- a/app/Models/Category.php
+++ b/app/Models/Category.php
@@ -24,9 +24,10 @@ use Watson\Validating\ValidatingTrait;
* send a EULA to the user, etc.
*
* @version v1.0
+ *
* @mixin IdeHelperCategory
*/
-class Category extends SnipeModel
+class Category extends SnipeModel implements Importable
{
use HasFactory;
diff --git a/app/Models/Component.php b/app/Models/Component.php
index 15d85126d4..c2110c38ef 100644
--- a/app/Models/Component.php
+++ b/app/Models/Component.php
@@ -31,9 +31,10 @@ use Watson\Validating\ValidatingTrait;
* Model for Components.
*
* @version v1.0
+ *
* @mixin IdeHelperComponent
*/
-class Component extends SnipeModel
+class Component extends SnipeModel implements Importable
{
use HasFactory;
diff --git a/app/Models/Consumable.php b/app/Models/Consumable.php
index c65ed7162f..fbe25b051d 100644
--- a/app/Models/Consumable.php
+++ b/app/Models/Consumable.php
@@ -28,7 +28,7 @@ use Watson\Validating\ValidatingTrait;
/**
* @mixin IdeHelperConsumable
*/
-class Consumable extends SnipeModel
+class Consumable extends SnipeModel implements Importable
{
use HasFactory;
diff --git a/app/Models/Importable.php b/app/Models/Importable.php
new file mode 100644
index 0000000000..530661abd8
--- /dev/null
+++ b/app/Models/Importable.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace App\Models;
+
+/**
+ * Marker interface for models the Importer wizard knows how to import.
+ *
+ * The Livewire Importer at app/Livewire/Importer.php cross-checks every
+ * class string from `$required_field_model_map` against this interface
+ * before `new $modelClass`. The property itself is `#[Locked]`, so the
+ * Livewire hydration layer already rejects client writes, but this
+ * interface check is defense in depth. Even if a future Livewire
+ * regression bypasses Locked, the `new` dispatch stays bounded to the
+ * exact set of models that opt in by implementing this interface.
+ *
+ * No methods. The models that implement it already carry everything the
+ * importer needs via their own `$rules` and attribute columns. Adding
+ * this interface to a new model is the deliberate signal that "yes, the
+ * importer wizard may instantiate this." Introduced with GHSA-r43j-45c3
+ * -9vvw.
+ */
+interface Importable
+{
+ /**
+ * The validation-rule array the importer walks to decide which
+ * target columns are required at the mapping-wizard step. Every
+ * Snipe-IT model that implements this interface already exposes
+ * `getRules()` via Watson's ValidatingTrait, so declaring it here
+ * is just codifying the existing contract so static analysis can
+ * see the shape instead of resolving the call through the trait.
+ *
+ * @return array<string, mixed>
+ */
+ public function getRules();
+}
diff --git a/app/Models/License.php b/app/Models/License.php
index babf78dced..c04d4ba535 100755
--- a/app/Models/License.php
+++ b/app/Models/License.php
@@ -26,7 +26,7 @@ use Watson\Validating\ValidatingTrait;
/**
* @mixin IdeHelperLicense
*/
-class License extends Depreciable
+class License extends Depreciable implements Importable
{
use HasFactory;
diff --git a/app/Models/Location.php b/app/Models/Location.php
index a51ed9d0f4..53166dfd76 100755
--- a/app/Models/Location.php
+++ b/app/Models/Location.php
@@ -22,7 +22,7 @@ use Watson\Validating\ValidatingTrait;
/**
* @mixin IdeHelperLocation
*/
-class Location extends SnipeModel
+class Location extends SnipeModel implements Importable
{
use CompanyableTrait;
use HasFactory;
diff --git a/app/Models/Manufacturer.php b/app/Models/Manufacturer.php
index b8ca866079..75bae9f175 100755
--- a/app/Models/Manufacturer.php
+++ b/app/Models/Manufacturer.php
@@ -17,7 +17,7 @@ use Watson\Validating\ValidatingTrait;
/**
* @mixin IdeHelperManufacturer
*/
-class Manufacturer extends SnipeModel
+class Manufacturer extends SnipeModel implements Importable
{
use HasFactory;
diff --git a/app/Models/Supplier.php b/app/Models/Supplier.php
index f14b894d69..e622c5d93f 100755
--- a/app/Models/Supplier.php
+++ b/app/Models/Supplier.php
@@ -21,7 +21,7 @@ use Watson\Validating\ValidatingTrait;
/**
* @mixin IdeHelperSupplier
*/
-class Supplier extends SnipeModel
+class Supplier extends SnipeModel implements Importable
{
use HasFactory;
use HasUploads;
diff --git a/app/Models/User.php b/app/Models/User.php
index 5f376476ab..a749e00e0d 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -40,7 +40,7 @@ use Watson\Validating\ValidatingTrait;
/**
* @mixin IdeHelperUser
*/
-class User extends SnipeModel implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract, HasLocalePreference
+class User extends SnipeModel implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract, HasLocalePreference, Importable
{
use CompanyableTrait;
use HasCalendarEvents;
diff --git a/tests/Feature/Livewire/ImporterLockedPropertyTest.php b/tests/Feature/Livewire/ImporterLockedPropertyTest.php
new file mode 100644
index 0000000000..131f32fafd
--- /dev/null
+++ b/tests/Feature/Livewire/ImporterLockedPropertyTest.php
@@ -0,0 +1,118 @@
+<?php
+
+namespace Tests\Feature\Livewire;
+
+use App\Livewire\Importer;
+use App\Models\Asset;
+use App\Models\User;
+use Livewire\Features\SupportLockedProperties\CannotUpdateLockedPropertyException;
+use Livewire\Livewire;
+use Tests\TestCase;
+
+class ImporterLockedPropertyTest extends TestCase
+{
+ public function test_livewire_rejects_client_writes_to_required_field_model_map(): void
+ {
+ // Livewire's #[Locked] attribute throws
+ // CannotUpdateLockedPropertyException on any update() path (which
+ // is what the Livewire test helper's set() method goes through).
+ // A pre-fix component without the attribute would silently accept
+ // the write and expose the sink.
+ $this->expectException(CannotUpdateLockedPropertyException::class);
+
+ Livewire::actingAs(User::factory()->canImport()->create())
+ ->test(Importer::class)
+ ->set('required_field_model_map', [
+ 'asset' => ['Pwn\\DoesNotExist', ['item_name' => 'name']],
+ ]);
+ }
+
+ public function test_required_for_type_rejects_a_class_that_does_not_implement_importable(): void
+ {
+ // Even if a Livewire regression ever bypasses #[Locked],
+ // the Importable marker-interface check in
+ // requiredForType() must still refuse to dispatch into a class
+ // that has not opted in.
+ $component = new Importer;
+
+ $component->required_field_model_map = [
+ 'asset' => [\Illuminate\Support\Carbon::class, ['item_name' => 'name']],
+ ];
+
+ $this->assertSame([], $component->requiredForType('asset'));
+ }
+
+ public function test_required_for_type_rejects_a_class_that_does_not_exist(): void
+ {
+ // Separate case: non-existent class names must not reach `new`.
+ // The pre-fix behavior would throw Error: Class not found. Post-
+ // fix, the method short-circuits to [] because class_exists() is
+ // false.
+ $component = new Importer;
+
+ $component->required_field_model_map = [
+ 'asset' => ['Pwn\\DoesNotExist', ['item_name' => 'name']],
+ ];
+
+ $this->assertSame([], $component->requiredForType('asset'));
+ }
+
+ public function test_required_for_type_still_works_for_a_whitelisted_class(): void
+ {
+ // Positive-path: the pre-fix behavior for a legitimate type still
+ // works. Location is in ALLOWED_IMPORT_MODEL_CLASSES, so the
+ // method walks Location::getRules() and returns the "name"
+ // importer key because Location::$rules marks name as required.
+ // (Asset is in the allowlist too but Asset::$rules["name"] is
+ // nullable, so it would not surface in the required list.)
+ $component = new Importer;
+
+ $required = $component->requiredForType('location');
+
+ $this->assertContains('name', $required);
+ }
+
+ public function test_required_for_type_rejects_unknown_types_without_dispatch(): void
+ {
+ // Unknown $type keys fall through to the empty-array path before
+ // the allowlist check, so an attacker who can only influence the
+ // scalar $type (not the map) also gets a safe no-op.
+ $component = new Importer;
+
+ $this->assertSame([], $component->requiredForType('nonexistent-type'));
+ $this->assertSame([], $component->requiredForType(''));
+ $this->assertSame([], $component->requiredForType(null));
+ }
+
+ public function test_asset_history_short_circuit_bypasses_the_dispatch_entirely(): void
+ {
+ // assetHistory has no backing model, so requiredForType returns
+ // its hardcoded required-column list before the class-dispatch
+ // branch runs. Confirms the existing short-circuit is intact.
+ $component = new Importer;
+
+ $this->assertSame(
+ ['asset_tag', 'full_name', 'checkout_date'],
+ $component->requiredForType('assetHistory')
+ );
+ }
+
+ public function test_every_class_in_the_map_implements_importable(): void
+ {
+ // Every class pointer in $required_field_model_map
+ // must implement the Importable marker interface, or the
+ // defense-in-depth check in requiredForType() will refuse the
+ // dispatch for a legitimate type. Catches the case where someone
+ // adds a new import type to the map without also adding
+ // `implements Importable` to the model class.
+ $component = new Importer;
+
+ foreach ($component->required_field_model_map as $type => $entry) {
+ [$modelClass] = $entry;
+ $this->assertTrue(
+ is_subclass_of($modelClass, \App\Models\Importable::class),
+ "map entry for type '{$type}' points at '{$modelClass}' which does not implement \\App\\Models\\Importable"
+ );
+ }
+ }
+}