r66994 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r66993‎ | r66994 | r66995 >
Date:07:10, 28 May 2010
Author:tstarling
Status:deferred
Tags:
Comment:
MFT r66990: CSS escape sequence normalisation
Modified paths:
  • /branches/wmf/1.16wmf4/includes/Sanitizer.php (modified) (history)

Diff [purge]

Index: branches/wmf/1.16wmf4/includes/Sanitizer.php
@@ -644,10 +644,6 @@
645645 # http://msdn.microsoft.com/workshop/author/dhtml/overview/recalc.asp
646646 if( $attribute == 'style' ) {
647647 $value = Sanitizer::checkCss( $value );
648 - if( $value === false ) {
649 - # haxx0r
650 - continue;
651 - }
652648 }
653649
654650 if ( $attribute === 'id' ) {
@@ -744,10 +740,8 @@
745741 $value = StringUtils::delimiterReplace( '/*', '*/', ' ', $value );
746742
747743 // Decode escape sequences and line continuation
748 - // See the grammar in the CSS 2 spec, appendix D, Mozilla implements it accurately.
749 - // IE 8 doesn't implement it at all, but there's no way to introduce url() into
750 - // IE that doesn't hit Mozilla also.
751 - static $decodeRegex;
 744+ // See the grammar in the CSS 2 spec, appendix D.
 745+ static $decodeRegex, $reencodeTable;
752746 if ( !$decodeRegex ) {
753747 $space = '[\\x20\\t\\r\\n\\f]';
754748 $nl = '(?:\\n|\\r\\n|\\r|\\f)';
@@ -756,30 +750,41 @@
757751 (?:
758752 ($nl) | # 1. Line continuation
759753 ([0-9A-Fa-f]{1,6})$space? | # 2. character number
760 - (.) # 3. backslash cancelling special meaning
 754+ (.) | # 3. backslash cancelling special meaning
 755+ () | # 4. backslash at end of string
761756 )/xu";
762757 }
763 - $decoded = preg_replace_callback( $decodeRegex,
 758+ $value = preg_replace_callback( $decodeRegex,
764759 array( __CLASS__, 'cssDecodeCallback' ), $value );
765 - if ( preg_match( '!expression|https?://|url\s*\(!i', $decoded ) ) {
766 - // Not allowed
767 - return false;
768 - } else {
769 - // Allowed, return CSS with comments stripped
770 - return $value;
 760+
 761+ // Reject problematic keywords and control characters
 762+ if ( preg_match( '/[\000-\010\016-\037\177]/', $value ) ) {
 763+ return '/* invalid control char */';
 764+ } elseif ( preg_match( '! expression | filter\s*: | accelerator\s*: | url\s*\( !ix', $value ) ) {
 765+ return '/* insecure input */';
771766 }
 767+ return $value;
772768 }
773769
774770 static function cssDecodeCallback( $matches ) {
775771 if ( $matches[1] !== '' ) {
 772+ // Line continuation
776773 return '';
777774 } elseif ( $matches[2] !== '' ) {
778 - return codepointToUtf8( hexdec( $matches[2] ) );
 775+ $char = codepointToUtf8( hexdec( $matches[2] ) );
779776 } elseif ( $matches[3] !== '' ) {
780 - return $matches[3];
 777+ $char = $matches[3];
781778 } else {
782 - throw new MWException( __METHOD__.': invalid match' );
 779+ $char = '\\';
783780 }
 781+ if ( $char == "\n" || $char == '"' || $char == "'" || $char == '\\' ) {
 782+ // These characters need to be escaped in strings
 783+ // Clean up the escape sequence to avoid parsing errors by clients
 784+ return '\\' . dechex( ord( $char ) ) . ' ';
 785+ } else {
 786+ // Decode unnecessary escape
 787+ return $char;
 788+ }
784789 }
785790
786791 /**
Property changes on: branches/wmf/1.16wmf4/includes/Sanitizer.php
___________________________________________________________________
Name: svn:mergeinfo
787792 + /branches/REL1_15/phase3/includes/Sanitizer.php:51646
/branches/sqlite/includes/Sanitizer.php:58211-58321
/branches/wmf-deployment/includes/Sanitizer.php:53381,60970
/trunk/phase3/includes/Sanitizer.php:63549,63764,63897-63901,64113,64509,65387,65391,65555,65590,65650,65816,66990

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r66990Normalise CSS escape sequences.tstarling05:02, 28 May 2010

Status & tagging log