r63426 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r63425‎ | r63426 | r63427 >
Date:22:28, 8 March 2010
Author:tstarling
Status:ok
Tags:
Comment:
MFT r63424 (CSS validation issue)
Modified paths:
  • /branches/wmf-deployment/includes/Sanitizer.php (modified) (history)
  • /branches/wmf-deployment/maintenance/parserTests.txt (modified) (history)

Diff [purge]

Index: branches/wmf-deployment/maintenance/parserTests.txt
@@ -4388,7 +4388,24 @@
43894389
43904390 !! end
43914391
 4392+!! test
 4393+CSS line continuation 1
 4394+!! input
 4395+<div style="background-image: u\&#10;rl(test.jpg);"></div>
 4396+!! result
 4397+<div></div>
43924398
 4399+!! end
 4400+
 4401+!! test
 4402+CSS line continuation 2
 4403+!! input
 4404+<div style="background-image: u\&#13;rl(test.jpg); "></div>
 4405+!! result
 4406+<div></div>
 4407+
 4408+!! end
 4409+
43934410 !! article
43944411 Template:Identity
43954412 !! text
Index: branches/wmf-deployment/includes/Sanitizer.php
@@ -665,24 +665,48 @@
666666 * @return Mixed
667667 */
668668 static function checkCss( $value ) {
669 - $stripped = Sanitizer::decodeCharReferences( $value );
 669+ $value = Sanitizer::decodeCharReferences( $value );
670670
671671 // Remove any comments; IE gets token splitting wrong
672 - $stripped = StringUtils::delimiterReplace( '/*', '*/', ' ', $stripped );
 672+ $value = StringUtils::delimiterReplace( '/*', '*/', ' ', $value );
673673
674 - $value = $stripped;
675 -
676 - // ... and continue checks
677 - $stripped = preg_replace( '!\\\\([0-9A-Fa-f]{1,6})[ \\n\\r\\t\\f]?!e',
678 - 'codepointToUtf8(hexdec("$1"))', $stripped );
679 - $stripped = str_replace( '\\', '', $stripped );
680 - if( preg_match( '/(?:expression|tps*:\/\/|url\\s*\().*/is',
681 - $stripped ) ) {
682 - # haxx0r
 674+ // Decode escape sequences and line continuation
 675+ // See the grammar in the CSS 2 spec, appendix D, Mozilla implements it accurately.
 676+ // IE 8 doesn't implement it at all, but there's no way to introduce url() into
 677+ // IE that doesn't hit Mozilla also.
 678+ static $decodeRegex;
 679+ if ( !$decodeRegex ) {
 680+ $space = '[\\x20\\t\\r\\n\\f]';
 681+ $nl = '(?:\\n|\\r\\n|\\r|\\f)';
 682+ $backslash = '\\\\';
 683+ $decodeRegex = "/ $backslash
 684+ (?:
 685+ ($nl) | # 1. Line continuation
 686+ ([0-9A-Fa-f]{1,6})$space? | # 2. character number
 687+ (.) # 3. backslash cancelling special meaning
 688+ )/xu";
 689+ }
 690+ $decoded = preg_replace_callback( $decodeRegex,
 691+ array( __CLASS__, 'cssDecodeCallback' ), $value );
 692+ if ( preg_match( '!expression|https?://|url\s*\(!i', $decoded ) ) {
 693+ // Not allowed
683694 return false;
 695+ } else {
 696+ // Allowed, return CSS with comments stripped
 697+ return $value;
684698 }
 699+ }
685700
686 - return $value;
 701+ static function cssDecodeCallback( $matches ) {
 702+ if ( $matches[1] !== '' ) {
 703+ return '';
 704+ } elseif ( $matches[2] !== '' ) {
 705+ return codepointToUtf8( hexdec( $matches[2] ) );
 706+ } elseif ( $matches[3] !== '' ) {
 707+ return $matches[3];
 708+ } else {
 709+ throw new MWException( __METHOD__.': invalid match' );
 710+ }
687711 }
688712
689713 /**

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r63424Fixed CSS validation issue (no handling for line continuation). Reported by S...tstarling22:22, 8 March 2010

Status & tagging log