Debian Codegraph @

includes/upload/SVGCSSChecker.php

raw symbols SCIP
1 <?php
2 namespace MediaWiki\Upload;
3
4 use MediaWiki\Parser\Sanitizer;
5 use Wikimedia\CSS\Objects\AtRule;
6 use Wikimedia\CSS\Objects\Token;
7 use Wikimedia\CSS\Parser\Parser as CSSParser;
8
9 /**
10 * Ensure SVG files cannot load external resources via URLs in CSS.
11 *
12 * Beyond that restriction, it aims to be relaxed in the CSS it allows.
13 *
14 * Data: urls are also banned except in @font-face. The rationale behind
15 * this is unclear. The restriction was copied over from the predecessor to
16 * this class.
17 *
18 * @since 1.43.6
19 */
20 class SVGCSSChecker {
21
22 /**
23 * List of \@rules banned.
24 * \@import for obvious reasons.
25 * \@charset just in case although i expect it does nothing inside an svg.
26 */
27 private const BANNED_AT_RULE = [
28 'charset',
29 'import'
30 ];
31
32 /**
33 * image() and image-set() can use bare strings as url
34 * src() is not supported by real browsers but is an alias for url() in spec
35 */
36 private const BANNED_FUNCS = [
37 'src',
38 'image',
39 'image-set'
40 ];
41
42 /**
43 * entrypoint to check style="..." attributes
44 *
45 * @param string $value
46 * @return array|bool True if good or array containing error details
47 */
48 public function checkStyleAttribute( string $value ) {
49 if ( preg_match( '/[\000-\010\013\016-\037\177]/', $value ) ) {
50 return [ 'invalid-control-character', 0, 0 ];
51 }
52 $cssParser = CSSParser::newFromString( $value );
53 $decList = $cssParser->parseDeclarationList();
54 $errors = $cssParser->getParseErrors();
55 if ( $errors ) {
56 // For style attributes with syntax errors, as a fallback
57 // we see if MW's wikitext sanitizer would alter the
58 // style attribute in any way. If no, then we assume it
59 // is safe. There are enough files with errors in style
60 // attributes that don't use any risky features like
61 // css comments or url(), that this is worth it.
62 $alteredStyle = Sanitizer::checkCss( $value );
63 if ( $alteredStyle === $value ) {
64 // No sketchy CSS features used, its ok despite errors
65 return true;
66 }
67 return [ $errors[0][0], $errors[0][1], $errors[0][2] ];
68 }
69
70 $res = $this->validateTokens( $decList->toTokenArray() );
71 if ( $res !== true ) {
72 return $res;
73 }
74 return true;
75 }
76
77 /**
78 * entrypoint to check presentational attributes like fill
79 *
80 * Presentational attributes can contain CSS like values such as url()
81 *
82 * @param string $value
83 * @return array|bool True if good or array containing error details
84 */
85 public function checkPresentationalAttribute( $value ) {
86 if ( preg_match( '/[\000-\010\013\016-\037\177]/', $value ) ) {
87 return [ 'invalid-control-character', 0, 0 ];
88 }
89 $cssParser = CSSParser::newFromString( $value );
90 $cvList = $cssParser->parseComponentValueList();
91 $errors = $cssParser->getParseErrors();
92 if ( $errors ) {
93 return [ $errors[0][0], $errors[0][1], $errors[0][2] ];
94 }
95
96 $res = $this->validateTokens( $cvList->toTokenArray() );
97 if ( $res !== true ) {
98 return $res;
99 }
100 return true;
101 }
102
103 /**
104 * Entrypoint to check <style> tags
105 *
106 * Note that data urls are allowed in @font-face
107 *
108 * @param string $value
109 * @return array|bool True if good or array containing error details
110 */
111 public function checkStyleTag( $value ) {
112 if ( preg_match( '/[\000-\010\013\016-\037\177]/', $value ) ) {
113 return [ 'invalid-control-character', 0, 0 ];
114 }
115 $cssParser = CSSParser::newFromString( $value );
116 $stylesheet = $cssParser->parseStylesheet();
117
118 $errors = $cssParser->getParseErrors();
119 if ( $errors ) {
120 return [ $errors[0][0], $errors[0][1], $errors[0][2] ];
121 }
122
123 $topLevelRules = $stylesheet->getRuleList();
124 foreach ( $topLevelRules as $rule ) {
125 if ( $rule instanceof AtRule ) {
126 $res = $this->validateAtRule( $rule );
127 if ( $res !== true ) {
128 return $res;
129 }
130 if ( $rule->getName() === 'font-face' ) {
131 // @font-face has laxer rules
132 $res = $this->validateTokens( $rule->toTokenArray(), true );
133 if ( $res !== true ) {
134 return $res;
135 }
136 continue;
137 }
138 }
139 // Note, this incidentally @namespace foo url( 'https://example.com' );
140 // We don't care about that but its super obscure so doesn't matter.
141 $res = $this->validateTokens( $rule->toTokenArray() );
142 if ( $res !== true ) {
143 return $res;
144 }
145 }
146 return true;
147 }
148
149 /**
150 * Validate that an at-rule is not on the ban list
151 *
152 * @param AtRule $rule
153 * @return array|bool
154 */
155 private function validateAtRule( AtRule $rule ) {
156 $name = strtolower( $rule->getName() );
157 if (
158 in_array( $name, self::BANNED_AT_RULE ) ||
159 preg_match( '/[^-a-z]/', $name )
160 ) {
161 return [ "banned-at-rule-$name", $rule->getPosition()[0], $rule->getPosition()[1] ];
162 }
163 return true;
164 }
165
166 /**
167 * Validate a list of tokens making sure none of them are url-like
168 *
169 * @param Token[] $tokens List of tokens
170 * @param bool $allowDataFonts Allow data: urls with a font type
171 * @return array|bool
172 */
173 private function validateTokens( array $tokens, $allowDataFonts = false ) {
174 // Go through all the tokens, and make sure none of them
175 // are url(). Except we allow urls that reference the current
176 // document. data: urls are not allowed because the predecessor
177 // to this class banned them. It is unclear why, perhaps the worry
178 // is embedding an SVG inside the data url to bypass sanitizer.
179 // We also ban the image and image-set() functions because they
180 // allow setting a url without the url function inside.
181 // We also ban src() for forwards-compatibility.
182 for ( $i = 0; $i < count( $tokens ); $i++ ) {
183 $token = $tokens[$i];
184 // unquoted urls are a T_URL where quoted urls are T_FUNCTION.
185 if ( $token->type() === Token::T_URL ) {
186 if (
187 !str_starts_with( $token->value(), '#' ) &&
188 !( $allowDataFonts &&
189 ( str_starts_with( $token->value(), 'data:font/' )
190 || str_starts_with( $token->value(), 'data:;base64,' ) ) /* T71008#717580 */
191 )
192 ) {
193 return [ 'banned-url', $token->getPosition()[0], $token->getPosition()[1] ];
194 }
195 } elseif ( $token->type() === Token::T_BAD_URL ) {
196 // In theory browsers should ignore this, but
197 // better to err on the side of failing when something
198 // weird is going on.
199 return [ 'banned-url', $token->getPosition()[0], $token->getPosition()[1] ];
200 } elseif ( $token->type() === Token::T_FUNCTION && strtolower( $token->value() ) === 'url' ) {
201 for ( $j = $i + 1; $j < count( $tokens ) && $tokens[$j]->type() === Token::T_WHITESPACE; $j++ );
202 if ( $j < count( $tokens ) && $tokens[$j]->type() === Token::T_STRING ) {
203 if (
204 str_starts_with( $tokens[$j]->value(), '#' ) ||
205 ( $allowDataFonts &&
206 ( str_starts_with( $tokens[$j]->value(), 'data:font/' )
207 || str_starts_with( $tokens[$j]->value(), 'data:;base64,' ) ) /* T71008#717580 */
208 )
209 ) {
210 continue;
211 }
212 }
213 return [ 'banned-url', $token->getPosition()[0], $token->getPosition()[1] ];
214 } elseif (
215 $token->type() === Token::T_FUNCTION &&
216 in_array( strtolower( $token->value() ), self::BANNED_FUNCS )
217 ) {
218 return [ 'banned-function-' . $token->value(), $token->getPosition()[0], $token->getPosition()[1] ];
219 }
220 }
221 return true;
222 }
223 }