X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=img_auth.php;h=bb419b39aa222a297c3aa3721990d03fe4517440;hb=6bf91826d0f2bfcfec8ada8cdbd00611846b97e9;hp=02240ceea9e124084831f7fb8befe27bf2148bbc;hpb=24babf696a57e7828522c7d5c9e4cdcada5433ad;p=lhc%2Fweb%2Fwiklou.git diff --git a/img_auth.php b/img_auth.php index 02240ceea9..bb419b39aa 100644 --- a/img_auth.php +++ b/img_auth.php @@ -1,173 +1,90 @@ 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_IMAGE, $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 ); - -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

-"; - exit; + header( 'HTTP/1.0 403 Forbidden' ); + header( 'Vary: Cookie' ); + header( 'Content-Type: text/html; charset=utf-8' ); + echo << + +

Access Denied

+

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

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