MediaWiki r19996 - Code Review

Jump to: navigation, search
Repository:MediaWiki
Revision:r19995‎ | r19996 (on ViewVC)‎ | r19997 >
Date:23:03, 19 February 2007
Author:tstarling
Status:old
Tags:
Comment:
* Moved the main ob_start() from the default LocalSettings.php to WebStart.php.
The ob_start() section should preferably be removed from older
LocalSettings.php files.
* Give Content-Length header for HTTP/1.0 clients.
* Partial support for Flash cross-domain-policy filtering. Text entry points should be protected, but uploads are not.
Modified paths:

Diff [purge]

Index: trunk/phase3/includes/OutputHandler.php
===================================================================
--- trunk/phase3/includes/OutputHandler.php	(revision 0)
+++ trunk/phase3/includes/OutputHandler.php	(revision 19996)
@@ -0,0 +1,64 @@
+<?php
+
+/**
+ * Standard output handler for use with ob_start
+ */
+function wfOutputHandler( $s ) {
+	global $wgDisableOutputCompression;
+	$s = wfMangleFlashPolicy( $s );
+	if ( !ini_get( 'zlib.output_compression' ) ) {
+		if ( $wgDisableOutputCompression || !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
+			$s = wfGzipHandler( $s );
+		}
+		if ( !ini_get( 'output_handler' ) ) {
+			wfDoContentLength( strlen( $s ) );
+		}
+	}
+	return $s;
+}
+
+/**
+ * Handler that compresses data with gzip if allowed by the Accept header.
+ * Unlike ob_gzhandler, it works for HEAD requests too.
+ */
+function wfGzipHandler( $s ) {
+	if ( $s !== '' && function_exists( 'gzencode' ) && !headers_sent() ) {
+		$tokens = preg_split( '/[,; ]/', $_SERVER['HTTP_ACCEPT_ENCODING'] );
+		if ( in_array( 'gzip', $tokens ) ) {
+			header( 'Content-Encoding: gzip' );
+			$s = gzencode( $s, 3 );
+
+			# Set vary header if it hasn't been set already
+			$headers = headers_list();
+			$foundVary = false;
+			foreach ( $headers as $header ) {
+				if ( substr( $header, 0, 5 ) == 'Vary:' ) {
+					$foundVary == true;
+					break;
+				}
+			}
+			if ( !$foundVary ) {
+				header( 'Vary: Accept-Encoding' );
+			}
+		}
+	}
+	return $s;
+}
+
+/**
+ * Mangle flash policy tags which open up the site to XSS attacks.
+ */
+function wfMangleFlashPolicy( $s ) {
+	return preg_replace( '/\<\s*cross-domain-policy\s*\>/i', '<NOT-cross-domain-policy>', $s );
+}
+
+/**
+ * Add a Content-Length header if possible. This makes it cooperate with squid better.
+ */
+function wfDoContentLength( $length ) {
+	if ( !headers_sent() && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
+		header( "Content-Length: $length" );
+	}
+}
+
+?>

Property changes on: trunk/phase3/includes/OutputHandler.php
___________________________________________________________________
Name: svn:eol-style
   + native

Index: trunk/phase3/includes/WebStart.php
===================================================================
--- trunk/phase3/includes/WebStart.php	(revision 19995)
+++ trunk/phase3/includes/WebStart.php	(revision 19996)
@@ -85,7 +85,17 @@
 # Include this site setttings
 require_once( './LocalSettings.php' );
 wfProfileOut( 'WebStart.php-conf' );
+wfProfileIn( 'WebStart.php-ob_start' );
 
+# Initialise output buffering
+if ( ob_get_level() ) {
+	# Someone's been mixing configuration data with code!
+	# How annoying.
+} elseif ( !defined( 'MW_NO_OUTPUT_BUFFER' ) ) {
+	require_once( './includes/OutputHandler.php' );
+	ob_start( 'wfOutputHandler' );
+}
+
 if ( !defined( 'MW_NO_SETUP' ) ) {
 	require_once( './includes/Setup.php' );
 }
Index: trunk/phase3/includes/DefaultSettings.php
===================================================================
--- trunk/phase3/includes/DefaultSettings.php	(revision 19995)
+++ trunk/phase3/includes/DefaultSettings.php	(revision 19996)
@@ -2464,4 +2464,9 @@
  */
 $wgEnableCascadingProtection = true;
 
+/**
+ * Disable output compression (enabled by default if zlib is available)
+ */
+$wgDisableOutputCompression = false;
+
 ?>
Index: trunk/phase3/img_auth.php
===================================================================
--- trunk/phase3/img_auth.php	(revision 19995)
+++ trunk/phase3/img_auth.php	(revision 19996)
@@ -7,6 +7,7 @@
  * to an array of pages you want everyone to be able to access. Your server must
  * support PATH_INFO, CGI-based configurations generally don't.
  */
+define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
 require_once( './includes/WebStart.php' );
 wfProfileIn( 'img_auth.php' );
 require_once( './includes/StreamFile.php' );
Index: trunk/phase3/config/index.php
===================================================================
--- trunk/phase3/config/index.php	(revision 19995)
+++ trunk/phase3/config/index.php	(revision 19996)
@@ -245,7 +245,7 @@
 <?php
 $endl = "
 ";
-$wgNoOutputBuffer = true;
+define( 'MW_NO_OUTPUT_BUFFER', 1 );
 $conf = new ConfigData;
 
 install_version_checks();
@@ -390,13 +390,6 @@
 	print "</li>\n";
 }
 
-$conf->zlib = function_exists( "gzencode" );
-if( $conf->zlib ) {
-	print "<li>Have zlib support; enabling output compression.</li>\n";
-} else {
-	print "<li>No zlib support.</li>\n";
-}
-
 $conf->turck = function_exists( 'mmcache_get' );
 if ( $conf->turck ) {
 	print "<li><a href=\"http://turck-mmcache.sourceforge.net/\">Turck MMCache</a> installed</li>\n";
@@ -1286,7 +1279,6 @@
 function writeLocalSettings( $conf ) {
 	$conf->UseImageResize = $conf->UseImageResize ? 'true' : 'false';
 	$conf->PasswordSender = $conf->EmergencyContact;
-	$zlib = ($conf->zlib ? "" : "# ");
 	$magic = ($conf->ImageMagick ? "" : "# ");
 	$convert = ($conf->ImageMagick ? $conf->ImageMagick : "/usr/bin/convert" );
 	$rights = ($conf->RightsUrl) ? "" : "# ";
@@ -1381,10 +1373,9 @@
 	if ( isset( \$_SERVER ) && array_key_exists( 'REQUEST_METHOD', \$_SERVER ) ) {
 		die( \"This script must be run from the command line\\n\" );
 	}
-} elseif ( empty( \$wgNoOutputBuffer ) ) {
-	## Compress output if the browser supports it
-	{$zlib}if( !ini_get( 'zlib.output_compression' ) ) @ob_start( 'ob_gzhandler' );
 }
+## Uncomment this to disable output compression
+# \$wgDisableOutputCompression = true;
 
 \$wgSitename         = \"{$slconf['Sitename']}\";
 
Index: trunk/phase3/thumb.php
===================================================================
--- trunk/phase3/thumb.php	(revision 19995)
+++ trunk/phase3/thumb.php	(revision 19996)
@@ -5,6 +5,7 @@
  * If the file exists, we make do with abridged MediaWiki initialisation.
  */
 define( 'MW_NO_SETUP', 1 );
+define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
 require_once( './includes/WebStart.php' );
 wfProfileIn( 'thumb.php' );
 wfProfileIn( 'thumb.php-start' );
Index: trunk/phase3/RELEASE-NOTES
===================================================================
--- trunk/phase3/RELEASE-NOTES	(revision 19995)
+++ trunk/phase3/RELEASE-NOTES	(revision 19996)
@@ -197,6 +197,11 @@
 * Sort log types in Special:Log
 * Added a classname ("mw-toolbar-editbutton") and unique IDs to the edit
   toolbar buttons
+* Moved the main ob_start() from the default LocalSettings.php to WebStart.php. 
+  The ob_start() section should preferably be removed from older 
+  LocalSettings.php files.
+* Give Content-Length header for HTTP/1.0 clients.
+* Partial support for Flash cross-domain-policy filtering. 
 
 
 == Languages updated ==
Personal tools
Namespaces
Variants
Views
Actions
Site
Support
Download
Development
Communication
Toolbox