I have recently had an issue where I have been unable to install the WP Smush Pro plugin because I don't have the Manual Install or One-Click Installation options available.

I came across this post which suggested tweaking the settings in wp-config.php. I added the settings suggested, however the one that seems to be the most important is:

define('FS_METHOD', 'direct');

What I would like to know is what real concerns should I have around setting FS_METHOD to direct? Are there any other alternatives to installing the plugin?

This is what the official documentation has to say:

FS_METHOD forces the filesystem method. It should only be "direct", "ssh2", "ftpext", or "ftpsockets". Generally, you should only change this if you are experiencing update problems. If you change it and it doesn't help, change it back/remove it. Under most circumstances, setting it to 'ftpsockets' will work if the automatically chosen method does not.

(Primary Preference) "direct" forces it to use Direct File I/O requests from within PHP, this is fraught with opening up security issues on poorly configured hosts, This is chosen automatically when appropriate.

up vote 19 down vote accepted

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.

There is a 'well-configured' situation where 'direct' will lead to problems.

It is also possible to configure shared WP hosting with non-shared PHP execution users, different from the file/directory ownership users. So you end up with the files owned by user1 and the PHP code is executed as php-user1.

In that situation, hacked plugins or core code (a) can't write to (or even read from, depending on permissions) other users' directoriess; (b) can't write this user's files and so can't add trojan code to the core or plugin code.

So if the hosting is set up like that, you MUST use FTP for updates and 'direct' will not work.

If you set 'direct' in wp-config.php and the PHP execution user does not have write permission, you'll get Update Failed messages and have no pop-up asking for FTP credentials.

I thoroughly checked file system matters. Most people, I guess use the 'suexec' paradigm: one ftpuser, one webuser (alias apache, wwwuser, http or similar). Both typically being in the same group.

If the webuser directly triggers the download and than placing the files (thus he becomes the owner), I don't think anything is wrong with that.

If you still want to change files (i.e. that came in by updates) from your ftpuser (read: by your ftp client), make sure to have good default file rights (the reverse of umask) in your wp-config:

define('FS_CHMOD_DIR', 0770);
define('FS_CHMOD_FILE', 0660);

Keeping the world (other guys on your shared hosting, possible intruders) out as much as possible, but allowing 'the other one' of your two account, to edit the file, too. (A much better strategy than continued was of time on chown and chmod, from my experience)

In short: I don't see a security flaw in direct method.

In fact, it's a good solution, if method 'ftpext' fails, likely because of lacking FTP configuration by your hosting provider. (Telltale sign, that many people seem to have: 'PHP Warning: array_keys() expects parameter 1 to be array, boolean given'))

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.