Skip to content

A free open source IT asset/license management system

Avoid searching by human readable custom field name to avoid collisions with normal attributes

snipef9e16e16d1d7
diff --git a/app/Models/Traits/Searchable.php b/app/Models/Traits/Searchable.php
index 6b7bb08807..da74e66161 100644
--- a/app/Models/Traits/Searchable.php
+++ b/app/Models/Traits/Searchable.php
@@ -195,8 +195,8 @@ trait Searchable
}
// Check if this is a custom field (only for Assets - for *now*).
- // Accepts both the human-readable field name (e.g. "CPU") and the raw
- // db_column slug (e.g. "_snipeit_cpu_4") as filter keys.
+ // Only db_column keys (e.g. "_snipeit_cpu_4") are accepted to avoid
+ // collisions with standard attributes or relation filter keys.
if ($this instanceof Asset) {
$dbColumn = $this->resolveCustomFieldDbColumn($filterKey);
@@ -747,8 +747,7 @@ trait Searchable
/**
* Resolve a filter key to the actual database column name for a custom field.
*
- * Accepts both human-readable field names (e.g. "CPU", "cpu") and raw
- * db_column slugs (e.g. "_snipeit_cpu_4") as filter keys.
+ * Accepts only raw db_column slugs (e.g. "_snipeit_cpu_4") as filter keys.
*
* Returns null when the key cannot be matched to any known custom field.
*
@@ -762,27 +761,15 @@ trait Searchable
$map = $this->buildCustomFieldFilterMap();
- // 1. Exact match on db_column (e.g. "_snipeit_cpu_4")
- if (array_key_exists($filterKey, $map)) {
- return $map[$filterKey];
- }
-
- // 2. Case-insensitive match on human-readable field name (e.g. "CPU", "cpu")
- $lowerKey = strtolower($filterKey);
-
- if (array_key_exists($lowerKey, $map)) {
- return $map[$lowerKey];
- }
-
- return null;
+ // Exact match on db_column (e.g. "_snipeit_cpu_4") only.
+ return $map[$filterKey] ?? null;
}
/**
* Build a lookup map for custom field filter resolution.
*
- * The returned array has two types of entries for every custom field:
- * - db_column (exact) → db_column e.g. "_snipeit_cpu_4" => "_snipeit_cpu_4"
- * - lowercase name → db_column e.g. "cpu" => "_snipeit_cpu_4"
+ * The returned array contains db_column entries only:
+ * - db_column (exact) → db_column, e.g. "_snipeit_cpu_4" => "_snipeit_cpu_4"
*
* Results are cached statically for the duration of the request.
* Call flushCustomFieldFilterMap() to reset the cache (useful in tests).
@@ -801,15 +788,12 @@ trait Searchable
CustomField::query()
->whereNotNull('db_column')
->where('field_encrypted', 0)
- ->get(['name', 'db_column'])
+ ->get(['db_column'])
->each(function (CustomField $field) use (&$map): void {
$dbColumn = $field->db_column;
// Exact db_column key (e.g. "_snipeit_cpu_4")
$map[$dbColumn] = $dbColumn;
-
- // Lowercase human-readable name key (e.g. "cpu")
- $map[strtolower($field->name)] = $dbColumn;
});
} catch (\Exception $e) {
// Guard against missing table or schema issues during migrations / tests
diff --git a/tests/Feature/Search/SearchableTraitTest.php b/tests/Feature/Search/SearchableTraitTest.php
index d861998658..d47d5ff979 100644
--- a/tests/Feature/Search/SearchableTraitTest.php
+++ b/tests/Feature/Search/SearchableTraitTest.php
@@ -521,12 +521,9 @@ class SearchableTraitTest extends TestCase
}
/**
- * Test filtering on a custom field by its human-readable name (e.g. "CPU").
- *
- * The filter key should be case-insensitive and should map to the underlying
- * db_column (e.g. "_snipeit_cpu_N") automatically.
+ * Test filtering on a custom field using the raw db_column slug.
*/
- public function test_custom_field_filter_by_human_readable_name()
+ public function test_custom_field_filter_by_db_column_slug()
{
$field = CustomField::factory()->cpu()->create();
$dbColumn = $field->db_column_name();
@@ -538,19 +535,19 @@ class SearchableTraitTest extends TestCase
// Flush cache so the newly created field is picked up.
Asset::flushCustomFieldFilterMap();
- // Filter using the human-readable field name "CPU" (exact case)
+ // Filter using the raw db_column key.
$this->actingAsForApi(User::factory()->viewAssets()->create())
->getJson(route('api.assets.index', [
- 'filter' => json_encode(['CPU' => '3.2GHz']),
+ 'filter' => json_encode([$dbColumn => '3.2GHz']),
]))
->assertOk()
->assertJson(fn (AssertableJson $json) => $json->has('rows', 1)->etc());
}
/**
- * Test filtering on a custom field using a lowercase name.
+ * Test filtering by a human-readable custom field name is ignored.
*/
- public function test_custom_field_filter_by_lowercase_name()
+ public function test_custom_field_filter_by_human_readable_name_is_ignored()
{
$field = CustomField::factory()->cpu()->create();
$dbColumn = $field->db_column_name();
@@ -562,30 +559,44 @@ class SearchableTraitTest extends TestCase
$this->actingAsForApi(User::factory()->viewAssets()->create())
->getJson(route('api.assets.index', [
- 'filter' => json_encode(['cpu' => 'i9']),
+ 'filter' => json_encode(['CPU' => 'i9']),
]))
->assertOk()
- ->assertJson(fn (AssertableJson $json) => $json->has('rows', 1)->etc());
+ // Human-readable custom field keys are intentionally ignored.
+ ->assertJson(fn (AssertableJson $json) => $json->has('rows', 2)->etc());
}
/**
- * Test filtering on a custom field using the raw db_column slug.
+ * Test custom field name collisions do not override relation filters.
*/
- public function test_custom_field_filter_by_db_column_slug()
+ public function test_custom_field_name_collision_does_not_override_relation_filter()
{
- $field = CustomField::factory()->cpu()->create();
+ $status = Statuslabel::factory()->create(['name' => 'CollisionStatus-'.now()->timestamp]);
+ $otherStatus = Statuslabel::factory()->create(['name' => 'DifferentStatus-'.now()->timestamp]);
+
+ $field = CustomField::factory()->create([
+ 'name' => 'status',
+ 'field_encrypted' => 0,
+ ]);
$dbColumn = $field->db_column_name();
- Asset::factory()->create([$dbColumn => '3.2GHz i9']);
- Asset::factory()->create([$dbColumn => '2.4GHz i5']);
+ Asset::factory()->create([
+ 'status_id' => $status->id,
+ $dbColumn => 'custom-status-value',
+ ]);
+ Asset::factory()->create([
+ 'status_id' => $otherStatus->id,
+ $dbColumn => 'CollisionStatus',
+ ]);
Asset::flushCustomFieldFilterMap();
$this->actingAsForApi(User::factory()->viewAssets()->create())
->getJson(route('api.assets.index', [
- 'filter' => json_encode([$dbColumn => 'i9']),
+ 'filter' => json_encode(['status' => 'CollisionStatus']),
]))
->assertOk()
+ // This must filter the status relation, not the custom field with same name.
->assertJson(fn (AssertableJson $json) => $json->has('rows', 1)->etc());
}
@@ -647,7 +658,7 @@ class SearchableTraitTest extends TestCase
$this->actingAsForApi(User::factory()->viewAssets()->create())
->getJson(route('api.assets.index', [
- 'filter' => json_encode(['CPU' => 'Intel']),
+ 'filter' => json_encode([$dbColumn => 'Intel']),
]))
->assertOk()
->assertJson(fn (AssertableJson $json) => $json->has('rows', 1)->etc());