What security concerns should I have when setting FS_METHOD to "direct" in wp-config?

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.


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.

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

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.

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.

Similar questions

Preview and posting images from front-end with WordPress security concerns
I've a front-end form with a file input where anybody (no registered users) can upload an image that will be attached to a custom meta field in the back-end. To preview the image I'm using the old iframe technique. My form looks like this: Then I use WordPress built-in functions to handle the upload and move the file into the media gallery. I use t...
First Website - Security Concerns
I have taught myself HTML/CSS and some JavaScript as a hobby, and have reached the point where I am comfortable building a clean simple website. The company I work for (we do nothing related to coding) has a website that is quite outdated so naturally I saw this as an opportunity for my first live site. I approached my Managers at work to take a lo...
The reason to check for invalid UTF-8, convert single less than signs, and strips octets for security concerns
I'm searching about sanitizing user input text-area field on Wordpress. I found several sanitizing functions, but there's are some different between functions. I wonder the one of sanitizing function' feature, sanitize_text_field( string $str ) First of all, I wonder the reason "Checks for invalid UTF-8" Why Invalid UTF-8 to be sanitized? Second, I...
Concerns over wp-config file
I have been tasked with investigating a website that has been reported as abusive, I had no prior experience with this website and have no idea why or how things are set up as they are, in my investigations I have found multiple wp-admin folders with numbers on the end of them and a wp-config file that contains a lot of code that I don't believe sh...
What concerns should I be aware of when hosting Wordpress/PHP on IIS 7.5 and Server 2008 R2?
I am considering moving a Wordpress install to a server running Server 2008 R2. It seems that Microsoft has done some to make running PHP more palatable, but I really haven't seen any recent data or real life test cases.
Moving wp-config.php outside root folder where we have multiple wordpress websites for enhanced security
I have moved wp-config.php but my file structure is as given below: abcom and applecom folder has wordpress websites both As advised, I have moved wp-config.php outside abccom for security, but now when I need to do the same for applecom, how can I achieve it? https://webdesign.tutsplus.com/tutorials/how-to-secure-your-wordpress-wp-configphp--cms-2...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.