mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2024-11-10 15:48:42 +00:00
d3abcb90e3
The new logic is as follows: * Given a URL to insert into url(), check that it is properly URL encoded (in particular, a doublequote and backslash never occurs within it) and then place it as url("http://example.com"). * Given a font name, if it is strictly alphanumeric, it is safe to omit quotes. Otherwise, wrap in double quotes and replace '"' with '\22 ' (note trailing space) and '\' with '\5C ' (ditto). We introduce expandCSSEscape() which is a hack for common parsing idioms in CSS; this means that CSS escapes are now recognized inside URLs as well as unquoted font names. Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
53 lines
1.7 KiB
PHP
53 lines
1.7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Validates a URI in CSS syntax, which uses url('http://example.com')
|
|
* @note While theoretically speaking a URI in a CSS document could
|
|
* be non-embedded, as of CSS2 there is no such usage so we're
|
|
* generalizing it. This may need to be changed in the future.
|
|
* @warning Since HTMLPurifier_AttrDef_CSS blindly uses semicolons as
|
|
* the separator, you cannot put a literal semicolon in
|
|
* in the URI. Try percent encoding it, in that case.
|
|
*/
|
|
class HTMLPurifier_AttrDef_CSS_URI extends HTMLPurifier_AttrDef_URI
|
|
{
|
|
|
|
public function __construct() {
|
|
parent::__construct(true); // always embedded
|
|
}
|
|
|
|
public function validate($uri_string, $config, $context) {
|
|
// parse the URI out of the string and then pass it onto
|
|
// the parent object
|
|
|
|
$uri_string = $this->parseCDATA($uri_string);
|
|
if (strpos($uri_string, 'url(') !== 0) return false;
|
|
$uri_string = substr($uri_string, 4);
|
|
$new_length = strlen($uri_string) - 1;
|
|
if ($uri_string[$new_length] != ')') return false;
|
|
$uri = trim(substr($uri_string, 0, $new_length));
|
|
|
|
if (!empty($uri) && ($uri[0] == "'" || $uri[0] == '"')) {
|
|
$quote = $uri[0];
|
|
$new_length = strlen($uri) - 1;
|
|
if ($uri[$new_length] !== $quote) return false;
|
|
$uri = substr($uri, 1, $new_length - 1);
|
|
}
|
|
|
|
$uri = $this->expandCSSEscape($uri);
|
|
|
|
$result = parent::validate($uri, $config, $context);
|
|
|
|
if ($result === false) return false;
|
|
|
|
// extra sanity check; should have been done by URI
|
|
$result = str_replace(array('"', "\\", "\n", "\x0c", "\r"), "", $result);
|
|
|
|
return "url(\"$result\")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// vim: et sw=4 sts=4
|