This article is for system administrators who wish to restrict access to images and files based on user and/oruser group permissions.
Uploaded files are generally served directly by the web server, not through MediaWiki.While there is a minimal level of security through obscurity with path obfuscation (e.g./c/c4/…), the path can be calculated easily from the file name and does not provide true protection.
This is not a recommended configuration.MediaWiki is not designed to be aCMS, or to protect sensitive data.To the contrary, it was designed to be as open as possible.Thus it does not inherently support full featured, air-tight protection of private content.Any administrator wishing to use this functionality should carefully reviewSecurity issues with authorization extensions.
By default, all uploaded images (and files) are accessible directly by the web server.If you wish to allow access only to authorized users within the MediaWiki framework, two conditions must be met:
The fundamental implementation requires:
$wgUploadDirectory) should be moved outside the web root on the file system or otherwise protected; and$wgUploadPath) should point toimg_auth.php.The mechanisms for both are dependent on the web server platform.This article gives detailed instructions for two platforms:
For all instruction, assume that my MediaWiki is installed in "/path/to".
For example in:
http://wiki.example.org/MyWiki
"/path/to" is "/MyWiki"
Image authorization works by routing requests for uploaded media files through the img_auth.php script, instead of allowing the webserver to send the file to the browser directly.This is done by setting$wgUploadPath to the location of the img_auth.php script ($wgUploadPath = "$wgScriptPath/img_auth.php";), instead of the upload diectory.This causes MediaWiki to generate file URLs that look something likehttp://wiki.example.org/w/img_auth.php/01/01/Example.png.When the web server receives a request for such a URL, it will know to call theimg_auth.php script and pass it the remainder of the URL asPATH_INFO.The script will then check whether the user has access to the file given in thePATH_INFO, based on the normal mechanism used for managing access to wiki pages.If img_auth.php determines that the user has access to the requested file, it reads the file's content and streams it back to the user, just as if the web server was serving the file directly from the disk.If, however, the user does not have access to the file, img_auth.php returns the standard 403 "Access Denied" error.
Before attempting this configuration it is very important you understand how toconfigure file uploads.Please take a few moments to review and understand this article - it will save you a lot of time.
This requires that your PHP setup support PATH_INFO (many CGI configurations do not) and you need to be in$wgWhitelistRead mode or else, there wouldn't be a point ... unless you just like a more secure MW install.Seebelow.
Even if you don't want to restrict access to your images you might want to make use of theimg_auth.php mechanism: to avoid publicly accessible directories, where the web server has write permissions.Though a web server writable directory is not insecure in itself, it is the first half of a successful attack to your web server.The second half then would be some exploitable (php) script, being MW or, most likely, some other script.If the attacker can exploit the broken script to upload or generate another script intended to help him with further attacks/spamming etc, the attacker still needs a place to store that script in, writable by the web server ... and has it available and well known in theimages directory of MW standard installations.
A very first security measure against this will be to place a.htaccess file inside theimages directory with this content:
# No php execution in the upload areaphp_admin_flagengineoff
And that.htaccess mustnot be writable by the web server! It is a pity, that MW doesn't come with this by default (at least not in 1.6.10).
But even better will be to also move the web server writableimages directory outside of the document root, renaming it to something unguessable (e.g. the MD5 hash of <whatever>) and streaming the images viaimg_auth.php, so that the real directory name never ever shows up.
To accomplish that follow these steps:
images/upload directory outside of (in parallel to) your document root (note the/.. at the end of the path):cd</absolute/path/to/your/doc_root>/..mkdir<dir_name_unguessable>chgrp<your_web_server_group><dir_name_unguessable>chmod770<dir_name_unguessable>.ht* files normally, but just in case this directory ever will be made available to the web server directly):cd<dir_name_unguessable>echo'php_admin_flag engine off'>.htaccesschmod444.htaccess
$wgUploadPath="$wgScriptPath/img_auth.php";$wgUploadDirectory='</absolute/path/to/your/doc_root>/../<dir_name_unguessable>';$wgEnableUploads=true;# We don't wanna restrict access, just make our MW install more secure$wgWhitelistRead=false;which should do the right thing without any additional configuration.
That should do the job for web servers with PHP running as an Apache module.No further Apache config file changes are necessary.You then will never see the path to your images,img_auth.php intercepts all read accesses.But all of your images are served, including thumbs.
If you use CGI or IIS your milage may vary.
In your [/path/to]/images directory, create an .htaccess containing one line:
Deny from All
LocalSettings.php. This is not needed if Apache step 2.2 is done.$wgUploadPath="[/path/to]/img_auth.php";
[/path/to] is the URL path, not the file system path, so if img_auth.php is in/usr/share/mediawiki but is accessed ashttp://example.org/mediawiki/img_auth.php, the line would read:
$wgUploadPath="/mediawiki/img_auth.php";
Be sure to add a leading slash/ ifimg_auth.php is actually in your root-directory.Images won't be displayed at all if you forget to do so:
$wgUploadPath="/img_auth.php";
Edit thehttpd.conf file and add the two following aliases:
Alias[/path/to]/images/[/path/to]/img_auth.php/Alias[/path/to]/images[/path/to]/img_auth.php
The second [/path/to] on each line should be the absolute path on the file system, and it may be necessary to add a trailing frontslash to img_auth.php (i.e., use [/path/to]/img_auth.php/).
When PATH_INFO is not available download theCGI-supporting image authorization script.Save script under the namecgi_img_auth.php in your MediaWiki directory.
In your[/path/to]/images directory, create an.htaccess containing one line:
DenyfromAll
$wgUploadPath="[/path/to]/cgi_img_auth.php";
Edit the .htaccess to look like this
RewriteEngineonRewriteRule^/path/to/images(.*)$/path/to/cgi_img_auth.php/$1[R]RewriteRule^path/to/cgi_img_auth.php/(.*)$path/to/cgi_img_auth.php?path=/$1
Note, however, this step isunnecessary on some installations.
If your website is rewriting URLs through.htaccess, then you will need an exception before the custom rewrites:
RewriteCond%{REQUEST_URI}/img_auth\.php/RewriteRule^-[L]
(This means:in case of img_auth called, stop rules)
Example of .htaccess:
RewriteEngineOn# First condition&rule:RewriteCond%{REQUEST_URI}/img_auth\.php/RewriteRule^-[L]# Rest of rules:RewriteCond...RewriteRule...
If you don't want user to list your images folder set this up on your Apache configuration:
<Directory/var/www/wiki/images>Options-Indexes</Directory>
Implementation in IIS is more complex because it lacks the inherent 'pipe' capabilities of Apache or Unix in general.However using a few tricks, IIS can be made to execute the CGI and achieve protection.
| The img_auth approach ONLY works with the PHP ISAPI mode on the WIMP (Windows, IIS, MySQL, PHP) platform. If you followed thestandard Windows installation instructions, your wiki will be using CGI (php-cgi.exe) and you will have to convert to ISAPI. Instructions for doing so are in thediscussion area. There is a bug in the php-cgi approach in parsing PATH_INFO that will show up as "CGI Error: The specified CGI application misbehaved by not returning a complete set of HTTP headers" on some files in the restricted path. This is a PHP/IIS issue, not a MediaWiki problem. |
With IIS it is important that users cannot access images or files by using alternative URL paths to the bypass the virtual directory redirect.Therefore, a new directory outside the MediaWiki root must be created.
Create a new physical directory.This directory should not be inside any other existing web directories or virtual web directories:
Example:
c:\inetpub\wwwroot\MyWikiImg
The Directory security must allow read, write, modify for the Internet Guest Account (usually IUSR_[server name]).Don't worry, you're going to regulate this in subsequent steps.
In IIS this is done by creating a virtual directory with the same name as the physical directory (if your directory is off the root web).
Create a new virtual directory using Start->Administrative Tools->Internet Information Services (IIS) Manager in the web service you are using for MediaWiki.
Right click on the web service->New Virtual Directory...
In the wizard, create a new virtual directory with the same name as the physical directory and point it to that directory.
Still in IIS Manager, right click on the new virtual directory->Properties select the 'Virtual Directory' tab and change the 'The content for this resource should come from:' to 'A redirection to a URL'.Fill in the 'Redirect To:' with the URL to img_auth in your MediaWiki.
Example:
http://wiki.example.org/MyWiki/img_auth.php
Remember to Click Apply!
Copy the contents of the old images directory ($ip/image) and subdirectories into the new directory you created.Note: Theimage directory will not exist in the new directory.The new directory should not appear as:
Wrong:
MyWikiImg images 0 1 . . .
Right:
MyWikiImg 0 1 . . .
$wgUploadPath="[NewVirtualDirPath]";
Example:
$wgUploadPath="/MyWikiImg";
$wgUploadDirectory="[NewVirtualDirImages]";
Example:
$wgUploadDirectory="D:\Inetpub\wwwroot\MyWikiImg";
If your installation is not working, it may be because img_auth.php requires the server to return PATH_INFO to know exactly which file you wish to access (e.g., everything in the URL after the virtual directory).
There have been several articles and hints that some versions of IIS may disallow the server variables PATH_INFO and PATH_TRANSLATE 'for security reasons'.While we did not have this problem on the current server and patch level (IIS 6.0) it is a noted issue for IIS 4.0 (and possibly prior), you may want to investigate if img_auth.php is not working for you.
The full knowledgebase article may be found atUsing PATH_INFO and PATH_TRANSLATED from CGI Applications.The article instructs you on how to run a program written in MS Visual Basic (you may need to load CScript).