r24609 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r24608‎ | r24609 | r24610 >
Date:06:15, 6 August 2007
Author:robchurch
Status:old
Tags:
Comment:
* Fix img_auth.php image name extraction for whitelist checking
* (bug 10756) img_auth.php will now refuse logged-out requests when there is no whitelist, rather than allowing them through
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/img_auth.php (modified) (history)

Diff [purge]

Index: trunk/phase3/img_auth.php
@@ -1,63 +1,89 @@
22 <?php
 3+
34 /**
4 - * Image download authorisation script
 5+ * Image authorisation script
56 *
6 - * To use, in LocalSettings.php set $wgUploadDirectory to point to a non-public
7 - * directory, and $wgUploadPath to point to this file. Also set $wgWhitelistRead
8 - * to an array of pages you want everyone to be able to access. Your server must
9 - * support PATH_INFO, CGI-based configurations generally don't.
 7+ * To use this:
 8+ *
 9+ * Set $wgUploadDirectory to a non-public directory (not web accessible)
 10+ * Set $wgUploadPath to point to this file
 11+ *
 12+ * Your server needs to support PATH_INFO; CGI-based configurations
 13+ * usually don't.
1014 */
 15+
1116 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
12 -require_once( './includes/WebStart.php' );
 17+require_once( dirname( __FILE__ ) . '/includes/WebStart.php' );
1318 wfProfileIn( 'img_auth.php' );
14 -require_once( './includes/StreamFile.php' );
 19+require_once( dirname( __FILE__ ) . '/includes/StreamFile.php' );
1520
 21+// Extract path and image information
1622 if( !isset( $_SERVER['PATH_INFO'] ) ) {
17 - wfDebugLog( 'img_auth', "missing PATH_INFO" );
 23+ wfDebugLog( 'img_auth', 'Missing PATH_INFO' );
1824 wfForbidden();
1925 }
2026
21 -# Get filenames/directories
22 -wfDebugLog( 'img_auth', "PATH_INFO is: " . $_SERVER['PATH_INFO'] );
 27+$path = $_SERVER['PATH_INFO'];
2328 $filename = realpath( $wgUploadDirectory . $_SERVER['PATH_INFO'] );
24 -$realUploadDirectory = realpath( $wgUploadDirectory );
25 -$imageName = $wgContLang->getNsText( NS_IMAGE ) . ":" . wfBaseName( $_SERVER['PATH_INFO'] );
 29+$realUpload = realpath( $wgUploadDirectory );
 30+wfDebugLog( 'img_auth', "\$path is {$path}" );
 31+wfDebugLog( 'img_auth', "\$filename is {$filename}" );
2632
27 -# Check if the filename is in the correct directory
28 -if ( substr( $filename, 0, strlen( $realUploadDirectory ) ) != $realUploadDirectory ) {
29 - wfDebugLog( 'img_auth', "requested path not in upload dir: $filename" );
 33+// Basic directory traversal check
 34+if( substr( $filename, 0, strlen( $realUpload ) ) != $realUpload ) {
 35+ wfDebugLog( 'img_auth', 'Requested path not in upload directory' );
3036 wfForbidden();
3137 }
3238
33 -if ( is_array( $wgWhitelistRead ) && !in_array( $imageName, $wgWhitelistRead ) && !$wgUser->getID() ) {
34 - wfDebugLog( 'img_auth', "not logged in and requested file not in whitelist: $imageName" );
 39+// Extract the file name and chop off the size specifier
 40+// (e.g. 120px-Foo.png => Foo.png)
 41+$name = wfBaseName( $path );
 42+if( preg_match( '!\d+px-(.*)!i', $name, $m ) )
 43+ $name = $m[1];
 44+wfDebugLog( 'img_auth', "\$name is {$name}" );
 45+
 46+$title = Title::makeTitleSafe( NS_IMAGE, $name );
 47+if( !$title instanceof Title ) {
 48+ wfDebugLog( 'img_auth', "Unable to construct a valid Title from `{$name}`" );
3549 wfForbidden();
3650 }
 51+$title = $title->getPrefixedText();
3752
 53+// Check the whitelist if needed
 54+if( !$wgUser->getId() && ( !is_array( $wgWhitelistRead ) || !in_array( $title, $wgWhitelistRead ) ) ) {
 55+ wfDebugLog( 'img_auth', "Not logged in and `{$title}` not in whitelist." );
 56+ wfForbidden();
 57+}
 58+
3859 if( !file_exists( $filename ) ) {
39 - wfDebugLog( 'img_auth', "requested file does not exist: $filename" );
 60+ wfDebugLog( 'img_auth', "`{$filename}` does not exist" );
4061 wfForbidden();
4162 }
4263 if( is_dir( $filename ) ) {
43 - wfDebugLog( 'img_auth', "requested file is a directory: $filename" );
 64+ wfDebugLog( 'img_auth', "`{$filename}` is a directory" );
4465 wfForbidden();
4566 }
4667
47 -# Write file
48 -wfDebugLog( 'img_auth', "streaming file: $filename" );
 68+// Stream the requested file
 69+wfDebugLog( 'img_auth', "Streaming `{$filename}`" );
4970 wfStreamFile( $filename );
5071 wfLogProfilingData();
5172
 73+/**
 74+ * Issue a standard HTTP 403 Forbidden header and a basic
 75+ * error message, then end the script
 76+ */
5277 function wfForbidden() {
5378 header( 'HTTP/1.0 403 Forbidden' );
5479 header( 'Content-Type: text/html; charset=utf-8' );
55 - print
56 -"<html><body>
57 -<h1>Access denied</h1>
58 -<p>You need to log in to access files on this server</p>
59 -</body></html>";
 80+ echo <<<END
 81+<html>
 82+<body>
 83+<h1>Access Denied</h1>
 84+<p>You need to log in to access files on this server.</p>
 85+</body>
 86+</html>
 87+END;
6088 wfLogProfilingData();
61 - exit;
62 -}
63 -
64 -
 89+ exit();
 90+}
\ No newline at end of file
Index: trunk/phase3/RELEASE-NOTES
@@ -356,6 +356,9 @@
357357 edit box scroll position preserve/restore behaviour
358358 * (bug 10805) Fix "undo" link when viewing the diff of the most recent
359359 change to a page using "diff=0"
 360+* img_auth.php now interacts properly with $wgWhitelistRead
 361+* (bug 10765) img_auth.php will now forbid access to images if $wgWhitelistRead
 362+ is not set to an array
360363
361364 == API changes since 1.10 ==
362365

Follow-up revisions

RevisionCommit summaryAuthorDate
r24632Merged revisions 24601-24631 via svnmerge from...david18:44, 6 August 2007

Status & tagging log