Wordpress - What security concerns should I have when setting FS_METHOD to "direct" in wp-config?
This is just, how I understood the idea of the WordPress File API. If it is wrong, please downvote :)
Okay. If you upload a file, this file has an owner. If you upload your file with FTP, you login and the file will be owned by the FTP user. Since you have the credentials, you can alter these files through FTP. The owner can usually execute, delete, alter etc. the file. Of course, you can change this by changing the file permissions.
If you upload a file using PHP, the linux user, which is executing PHP is owning the file. This user can now edit, delete, execute etc. the file. This is okay as long as only you are the user, who is executing PHP on your system.
Lets assume, you are on a "poorly" configured shared host. A lot of people run their PHP websites on this system. Lets say only one linux user is executing PHP for all these people. One of the webmasters on this shared host has bad intentions. He sees your page and he figures out the path to your WordPress installation. For example, WP_DEBUG is set to true and there is an error message like
[warning] /var/www/vhosts/userxyz/wp-content/plugins/bad-plugin/doesnt-execute-correctly.php on line 1
"Ha!" the bad boy says. Lets see, if this guy has set FS_METHOD
to direct
and he writes a script like
<?php
unlink( '/var/www/vhosts/userxyz/wp-content/plugins/bad-plugin/doesnt-execute-correctly.php' );
?>
Since only one user is running PHP and this user is also used by the bad boy he can alter/delete/execute the files on your system if you have uploaded them via PHP and by this attached the PHP user as the owner.
Your site is hacked.
Or, as it says in the Codex:
Many hosting systems have the webserver running as a different user than the owner of the WordPress files. When this is the case, a process writing files from the webserver user will have the resulting files owned by the webserver's user account instead of the actual user's account. This can lead to a security problem in shared hosting situations, where multiple users are sharing the same webserver for different sites.
What's the risk?
On a poorly configured shared host, every customer's PHP will execute as the same user (let's say apache
for discussion). This setup is surprisingly common.
If you're on such a host and use WordPress to install the plugin using direct file access, all of your plugin files will belong to apache
. A legitimate user on the same server would be able to attack you by writing a PHP script that injects evil code into your plugin files. They upload their script to their own website and request its URL. Your code is successfully compromised because their script runs as apache
, the same one that owns your plugin files.
What does FS_METHOD 'direct'
have to do with it?
When WordPress needs to install files (such as a plugin) it uses the get_filesystem_method() function to determine how to access the filesystem. If you don't define FS_METHOD
it will choose a default for you, otherwise it will use your selection as long as it makes sense.
The default behavior will try to detect whether you're in an at-risk environment like the one I described above, and if it thinks you're safe it will use the 'direct'
method. In this case WordPress will create the files directly through PHP, causing them to belong to the apache
user (in this example). Otherwise it'll fall back to a safer method, such as prompting you for SFTP credentials and creating the files as you.
FS_METHOD = 'direct'
asks WordPress to bypass that at-risk detection and always create files using the 'direct'
method.
Then why use FS_METHOD = 'direct'
?
Unfortunately, WordPress' logic for detecting an at-risk environment is flawed and produces both false-positives and false-negatives. Whoops. The test involves creating a file and making sure it belongs to the same owner as the directory it lives in. The assumption is that if the users are the same, PHP is running as your own account and it's safe to install plugins as that account. If they're different, WordPress assumes that PHP is running as a shared account and it's not safe to install plugins as that account. Unfortunately both of these assumptions are educated guesses that will frequently be wrong.
You would use define('FS_METHOD', 'direct' );
in a false positive scenario such as this one: you are part of a trusted team whose members all upload files through their own account. PHP runs as its own separate user. WordPress will assume that this is an at-risk environment and will not default to 'direct'
mode. In reality it's only shared with users you trust and as such 'direct'
mode is safe. In this case you should use define('FS_METHOD', 'direct' );
to force WordPress to write files directly.