Make WordPress Core

Opened 10 months ago

Last modified 3 weeks ago

#64519 new defect (bug)

WP_Filesystem_FTP and WP_Filesystem_FTPext classes create orphaned temp/noise files

Reported by: lucianwpwhite Owned by:
Priority: normal Milestone: Awaiting Review
Component: Filesystem API Version: 6.9
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

The WP_Filesystem_FTP and WP_Filesystem_FTPext classes create temporary files during put_contents operations, but if the FTP connection is invalid or null, the method throws an exception without cleaning up the temp file, resulting in orphaned files in tmp.on busy sites you can easily end up with 100K files in matter of days not to mention the fact that the file name provided is not respected.

For example lets say you pass/var/www/html/wp-content/uploads/plugin/index.php

If ftp is not configured you will end up with:/tmp/plugin-{random string}.php

And then the FTP will throw exception and the file will remain

Change History (4)

#1 @lucianwpwhite
10 months ago

If FTP is not configured you will end up with:/tmp/plugin-{random string}.tmp

sorry for the typo in the ticket description - I said the extension is .php but it's actually .tmp

This ticket was mentioned in ​PR #14891 on ​WordPress/wordpress-develop by ​bluefairydevelopment.


3 weeks ago
#2

  • Keywords has-patch has-unit-tests added

WP_Filesystem_FTPext::put_contents() writes the contents to a temporary file from wp_tempnam() and then uploads it with ftp_fput(). When there is no FTP connection ($this->link is null or false), or the connection has been closed, ftp_fput() throws on PHP 8, so the fclose() / unlink() cleanup never runs. As the reporter describes, busy sites can accumulate very large numbers of *.tmp files in the temp directory.

This change:

  • returns false early when there is no connection, matching get_contents(), which already has this check, so no temporary file is created at all;
  • moves the cleanup into a finally block around ftp_fput(), so the temporary file is removed even if the upload throws, for example on a closed connection. The exception still propagates as before.

Tests: new tests/phpunit/tests/filesystem/wpFilesystemFtpext/putContents.php.

  • test_put_contents_without_connection_leaves_no_temp_file (null and false links): returns false and leaves no temporary file.
  • test_put_contents_on_closed_connection_leaves_no_temp_file: opens a real connection to a minimal local FTP responder, closes it, then calls put_contents(). It requires the ftp extension and does not need a real FTP server.

Each part of the fix was checked separately:

  • Trunk: all three tests fail.
  • With only the early return: the closed-connection test still fails.
  • With the full change: all pass, and --group filesystem shows no new failures. Its 20 warnings are the same PHPUnit 10 deprecation notices trunk already has.
  • phpcs is clean.

Not in this PR: WP_Filesystem_FTPext has other methods that use $this->link without a connection check, and the reporter's point that the temporary file name doesn't match the target file comes from wp_tempnam(), which is shared by every filesystem method. Both are left for follow-up to keep this change focused.

Props lucianwpwhite.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: reading the ticket and the code, the fix, and the tests, including confirming that each test fails without the corresponding part of the fix. Submitted by Blue Fairy Development, who are responsible for the change.

#3 @bluefairydevelopment
3 weeks ago

PR with fix and unit tests: ​https://github.com/WordPress/wordpress-develop/pull/14891. Returns early without a connection and removes the temp file in a finally block, so a closed connection no longer leaks it either. Props lucianwpwhite for the report.

​@bluefairydevelopment commented on ​PR #14891:


3 weeks ago
#4

The closed-connection test failed on PHP 7.4: before PHP 8.1 an FTP connection is a resource, and using a closed one raises a warning rather than throwing an Error. That test now requires PHP 8.1 (8c04ab6). The early-return tests still run on all versions.

Note: See TracTickets for help on using tickets.