r81334 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81333‎ | r81334 | r81335 >
Date:22:37, 1 February 2011
Author:tstarling
Status:ok
Tags:
Comment:
Security fixes
Modified paths:
  • /branches/wmf/1.16wmf4/includes/Sanitizer.php (modified) (history)
  • /branches/wmf/1.16wmf4/includes/StringUtils.php (modified) (history)
  • /branches/wmf/1.16wmf4/includes/StubObject.php (modified) (history)
  • /branches/wmf/1.16wmf4/languages/Language.php (modified) (history)

Diff [purge]

Index: branches/wmf/1.16wmf4/includes/StubObject.php
@@ -152,7 +152,7 @@
153153 $code = strtolower( $code );
154154
155155 # Validate $code
156 - if( empty( $code ) || !preg_match( '/^[a-z-]+$/', $code ) || ( $code === 'qqq' ) ) {
 156+ if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
157157 wfDebug( "Invalid user language code\n" );
158158 $code = $wgContLanguageCode;
159159 }
Index: branches/wmf/1.16wmf4/includes/Sanitizer.php
@@ -739,6 +739,13 @@
740740 // Remove any comments; IE gets token splitting wrong
741741 $value = StringUtils::delimiterReplace( '/*', '*/', ' ', $value );
742742
 743+ // Remove anything after a comment-start token, to guard against
 744+ // incorrect client implementations.
 745+ $commentPos = strpos( $value, '/*' );
 746+ if ( $commentPos !== false ) {
 747+ $value = substr( $value, 0, $commentPos );
 748+ }
 749+
743750 // Decode escape sequences and line continuation
744751 // See the grammar in the CSS 2 spec, appendix D.
745752 static $decodeRegex, $reencodeTable;
Index: branches/wmf/1.16wmf4/includes/StringUtils.php
@@ -77,16 +77,20 @@
7878 }
7979
8080 if ( $tokenType == 'start' ) {
81 - $inputPos = $tokenOffset + $tokenLength;
8281 # Only move the start position if we haven't already found a start
8382 # This means that START START END matches outer pair
8483 if ( !$foundStart ) {
8584 # Found start
 85+ $inputPos = $tokenOffset + $tokenLength;
8686 # Write out the non-matching section
8787 $output .= substr( $subject, $outputPos, $tokenOffset - $outputPos );
8888 $outputPos = $tokenOffset;
8989 $contentPos = $inputPos;
9090 $foundStart = true;
 91+ } else {
 92+ # Move the input position past the *first character* of START,
 93+ # to protect against missing END when it overlaps with START
 94+ $inputPos = $tokenOffset + 1;
9195 }
9296 } elseif ( $tokenType == 'end' ) {
9397 if ( $foundStart ) {
Index: branches/wmf/1.16wmf4/languages/Language.php
@@ -144,6 +144,14 @@
145145 protected static function newFromCode( $code ) {
146146 global $IP;
147147 static $recursionLevel = 0;
 148+
 149+ // Protect against path traversal below
 150+ if ( !Language::isValidCode( $code )
 151+ || strcspn( $code, "/\\\000" ) !== strlen( $code ) )
 152+ {
 153+ throw new MWException( "Invalid language code \"$code\"" );
 154+ }
 155+
148156 if ( $code == 'en' ) {
149157 $class = 'Language';
150158 } else {
@@ -174,6 +182,14 @@
175183 }
176184
177185 /**
 186+ * Returns true if a language code string is of a valid form, whether or
 187+ * not it exists.
 188+ */
 189+ public static function isValidCode( $code ) {
 190+ return (bool)preg_match( '/^[a-z-]+$/', $code );
 191+ }
 192+
 193+ /**
178194 * Get the LocalisationCache instance
179195 */
180196 public static function getLocalisationCache() {
@@ -2627,6 +2643,13 @@
26282644 * @return string $prefix . $mangledCode . $suffix
26292645 */
26302646 static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
 2647+ // Protect against path traversal
 2648+ if ( !Language::isValidCode( $code )
 2649+ || strcspn( $code, "/\\\000" ) !== strlen( $code ) )
 2650+ {
 2651+ throw new MWException( "Invalid language code \"$code\"" );
 2652+ }
 2653+
26312654 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
26322655 }
26332656

Status & tagging log