A free open source IT asset/license management system
More consistent responses from QR codes
snipe5a9649852ffe
diff --git a/app/Http/Controllers/QrCodeController.php b/app/Http/Controllers/QrCodeController.phpindex 9eafed9213..fd096dc069 100644--- a/app/Http/Controllers/QrCodeController.php+++ b/app/Http/Controllers/QrCodeController.php@@ -6,6 +6,7 @@ use App\Helpers\Helper;use App\Models\Setting;use Com\Tecnick\Barcode\Barcode;use Illuminate\Http\Response;+use Illuminate\Support\Facades\Gate;use Symfony\Component\HttpFoundation\BinaryFileResponse;class QrCodeController extends Controller@@ -30,19 +31,19 @@ class QrCodeController extends Controllerif ($settings->label2_2d_type === 'none') {return false;}++ $unavailable = fn() => abort(404, trans('general.generic_model_not_found', ['model' => trans('general.item')]));if (! array_key_exists($object_type, self::$map_show_route)) {- return $object_type.' is not a valid type.';+ $unavailable();}$object = parent::getMapObjectType()[$object_type]::withTrashed()->find($id);- if (! $object) {- return 'That item is invalid';+ if (!$object || !Gate::allows('view', $object)) {+ $unavailable();}- $this->authorize('view', $object);-$size = Helper::barcodeDimensions($settings->label2_2d_type);$qr_file = public_path().'/uploads/barcodes/qr-'.str_slug($object_type).'-'.str_slug($id).'.png';diff --git a/app/Services/PredefinedKitCheckoutService.php b/app/Services/PredefinedKitCheckoutService.phpindex 99e9d6585b..fe1b09162d 100644--- a/app/Services/PredefinedKitCheckoutService.php+++ b/app/Services/PredefinedKitCheckoutService.php@@ -251,9 +251,9 @@ class PredefinedKitCheckoutService// request gets skipped with an operator-visible error// so the audit log matches the persisted state.$locked = LicenseSeat::whereKey($licenseSeat->id)->lockForUpdate()->first();- if (!$locked || $locked->assigned_to !== null) {+ if (! $locked || $locked->assigned_to !== null) {$errors[] = trans('admin/kits/general.none_licenses', [- 'license' => $licenseSeat->license?->name ?? '?',+ 'license' => $licenseSeat->license->name ?? '?','qty' => 1,]);@@ -279,7 +279,7 @@ class PredefinedKitCheckoutService// capacity was already consumed by a racing request.$requestedQuantity = $consumable->pivot->quantity;$locked = Consumable::whereKey($consumable->id)->lockForUpdate()->first();- if (!$locked || $locked->numRemaining() < $requestedQuantity) {+ if (! $locked || $locked->numRemaining() < $requestedQuantity) {$errors[] = trans('admin/kits/general.none_consumables', ['consumable' => $consumable->name,'qty' => $requestedQuantity,@@ -299,7 +299,7 @@ class PredefinedKitCheckoutService// Same shape as the consumable branch above.$requestedQuantity = $accessory->pivot->quantity;$locked = Accessory::whereKey($accessory->id)->lockForUpdate()->first();- if (!$locked || $locked->numRemaining() < $requestedQuantity) {+ if (! $locked || $locked->numRemaining() < $requestedQuantity) {$errors[] = trans('admin/kits/general.none_accessory', ['accessory' => $accessory->name,'qty' => $requestedQuantity,diff --git a/phpstan-baseline.neon b/phpstan-baseline.neonindex 9b78f20913..84662c3691 100644--- a/phpstan-baseline.neon+++ b/phpstan-baseline.neon@@ -9588,6 +9588,18 @@ parameters:count: 2path: app/Providers/ValidationServiceProvider.php+ -+ message: '#^Access to an undefined property App\\Models\\LicenseSeat\:\:\$created_by\.$#'+ identifier: property.notFound+ count: 1+ path: app/Services/PredefinedKitCheckoutService.php++ -+ message: '#^Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\Relation\:\:attach\(\)\.$#'+ identifier: method.notFound+ count: 2+ path: app/Services/PredefinedKitCheckoutService.php+-message: '#^Call to function is_null\(\) with App\\Models\\User will always evaluate to false\.$#'identifier: function.impossibleTypediff --git a/routes/web.php b/routes/web.phpindex 4dc49ea118..731650ddbb 100644--- a/routes/web.php+++ b/routes/web.php@@ -923,6 +923,7 @@ Route::group(['middleware' => 'web'], function () {Route::get('{object_type}/{id}/qr_code',[QrCodeController::class, 'show'])->name('qr_code/common')+ ->middleware('auth')->where(['object_type' => 'accessories|assets|hardware|licenses|locations|models|companies|components|consumables|users']);/**diff --git a/tests/Feature/QrCode/QrCodeExistenceOracleTest.php b/tests/Feature/QrCode/QrCodeExistenceOracleTest.phpnew file mode 100644index 0000000000..5b6a36b9ce--- /dev/null+++ b/tests/Feature/QrCode/QrCodeExistenceOracleTest.php@@ -0,0 +1,68 @@+<?php++namespace Tests\Feature\QrCode;++use App\Models\Asset;+use App\Models\Setting;+use App\Models\User;+use Tests\TestCase;+++class QrCodeExistenceOracleTest extends TestCase+{+ protected function setUp(): void+ {+ parent::setUp();+ // QR generation is a no-op when label2_2d_type === 'none'.+ // Force a real value so the controller reaches the auth check+ // (rather than short-circuiting with `return false` on the+ // settings guard). The test never actually renders a barcode+ // because the auth / policy branches all abort before then.+ Setting::factory()->create(['label2_2d_type' => 'QRCODE']);+ }++ public function test_anonymous_request_redirects_to_login_for_both_existing_and_missing_ids(): void+ {+ $asset = Asset::factory()->create();++ // No differential possible if both requests are absorbed by+ // the `auth` middleware and produce identical redirects.+ $existing = $this->get(route('qr_code/common', ['object_type' => 'assets', 'id' => $asset->id]));+ $missing = $this->get(route('qr_code/common', ['object_type' => 'assets', 'id' => $asset->id + 999_999]));++ $existing->assertRedirect(route('login'));+ $missing->assertRedirect(route('login'));+ $this->assertSame($existing->status(), $missing->status(), 'Anonymous responses must not vary by whether the id exists.');+ }++ public function test_authenticated_but_unauthorized_user_gets_uniform_404_regardless_of_id_existence(): void+ {+ // Viewer with a fully empty permission blob, so the `view`+ // policy denies for every asset regardless of ownership.+ $viewer = User::factory()->create();+ $asset = Asset::factory()->create();++ $existing = $this->actingAs($viewer)->get(route('qr_code/common', ['object_type' => 'assets', 'id' => $asset->id]));+ $missing = $this->actingAs($viewer)->get(route('qr_code/common', ['object_type' => 'assets', 'id' => $asset->id + 999_999]));++ $existing->assertStatus(404);+ $missing->assertStatus(404);+ $this->assertSame(+ $existing->getContent(),+ $missing->getContent(),+ 'Existing-but-denied and missing responses must be byte-identical so no oracle exists.',+ );+ }++ public function test_unknown_object_type_returns_the_same_uniform_404(): void+ {+ // The route's regex-constrained `object_type` param would+ // normally 404 at the router for a garbage type. Pin the+ // behavior explicitly so a future route-regex loosening does+ // not silently re-expose an oracle via "unknown type" branch.+ $viewer = User::factory()->create();++ $bogus = $this->actingAs($viewer)->get('/nonsense/1/qr_code');+ $bogus->assertStatus(404);+ }+}