| 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 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class SVGCSSChecker { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
private const BANNED_AT_RULE = [ |
| 28 |
'charset', |
| 29 |
'import' |
| 30 |
]; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
private const BANNED_FUNCS = [ |
| 37 |
'src', |
| 38 |
'image', |
| 39 |
'image-set' |
| 40 |
]; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 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 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
$alteredStyle = Sanitizer::checkCss( $value ); |
| 63 |
if ( $alteredStyle === $value ) { |
| 64 |
|
| 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 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 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 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 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 |
|
| 132 |
$res = $this->validateTokens( $rule->toTokenArray(), true ); |
| 133 |
if ( $res !== true ) { |
| 134 |
return $res; |
| 135 |
} |
| 136 |
continue; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
$res = $this->validateTokens( $rule->toTokenArray() ); |
| 142 |
if ( $res !== true ) { |
| 143 |
return $res; |
| 144 |
} |
| 145 |
} |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 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 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
private function validateTokens( array $tokens, $allowDataFonts = false ) { |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
for ( $i = 0; $i < count( $tokens ); $i++ ) { |
| 183 |
$token = $tokens[$i]; |
| 184 |
|
| 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,' ) ) |
| 191 |
) |
| 192 |
) { |
| 193 |
return [ 'banned-url', $token->getPosition()[0], $token->getPosition()[1] ]; |
| 194 |
} |
| 195 |
} elseif ( $token->type() === Token::T_BAD_URL ) { |
| 196 |
|
| 197 |
|
| 198 |
|
| 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,' ) ) |
| 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 |
} |