Skip to content

A free open source IT asset/license management system

Use accessor for sidenav colors

snipeee679e1f4263
diff --git a/app/Http/Controllers/Api/StatuslabelsController.php b/app/Http/Controllers/Api/StatuslabelsController.php
index b951392553..ee4b900826 100644
--- a/app/Http/Controllers/Api/StatuslabelsController.php
+++ b/app/Http/Controllers/Api/StatuslabelsController.php
@@ -12,6 +12,7 @@ use App\Http\Transformers\StatuslabelsTransformer;
use App\Models\Asset;
use App\Models\Setting;
use App\Models\Statuslabel;
+use App\Rules\CssColor;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
@@ -101,6 +102,8 @@ class StatuslabelsController extends Controller
return response()->json(Helper::formatStandardApiResponse('error', null, ['type' => ['Status label type is required.']]));
}
+ $request->validate(['color' => ['nullable', new CssColor]]);
+
$statuslabel = new Statuslabel;
$statuslabel->fill($request->all());
@@ -165,6 +168,9 @@ class StatuslabelsController extends Controller
return response()->json(Helper::formatStandardApiResponse('error', null, 'Status label type is required.'));
}
+ // See color-validation comment in store() above.
+ $request->validate(['color' => ['nullable', new CssColor]]);
+
$statuslabel->fill($request->all());
$statusType = Statuslabel::getStatuslabelTypesForDB($request->input('type'));
diff --git a/app/Http/Controllers/StatuslabelsController.php b/app/Http/Controllers/StatuslabelsController.php
index 700679df30..6024c6b887 100755
--- a/app/Http/Controllers/StatuslabelsController.php
+++ b/app/Http/Controllers/StatuslabelsController.php
@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\Helpers\Helper;
use App\Models\Statuslabel;
+use App\Rules\CssColor;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
@@ -59,6 +60,8 @@ class StatuslabelsController extends Controller
return redirect()->back()->withInput()->withErrors(['statuslabel_types' => trans('validation.statuslabel_type')]);
}
+ $request->validate(['color' => ['nullable', new CssColor]]);
+
$statusType = Statuslabel::getStatuslabelTypesForDB($request->input('statuslabel_types'));
// Save the Statuslabel data
@@ -109,6 +112,9 @@ class StatuslabelsController extends Controller
return redirect()->back()->withInput()->withErrors(['statuslabel_types' => trans('validation.statuslabel_type')]);
}
+ // See color-validation comment in store() above.
+ $request->validate(['color' => ['nullable', new CssColor]]);
+
// Update the Statuslabel data
$statustype = Statuslabel::getStatuslabelTypesForDB($request->input('statuslabel_types'));
$statuslabel->name = $request->input('name');
diff --git a/app/Models/Statuslabel.php b/app/Models/Statuslabel.php
index d5fedc4dce..8799b3a0e1 100755
--- a/app/Models/Statuslabel.php
+++ b/app/Models/Statuslabel.php
@@ -6,6 +6,8 @@ use App\Http\Traits\UniqueUndeletedTrait;
use App\Models\Traits\Searchable;
use App\Presenters\Presentable;
use App\Presenters\StatusLabelPresenter;
+use App\Rules\CssColor;
+use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\SoftDeletes;
@@ -48,6 +50,16 @@ class Statuslabel extends SnipeModel
use Searchable;
+ /**
+ * Sanitize of the color column on read.
+ */
+ protected function color(): Attribute
+ {
+ return Attribute::make(
+ get: fn(?string $value) => CssColor::sanitize($value, ''),
+ );
+ }
+
/**
* Per-request memo of the ID lists used by Asset's status-driven scopes
* (RTD / Pending / Undeployable / Archived / NotArchived). Each scope
diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php
index d564becf07..435df6f06e 100644
--- a/database/factories/UserFactory.php
+++ b/database/factories/UserFactory.php
@@ -628,6 +628,16 @@ class UserFactory extends Factory
return $this->appendPermission(['statuslabels.delete' => '1']);
}
+ public function createStatusLabels()
+ {
+ return $this->appendPermission(['statuslabels.create' => '1']);
+ }
+
+ public function editStatusLabels()
+ {
+ return $this->appendPermission(['statuslabels.edit' => '1']);
+ }
+
public function deleteSuppliers()
{
return $this->appendPermission(['suppliers.delete' => '1']);
diff --git a/resources/views/layouts/default.blade.php b/resources/views/layouts/default.blade.php
index 104955a070..fcbba4613d 100644
--- a/resources/views/layouts/default.blade.php
+++ b/resources/views/layouts/default.blade.php
@@ -418,7 +418,7 @@
<li{!! (request()->is('statuslabels/'.$status_nav->id) ? ' class="active" aria-current="page"' : '') !!}>
<a href="{{ route('statuslabels.show', ['statuslabel' => $status_nav->id]) }}">
<i class="fas fa-circle text-grey fa-fw"
- aria-hidden="true"{!! ($status_nav->color!='' ? ' style="color: '.e($status_nav->color).'"' : '') !!}></i>
+ aria-hidden="true"{!! ($status_nav->color != '' ? ' style="color: '.e($status_nav->color).'"' : '') !!}></i>
{{ $status_nav->name }}
<span class="badge badge-secondary">{{ $status_nav->asset_count }}</span></a></li>
@endforeach
diff --git a/tests/Feature/StatusLabels/StatusLabelColorInjectionTest.php b/tests/Feature/StatusLabels/StatusLabelColorInjectionTest.php
new file mode 100644
index 0000000000..aa0008ad8d
--- /dev/null
+++ b/tests/Feature/StatusLabels/StatusLabelColorInjectionTest.php
@@ -0,0 +1,107 @@
+<?php
+
+namespace Tests\Feature\StatusLabels;
+
+use App\Models\Statuslabel;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Regression coverage for GHSA-xwf8-rj39-5q7q. Status label color was
+ * stored verbatim from request input and rendered into an inline
+ * `style="color: ..."` attribute in the sidebar, so a low-privilege
+ * user with statuslabels.create / statuslabels.edit could plant a CSS
+ * payload (full-viewport overlay, forced url(...) request) seen by
+ * every viewer holding assets.view, including superusers.
+ *
+ * The fix validates through App\Rules\CssColor on both the web and API
+ * write paths, and adds an Attribute-cast on the model that runs
+ * CssColor::sanitize() on read for defense-in-depth against pre-fix
+ * stored values.
+ */
+class StatusLabelColorInjectionTest extends TestCase
+{
+ public function test_web_store_rejects_a_css_injection_color(): void
+ {
+ $actor = User::factory()->createStatusLabels()->create();
+ $payload = 'red;position:fixed;top:0;left:0;width:100vw;height:100vh;background:url(//attacker.example/p.png)';
+
+ $this->actingAs($actor)
+ ->post(route('statuslabels.store'), [
+ 'name' => 'poisoned-web-'.uniqid(),
+ 'statuslabel_types' => 'deployable',
+ 'color' => $payload,
+ ])
+ ->assertSessionHasErrors('color');
+
+ $this->assertDatabaseMissing('status_labels', ['color' => $payload]);
+ }
+
+ public function test_web_update_rejects_a_css_injection_color(): void
+ {
+ $actor = User::factory()->editStatusLabels()->create();
+ $existing = Statuslabel::factory()->create(['color' => '#00ff00']);
+ $payload = 'red;background:url(//attacker.example/p.png)';
+
+ $this->actingAs($actor)
+ ->put(route('statuslabels.update', $existing->id), [
+ 'name' => $existing->name,
+ 'statuslabel_types' => 'deployable',
+ 'color' => $payload,
+ ])
+ ->assertSessionHasErrors('color');
+
+ $this->assertNotSame($payload, $existing->fresh()->getRawOriginal('color'));
+ }
+
+ public function test_api_store_rejects_a_css_injection_color(): void
+ {
+ $actor = User::factory()->createStatusLabels()->create();
+ $payload = 'red;position:fixed;background:url(//attacker.example/p.png)';
+
+ // Snipe-IT's Api handler catches ValidationException and
+ // returns HTTP 200 with an error-shaped body (see
+ // app/Exceptions/Handler.php:174-177), so the "rejected"
+ // signal here is `status: error` + a color error message,
+ // not an HTTP 422.
+ $this->actingAsForApi($actor)
+ ->postJson(route('api.statuslabels.store'), [
+ 'name' => 'poisoned-api-'.uniqid(),
+ 'type' => 'deployable',
+ 'color' => $payload,
+ ])
+ ->assertOk()
+ ->assertJson(['status' => 'error'])
+ ->assertJsonPath('messages.color', fn ($messages) => is_array($messages) && count($messages) > 0);
+
+ $this->assertDatabaseMissing('status_labels', ['color' => $payload]);
+ }
+
+ public function test_hex_rgb_and_hsl_colors_pass_validation(): void
+ {
+ $actor = User::factory()->createStatusLabels()->create();
+
+ foreach (['#3c8dbc', '#fff', 'rgb(1,2,3)', 'rgba(1,2,3,0.5)', 'hsl(120,50%,50%)'] as $ok) {
+ $this->actingAs($actor)
+ ->post(route('statuslabels.store'), [
+ 'name' => 'ok-color-'.uniqid(),
+ 'statuslabel_types' => 'deployable',
+ 'color' => $ok,
+ ])
+ ->assertSessionHasNoErrors();
+ }
+ }
+
+ public function test_pre_fix_stored_payload_is_sanitized_on_read(): void
+ {
+ // Bypass ValidatingTrait by inserting directly, simulating a
+ // row that predates the write-path guard.
+ $payload = 'red;position:fixed;background:url(//attacker.example/p.png)';
+ $label = Statuslabel::factory()->create();
+ \Illuminate\Support\Facades\DB::table('status_labels')
+ ->where('id', $label->id)
+ ->update(['color' => $payload]);
+
+ $this->assertSame('', $label->fresh()->color, 'CssColor::sanitize must strip a poisoned pre-fix value on read.');
+ }
+}