Skip to content

A free open source IT asset/license management system

Fixed FD-56674 - move masked serials to constant PRODUCT_KEY_MASK

snipec4d3a95b630c
diff --git a/app/Http/Controllers/Api/LicensesController.php b/app/Http/Controllers/Api/LicensesController.php
index 06168fc955..19c4ccb4bb 100644
--- a/app/Http/Controllers/Api/LicensesController.php
+++ b/app/Http/Controllers/Api/LicensesController.php
@@ -20,6 +20,7 @@ use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Gate;
class LicensesController extends Controller
{
@@ -34,6 +35,12 @@ class LicensesController extends Controller
{
$this->authorize('view', License::class);
+ // Callers without viewKeys must not be able to filter or search on
+ // licenses.serial. The transformer masks the value in the response
+ // body, but a filter/search hit still leaks key existence through
+ // the row count and result presence.
+ $canViewKeys = Gate::allows('viewKeys', License::class);
+
$licenses = License::with('company', 'manufacturer', 'supplier', 'category', 'adminuser', 'licenseSeatsRelation', 'assignedCount')
->withCount([
'freeSeats as free_seats_count',
@@ -63,7 +70,7 @@ class LicensesController extends Controller
$licenses->where('licenses.name', '=', $request->input('name'));
}
- if ($request->filled('product_key')) {
+ if ($request->filled('product_key') && $canViewKeys) {
$licenses->where('licenses.serial', '=', $request->input('product_key'));
}
@@ -115,9 +122,14 @@ class LicensesController extends Controller
$licenses->whereNull('expiration_date');
}
- // This invokes the Searchable model trait and will handle input by search or by advanced search filter
+ // This invokes the Searchable model trait and will handle input by search or by advanced search filter.
+ // Callers without viewKeys go through TextSearchWithoutSerial, which strips
+ // licenses.serial out of the searchable attribute set for the duration of the call.
if ($request->filled('filter') || $request->filled('search')) {
- $licenses->TextSearch($request->input('filter') ? $request->input('filter') : $request->input('search'));
+ $searchTerm = $request->input('filter') ? $request->input('filter') : $request->input('search');
+ $canViewKeys
+ ? $licenses->TextSearch($searchTerm)
+ : $licenses->TextSearchWithoutSerial($searchTerm);
}
if ($request->input('deleted') == 'true') {
diff --git a/app/Http/Controllers/Licenses/LicensesController.php b/app/Http/Controllers/Licenses/LicensesController.php
index 080a3b94d6..b33fb017dc 100755
--- a/app/Http/Controllers/Licenses/LicensesController.php
+++ b/app/Http/Controllers/Licenses/LicensesController.php
@@ -12,6 +12,7 @@ use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Gate;
use League\Csv\EscapeFormula;
use Symfony\Component\HttpFoundation\StreamedResponse;
@@ -344,7 +345,9 @@ class LicensesController extends Controller
$this->disableDebugbar();
- $response = new StreamedResponse(function () {
+ $canViewKeys = Gate::allows('viewKeys', License::class);
+
+ $response = new StreamedResponse(function () use ($canViewKeys) {
// Open output stream
$handle = fopen('php://output', 'w');
$licenses = License::with('company',
@@ -358,7 +361,7 @@ class LicensesController extends Controller
}
$licenses = $licenses->orderBy('created_at', 'DESC');
Company::scopeCompanyables($licenses)
- ->chunk(500, function ($licenses) use ($handle) {
+ ->chunk(500, function ($licenses) use ($handle, $canViewKeys) {
$headers = [
// strtolower to prevent Excel from trying to open it as a SYLK file
strtolower(trans('general.id')),
@@ -399,7 +402,7 @@ class LicensesController extends Controller
$license->id,
$license->company ? $license->company->name : '',
$license->name,
- $license->serial,
+ $canViewKeys ? $license->serial : License::PRODUCT_KEY_MASK,
$license->purchase_date,
$license->purchase_cost,
$license->order_number,
diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.php
index 5995cca4cb..f14ec912bd 100644
--- a/app/Http/Transformers/AssetsTransformer.php
+++ b/app/Http/Transformers/AssetsTransformer.php
@@ -403,7 +403,7 @@ class AssetsTransformer
if (Gate::allows('viewKeys', $licenseseat->license)) {
$product_key = $licenseseat->license->serial ?? null;
} else {
- $product_key = '------------';
+ $product_key = License::PRODUCT_KEY_MASK;
}
$array = [
diff --git a/app/Http/Transformers/LicensesTransformer.php b/app/Http/Transformers/LicensesTransformer.php
index a4ba954cce..c7a30689fe 100644
--- a/app/Http/Transformers/LicensesTransformer.php
+++ b/app/Http/Transformers/LicensesTransformer.php
@@ -33,7 +33,7 @@ class LicensesTransformer
'name' => e($license->manufacturer->name),
'tag_color' => ($license->manufacturer->tag_color) ? e($license->manufacturer->tag_color) : null,
] : null,
- 'product_key' => (Gate::allows('viewKeys', $license)) ? e($license->serial) : '------------',
+ 'product_key' => (Gate::allows('viewKeys', $license)) ? e($license->serial) : License::PRODUCT_KEY_MASK,
'order_number' => ($license->order_number) ? e($license->order_number) : null,
'purchase_order' => ($license->purchase_order) ? e($license->purchase_order) : null,
'purchase_date' => Helper::getFormattedDateObject($license->purchase_date, 'date'),
diff --git a/app/Models/License.php b/app/Models/License.php
index 5ff57201c5..43f199e212 100755
--- a/app/Models/License.php
+++ b/app/Models/License.php
@@ -44,6 +44,13 @@ class License extends Depreciable
protected $table = 'licenses';
+ /**
+ * Placeholder emitted anywhere a license product key would appear for a
+ * caller who lacks the viewKeys gate. Kept as a single source of truth
+ * so the API transformer, CSV export, and any future surface stay in sync.
+ */
+ public const PRODUCT_KEY_MASK = '------------';
+
protected $casts = [
'purchase_date' => 'date',
'expiration_date' => 'date',
@@ -830,6 +837,28 @@ class License extends Depreciable
return $this->hasMany(LicenseSeat::class)->whereNull('assigned_to')->whereNull('deleted_at')->whereNull('asset_id');
}
+ /**
+ * TextSearch variant that removes `serial` from the searchable attribute
+ * set for the duration of the call. Used by the API index for callers
+ * who lack the `viewKeys` permission, so search / filter parameters
+ * cannot be used as an existence oracle against license product keys.
+ */
+ public function scopeTextSearchWithoutSerial($query, $search)
+ {
+ $original = $this->searchableAttributes;
+
+ $this->searchableAttributes = array_values(array_filter(
+ $original,
+ fn ($attribute) => $attribute !== 'serial'
+ ));
+
+ try {
+ return $query->TextSearch($search);
+ } finally {
+ $this->searchableAttributes = $original;
+ }
+ }
+
public function scopeActiveLicenses($query)
{
diff --git a/tests/Feature/Licenses/Api/LicenseIndexTest.php b/tests/Feature/Licenses/Api/LicenseIndexTest.php
index fda741b95e..61288f9b4f 100644
--- a/tests/Feature/Licenses/Api/LicenseIndexTest.php
+++ b/tests/Feature/Licenses/Api/LicenseIndexTest.php
@@ -127,4 +127,127 @@ class LicenseIndexTest extends TestCase
])
->assertJson(fn (AssertableJson $json) => $json->has('rows', 6)->etc());
}
+
+ public function test_product_key_filter_is_ignored_for_users_without_view_keys(): void
+ {
+ License::factory()->create(['name' => 'License A', 'serial' => 'KNOWN-KEY-VALUE-AAA']);
+ License::factory()->create(['name' => 'License B', 'serial' => 'KNOWN-KEY-VALUE-BBB']);
+ License::factory()->create(['name' => 'License C', 'serial' => 'KNOWN-KEY-VALUE-CCC']);
+
+ // A viewKeys-less caller filtering by product_key must NOT get a differential
+ // response between valid and invalid candidates, since that difference would
+ // itself be an oracle on key existence. The filter is silently dropped and
+ // the full unfiltered list is returned instead.
+ $viewOnly = User::factory()->viewLicenses()->create();
+
+ $this->actingAsForApi($viewOnly)
+ ->getJson(route('api.licenses.index', ['product_key' => 'KNOWN-KEY-VALUE-AAA']))
+ ->assertOk()
+ ->assertJson(fn (AssertableJson $json) => $json->where('total', 3)->etc());
+
+ $this->actingAsForApi($viewOnly)
+ ->getJson(route('api.licenses.index', ['product_key' => 'DEFINITELY-NOT-A-REAL-KEY']))
+ ->assertOk()
+ ->assertJson(fn (AssertableJson $json) => $json->where('total', 3)->etc());
+ }
+
+ public function test_product_key_filter_is_honored_for_users_with_view_keys(): void
+ {
+ License::factory()->create(['name' => 'License A', 'serial' => 'REAL-KEY-VALUE-AAA']);
+ License::factory()->create(['name' => 'License B', 'serial' => 'REAL-KEY-VALUE-BBB']);
+
+ $keyHolder = User::factory()->viewLicenses()->viewKeysLicenses()->create();
+
+ $this->actingAsForApi($keyHolder)
+ ->getJson(route('api.licenses.index', ['product_key' => 'REAL-KEY-VALUE-AAA']))
+ ->assertOk()
+ ->assertJson(fn (AssertableJson $json) => $json->where('total', 1)->etc());
+
+ $this->actingAsForApi($keyHolder)
+ ->getJson(route('api.licenses.index', ['product_key' => 'DEFINITELY-NOT-A-REAL-KEY']))
+ ->assertOk()
+ ->assertJson(fn (AssertableJson $json) => $json->where('total', 0)->etc());
+ }
+
+ public function test_search_does_not_match_on_serial_for_users_without_view_keys(): void
+ {
+ License::factory()->create(['name' => 'Alpha One', 'serial' => 'ORACLE-CANDIDATE-XYZ']);
+ License::factory()->create(['name' => 'Alpha Two', 'serial' => 'DIFFERENT-KEY-ABC']);
+ License::factory()->create(['name' => 'Beta One', 'serial' => 'BETA-KEY-DEF']);
+
+ $viewOnly = User::factory()->viewLicenses()->create();
+
+ // Searching for a value that only appears in the serial column must not
+ // return the row that owns that serial. Otherwise the presence of the row
+ // in the response is a positive oracle on key existence.
+ $this->actingAsForApi($viewOnly)
+ ->getJson(route('api.licenses.index', ['search' => 'ORACLE-CANDIDATE-XYZ']))
+ ->assertOk()
+ ->assertJson(fn (AssertableJson $json) => $json->where('total', 0)->etc());
+
+ // Searching for a name substring keeps working, so legitimate search behavior
+ // is unaffected by the serial suppression.
+ $this->actingAsForApi($viewOnly)
+ ->getJson(route('api.licenses.index', ['search' => 'Alpha']))
+ ->assertOk()
+ ->assertJson(fn (AssertableJson $json) => $json->where('total', 2)->etc());
+ }
+
+ public function test_structured_filter_on_serial_returns_no_rows_for_users_without_view_keys(): void
+ {
+ License::factory()->create(['name' => 'Alpha One', 'serial' => 'ORACLE-CANDIDATE-XYZ']);
+ License::factory()->create(['name' => 'Alpha Two', 'serial' => 'DIFFERENT-KEY-ABC']);
+
+ $viewOnly = User::factory()->viewLicenses()->create();
+
+ // Structured (JSON) advanced-search filter keyed on `serial`. When the caller
+ // can't viewKeys, the TextSearchWithoutSerial scope strips 'serial' from the
+ // searchable attribute set, so applySingleSearchFilter treats it as an
+ // unknown key and adds no where-clause. The full list comes back and the
+ // oracle collapses.
+ $this->actingAsForApi($viewOnly)
+ ->getJson(route('api.licenses.index', [
+ 'filter' => '{"serial":"ORACLE-CANDIDATE-XYZ"}',
+ ]))
+ ->assertOk()
+ ->assertJson(fn (AssertableJson $json) => $json->where('total', 2)->etc());
+ }
+
+ public function test_search_matches_on_serial_for_users_with_view_keys(): void
+ {
+ License::factory()->create(['name' => 'Alpha One', 'serial' => 'ORACLE-CANDIDATE-XYZ']);
+ License::factory()->create(['name' => 'Alpha Two', 'serial' => 'DIFFERENT-KEY-ABC']);
+
+ $keyHolder = User::factory()->viewLicenses()->viewKeysLicenses()->create();
+
+ $this->actingAsForApi($keyHolder)
+ ->getJson(route('api.licenses.index', ['search' => 'ORACLE-CANDIDATE-XYZ']))
+ ->assertOk()
+ ->assertJson(fn (AssertableJson $json) => $json->where('total', 1)->etc());
+ }
+
+ public function test_text_search_without_serial_scope_leaves_searchable_attributes_intact_after_the_call(): void
+ {
+ // Regression guard: TextSearchWithoutSerial mutates $searchableAttributes on
+ // the query's model instance before delegating to TextSearch. The try/finally
+ // block restores it. If the finally is ever dropped, subsequent queries on
+ // the same model instance would silently lose serial from their search set.
+ License::factory()->create(['name' => 'Restoration probe', 'serial' => 'STAYS-SEARCHABLE-AFTER']);
+
+ $keyHolder = User::factory()->viewLicenses()->viewKeysLicenses()->create();
+
+ $model = new License;
+ $before = $model->searchableAttributes;
+
+ License::query()->TextSearchWithoutSerial('probe')->get();
+
+ $this->assertSame($before, (new License)->searchableAttributes);
+
+ // And confirm the serial-based search still works post-scope for a caller
+ // who is allowed to see keys.
+ $this->actingAsForApi($keyHolder)
+ ->getJson(route('api.licenses.index', ['search' => 'STAYS-SEARCHABLE-AFTER']))
+ ->assertOk()
+ ->assertJson(fn (AssertableJson $json) => $json->where('total', 1)->etc());
+ }
}
diff --git a/tests/Feature/Licenses/Ui/ExportLicensesCsvTest.php b/tests/Feature/Licenses/Ui/ExportLicensesCsvTest.php
new file mode 100644
index 0000000000..cd8bcf7b59
--- /dev/null
+++ b/tests/Feature/Licenses/Ui/ExportLicensesCsvTest.php
@@ -0,0 +1,88 @@
+<?php
+
+namespace Tests\Feature\Licenses\Ui;
+
+use App\Models\License;
+use App\Models\User;
+use Tests\TestCase;
+
+class ExportLicensesCsvTest extends TestCase
+{
+ public function test_csv_export_masks_serial_for_users_without_view_keys_permission(): void
+ {
+ $license = License::factory()->create([
+ 'name' => 'Definitely a real license',
+ 'serial' => 'SECRET-PRODUCT-KEY-DO-NOT-LEAK',
+ ]);
+
+ $viewOnly = User::factory()->viewLicenses()->create();
+
+ $this->actingAs($viewOnly)
+ ->get(route('licenses.export'))
+ ->assertOk()
+ ->assertSeePairsInStreamedResponse([
+ 'Name' => $license->name,
+ // League CSV's EscapeFormula prepends a backtick to values starting with
+ // characters Excel would parse as formula triggers (=+-@). The mask itself
+ // is License::PRODUCT_KEY_MASK ("------------"), which trips the leading-`-`
+ // rule, so what actually lands in the CSV cell is "`------------".
+ 'Serial Number' => '`'.License::PRODUCT_KEY_MASK,
+ ])
+ ->assertDontSeeTextInStreamedResponse('SECRET-PRODUCT-KEY-DO-NOT-LEAK');
+ }
+
+ public function test_csv_export_emits_real_serial_for_users_with_view_keys_permission(): void
+ {
+ $license = License::factory()->create([
+ 'name' => 'Definitely a real license',
+ 'serial' => 'SECRET-PRODUCT-KEY-VISIBLE',
+ ]);
+
+ $keyHolder = User::factory()->viewLicenses()->viewKeysLicenses()->create();
+
+ $this->actingAs($keyHolder)
+ ->get(route('licenses.export'))
+ ->assertOk()
+ ->assertSeePairsInStreamedResponse([
+ 'Name' => $license->name,
+ 'Serial Number' => 'SECRET-PRODUCT-KEY-VISIBLE',
+ ]);
+ }
+
+ public function test_csv_export_emits_real_serial_for_users_with_edit_permission(): void
+ {
+ // viewKeys grants on any of licenses.keys, licenses.create, or licenses.edit,
+ // so this branch has to be pinned too. Otherwise a future refactor could
+ // regress `edit` users into the masked branch.
+ $license = License::factory()->create([
+ 'name' => 'Real license',
+ 'serial' => 'EDIT-USER-SHOULD-SEE-THIS',
+ ]);
+
+ $editor = User::factory()->viewLicenses()->editLicenses()->create();
+
+ $this->actingAs($editor)
+ ->get(route('licenses.export'))
+ ->assertOk()
+ ->assertSeePairsInStreamedResponse([
+ 'Name' => $license->name,
+ 'Serial Number' => 'EDIT-USER-SHOULD-SEE-THIS',
+ ]);
+ }
+
+ public function test_csv_export_emits_real_serial_for_superuser(): void
+ {
+ $license = License::factory()->create([
+ 'name' => 'Real license',
+ 'serial' => 'SUPERUSER-SHOULD-SEE-THIS',
+ ]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('licenses.export'))
+ ->assertOk()
+ ->assertSeePairsInStreamedResponse([
+ 'Name' => $license->name,
+ 'Serial Number' => 'SUPERUSER-SHOULD-SEE-THIS',
+ ]);
+ }
+}