Skip to content

A free open source IT asset/license management system

Fixed gmvp-6qh3 - disallow custom CSS property declarations

snipe526382d2d3ab
diff --git a/app/Models/Setting.php b/app/Models/Setting.php
index de2da52539..0b06a21a03 100755
--- a/app/Models/Setting.php
+++ b/app/Models/Setting.php
@@ -302,6 +302,19 @@ class Setting extends Model
// regexes below see the same token stream the browser will.
$custom_css = preg_replace('#/\*.*?\*/#s', '', (string) $custom_css);
+ // Custom-property declarations (`--name: ...`) let a planter stash
+ // an arbitrary value that `var(--name)` can inject into a url()
+ // argument at computed-value time, bypassing the literal-text
+ // check further down. A planter could also override one of
+ // Snipe-IT's own `:root` variables and feed it back into any
+ // builtin rule that reads it.
+ // CSS reads existing variables via `var(--primary-color)`
+ // but does not need to declare new ones, so refuse any CSS that
+ // declares a custom property.
+ if (preg_match('/--[a-zA-Z0-9_-]+\s*:/', $custom_css)) {
+ return '';
+ }
+
// \b (word boundary) instead of \s+ so `@import"url"` and
// `@import/*c*/"url"` (both valid CSS tokenizations that a
// \s+ pattern would leave in place) still get stripped. \b
@@ -334,6 +347,14 @@ class Setting extends Model
return '';
}
+ // Defense in depth against the custom-property stash path
+ // caught above. If a later edit relaxes the --* declaration
+ // guard, we still refuse any url() value that resolves
+ // through var(). See GHSA-gmvp-6qh3-5xm6.
+ if (stripos($value, 'var(') !== false) {
+ return '';
+ }
+
// Allowlist rather than denylist: reject any value that
// starts with a URI scheme (`scheme:`, with or without
// `//`) or a protocol-relative `//`. Same-origin relative
diff --git a/tests/Unit/Models/SettingCustomCssTest.php b/tests/Unit/Models/SettingCustomCssTest.php
index dbbbc99437..658e78384c 100644
--- a/tests/Unit/Models/SettingCustomCssTest.php
+++ b/tests/Unit/Models/SettingCustomCssTest.php
@@ -335,6 +335,59 @@ class SettingCustomCssTest extends TestCase
$this->assertStringNotContainsString('image(', $out);
}
+ public function test_var_indirection_through_custom_property_is_blocked(): void
+ {
+ /*
+ * GHSA-gmvp-6qh3-5xm6: Reporter's PoC. `url()` scheme check sees
+ * `var(--b)` which has no scheme literal, but the browser resolves
+ * the variable at computed-value time and fetches the stashed URL.
+ */
+ $out = $this->withCustomCss(':root{--b:"https://attacker.example/p"}body{background:url(var(--b))}');
+
+ $this->assertStringNotContainsString('attacker.example', $out);
+ $this->assertStringNotContainsString('--b', $out);
+ $this->assertStringNotContainsString('var(', $out);
+ }
+
+ public function test_custom_property_declaration_alone_is_blocked(): void
+ {
+ /*
+ * A planter could override one of Snipe-IT's own :root variables
+ * to redirect any builtin rule that reads it, so refuse the
+ * declaration shape outright even without a visible url().
+ */
+ $out = $this->withCustomCss(':root { --primary-color: red; } .nav { color: black; }');
+
+ $this->assertSame('', $out);
+ }
+
+ public function test_reading_an_existing_variable_still_works(): void
+ {
+ /*
+ * Admin branding CSS legitimately reads existing Snipe-IT
+ * variables (e.g. to recolor nav elements). The fix must not
+ * break that use case.
+ */
+ $out = $this->withCustomCss('.nav > li { color: var(--primary-color); }');
+
+ $this->assertStringContainsString('var(--primary-color)', $out);
+ }
+
+ public function test_var_inside_url_argument_is_rejected_defense_in_depth(): void
+ {
+ /*
+ * Defense-in-depth check inside the url() callback. Exercise it
+ * directly by feeding a CSS payload that uses var() only inside a
+ * url() argument (no top-level --* declaration to trip the earlier
+ * guard). The url() must emit empty while the surrounding rule
+ * still carries through.
+ */
+ $out = $this->withCustomCss('body { background: url(var(--existing-color)); color: red; }');
+
+ $this->assertStringNotContainsString('var(', $out);
+ $this->assertStringContainsString('color: red', $out);
+ }
+
public function test_balanced_paren_walker_handles_nested_url_inside_image_set(): void
{
// image-set can wrap url() calls as children. The stripper