X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=img_auth.php;h=9141018103b4c0a0fe2e8347cce663ad0ec6c502;hb=d57050f8fca2a9465e7e2254d277648d7e095dee;hp=02240ceea9e124084831f7fb8befe27bf2148bbc;hpb=24babf696a57e7828522c7d5c9e4cdcada5433ad;p=lhc%2Fweb%2Fwiklou.git diff --git a/img_auth.php b/img_auth.php index 02240ceea9..9141018103 100644 --- a/img_auth.php +++ b/img_auth.php @@ -1,173 +1,118 @@ getNsText( NS_IMAGE ) . ":" . basename( $_SERVER['PATH_INFO'] ); +$realUpload = realpath( $wgUploadDirectory ); +wfDebugLog( 'img_auth', "\$path is {$path}" ); +wfDebugLog( 'img_auth', "\$filename is {$filename}" ); -# Check if the filename is in the correct directory -if ( substr( $filename, 0, strlen( $realUploadDirectory ) ) != $realUploadDirectory ) { +// Basic directory traversal check +if( substr( $filename, 0, strlen( $realUpload ) ) != $realUpload ) { + wfDebugLog( 'img_auth', 'Requested path not in upload directory' ); wfForbidden(); } -if ( is_array( $wgWhitelistRead ) && !in_array( $imageName, $wgWhitelistRead ) && !$wgUser->getID() ) { +// Extract the file name and chop off the size specifier +// (e.g. 120px-Foo.png => Foo.png) +$name = wfBaseName( $path ); +if( preg_match( '!\d+px-(.*)!i', $name, $m ) ) + $name = $m[1]; +wfDebugLog( 'img_auth', "\$name is {$name}" ); + +$title = Title::makeTitleSafe( NS_FILE, $name ); +if( !$title instanceof Title ) { + wfDebugLog( 'img_auth', "Unable to construct a valid Title from `{$name}`" ); wfForbidden(); } +$title = $title->getPrefixedText(); -# Write file -$type = wfGetType( $filename ); -if ( $type ) { - header("Content-type: $type"); +// Check the whitelist if needed +if( !$wgUser->getId() && ( !is_array( $wgWhitelistRead ) || !in_array( $title, $wgWhitelistRead ) ) ) { + wfDebugLog( 'img_auth', "Not logged in and `{$title}` not in whitelist." ); + wfForbidden(); } -readfile( $filename ); +if( !file_exists( $filename ) ) { + wfDebugLog( 'img_auth', "`{$filename}` does not exist" ); + wfForbidden(); +} +if( is_dir( $filename ) ) { + wfDebugLog( 'img_auth', "`{$filename}` is a directory" ); + wfForbidden(); +} -function wfGetType( $filename ) { - # There's probably a better way to do this - $types = << + +

Access Denied

+

You need to log in to access files on this server.

+ + +ENDS; + wfLogProfilingData(); + exit(); } -function wfForbidden() { - header( "HTTP/1.0 403 Forbidden" ); - print -" -

Access denied

-

You need to log in to access files on this server

-"; +/** + * Show a 403 error for use when the wiki is public + */ +function wfPublicError() { + header( 'HTTP/1.0 403 Forbidden' ); + header( 'Content-Type: text/html; charset=utf-8' ); + echo << + +

Access Denied

+

The function of img_auth.php is to output files from a private wiki. This wiki +is configured as a public wiki. For optimal security, img_auth.php is disabled in +this case. +

+ + +ENDS; + wfLogProfilingData(); exit; } -?>