mediawiki-extensions-Templa.../TemplateStylesMatcherFactory.php
Brad Jorsch b301a30abf Use wikimedia/css-sanitizer, and rewrite the hooking
wikimedia/css-sanitizer provides a real CSS parser, which should be
safer than poking at things with regular expressions.

Instead of the strange hybrid model that tried to both process inline
CSS and save CSS when the template is saved, it now looks for
<templatestyles src="Title" /> during the parse to do all the
transclusion of styles.

The output method is "<style> tags in the body", pending someone
implementing T160563.

It now also registers a "sanitized-css" content model, which should pick
up the CSS syntax highlighting and will validate the submitted CSS on
submit and prevent a save if it's not valid.

This patch also takes advantage of LGPL-2.x § 3 to relicense the
extension as GPL-2.0+, although at this point none of the LGPL code
remains anyway.

Bug: T133408
Bug: T136054
Bug: T135788
Bug: T135789
Change-Id: I993e6f18d32a43aac8398743133d227b05133bbd
Depends-On: If4eb5bf71f94fa366ec4eddb6964e8f4df6b824a
2017-06-07 15:14:09 +00:00

81 lines
2.1 KiB
PHP

<?php
/**
* @file
* @license https://opensource.org/licenses/GPL-2.0 GPL-2.0+
*/
use Wikimedia\CSS\Objects\ComponentValue;
use Wikimedia\CSS\Objects\Token;
use Wikimedia\CSS\Grammar\TokenMatcher;
use Wikimedia\CSS\Grammar\UrlMatcher;
/**
* Extend the standard factory for TemplateStyles-specific matchers
*/
class TemplateStylesMatcherFactory extends \Wikimedia\CSS\Grammar\MatcherFactory {
/** @var array URL validation regexes */
protected $allowedDomains;
/**
* @param array $allowedDomains See $wgTemplateStylesAllowedUrls
*/
public function __construct( array $allowedDomains ) {
$this->allowedDomains = $allowedDomains;
}
/**
* Check a URL for safety
* @param string $type
* @param string $url
* @return bool
*/
protected function checkUrl( $type, $url ) {
// Undo unnecessary percent encoding
$url = preg_replace_callback( '/%[2-7][0-9A-Fa-f]/', function ( $m ) {
$char = urldecode( $m[0] );
if ( strpos( '"#%<>[\]^`{|}/?&=+;', $char ) === false ) {
# Unescape it
return $char;
}
return $m[0];
}, $url );
// Don't allow unescaped \ or /../ in the non-query part of the URL
$tmp = preg_replace( '<[#?].*$>', '', $url );
if ( strpos( $tmp, '\\' ) !== false || preg_match( '<(?:^|/|%2[fF])\.+(?:/|%2[fF]|$)>', $tmp ) ) {
return false;
}
// Run it through the whitelist
$regexes = isset( $this->allowedDomains[$type] ) ? $this->allowedDomains[$type] : [];
foreach ( $regexes as $regex ) {
if ( preg_match( $regex, $url ) ) {
return true;
}
}
return false;
}
public function urlstring( $type ) {
$key = __METHOD__ . ':' . $type;
if ( !isset( $this->cache[$key] ) ) {
$this->cache[$key] = new TokenMatcher( Token::T_STRING, function ( Token $t ) use ( $type ) {
return $this->checkUrl( $type, $t->value() );
} );
}
return $this->cache[$key];
}
public function url( $type ) {
$key = __METHOD__ . ':' . $type;
if ( !isset( $this->cache[$key] ) ) {
$this->cache[$key] = new UrlMatcher( function ( $url, $modifiers ) use ( $type ) {
return !$modifiers && $this->checkUrl( $type, $url );
} );
}
return $this->cache[$key];
}
}