Skip to content

test for wp_delete_file_from_directory and update to Function to stop… - #5603

Open
pbearne wants to merge 6 commits into
WordPress:trunkfrom
pbearne:59781-wp_delete_file_from_directory
Open

pbearne wants to merge 6 commits into
WordPress:trunkfrom
pbearne:59781-wp_delete_file_from_directory

Conversation

@pbearne

@pbearne pbearne commented Oct 31, 2023

Copy link
Copy Markdown

… stream URL failing

Trac ticket: https://core.trac.wordpress.org/ticket/59781

@mindctrl mindctrl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Just a couple spelling nits.

@mindctrl

Copy link
Copy Markdown
Member

@pbearne I think this is good to go once the spelling nits are fixed. Could you bring it up to date with trunk and I'll give it another final whirl?

@github-actions

github-actions Bot commented Jul 23, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props pbearne, mindctrl, lancewillett.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Co-authored-by: John Parris <public@johnparris.com>
Copilot AI review requested due to automatic review settings July 27, 2026 20:10
@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds regression coverage and tightens stream-wrapper handling for wp_delete_file_from_directory() to address Trac #59781 (“stream URL failing”) in core’s filesystem deletion helper.

Changes:

  • Adds a new PHPUnit test class covering success, missing-file, file:// stream, non-file:// stream, and wrong-directory cases.
  • Updates wp_delete_file_from_directory() to reject non-file:// stream wrappers early when $file is a stream.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php Introduces test coverage for stream and non-stream deletion behavior.
src/wp-includes/functions.php Adds a guard to disallow non-file:// stream wrappers for the $file argument.
Comments suppressed due to low confidence (2)

tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php:55

  • Typo in docblock: "unsorted" should be "unsupported" (this test is about a non-file:// stream wrapper).
	/**
	 * Call with unsorted stream file.
	 *
	 * @ticket 59781

src/wp-includes/functions.php:7566

  • Extra blank line inside this guard clause deviates from the surrounding style (and is the only instance of this pattern in functions.php), which may fail whitespace/style checks.

	if ( defined( 'IFRAME_REQUEST' ) ) {
		return;
	}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +17 to +19
$file = realpath( DIR_TESTDATA ) . '/temp_file.txt';
$directory = realpath( DIR_TESTDATA );
touch( $file );
Copilot AI review requested due to automatic review settings July 27, 2026 20:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php:9

  • Test class name casing is inconsistent with other tests/phpunit/tests/functions/* tests (typically Tests_Functions_*). This makes the suite harder to navigate/search consistently.
class Tests_functions_wpDeleteFileFromDirectory extends WP_UnitTestCase {

Comment on lines 7881 to +7885
if ( wp_is_stream( $file ) ) {
$real_file = $file;
if ( ! str_starts_with( strtolower( $file ), 'file:' ) ) {

return false;
}

@lancewillett lancewillett left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The source guard rejects every registered stream wrapper except file:, although the existing function passes supported wrappers through its containment check and deletion. Please preserve that contract, or establish the intended compatibility change separately. The php:// plus local-path example does not justify rejecting working custom filesystem wrappers. Please also give the temporary file cleanup that runs after assertion failures.

AI review · gpt-6

@mindctrl mindctrl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Copilot review looks correct to me. Aside from that, I think it would be good to have a test that confirms a file can't be deleted outside the folder by using path traversal.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: John Parris <public@johnparris.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The new guard rejects deletable non-file stream wrappers, which can cause attachment files stored through those wrappers to be left behind.

2 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Preserve deletion support for registered stream wrappers

src/​wp-includes/​functions.php:7887

This guard rejects registered non-file wrappers that implement unlink(), so a contained path such as custom://bucket/uploads/image.jpg now returns false without attempting deletion. Because wp_delete_attachment_files() uses this helper, attachment files stored through these wrappers can be left behind. Preserve support for deletable wrappers by letting the existing directory check and wp_delete_file() determine success.

5 resolved since last review

Review effort: Balanced

@lancewillett lancewillett left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current head still rejects registered filesystem wrappers that could delete files before this change. In a temporary-file check using the same registered wrapper and call on both revisions, the base returned true and deleted the file. This head returned false and left it in place. Please preserve working wrappers and keep any intentional change to that filesystem contract in a separate proposal.

The revised php://temp test uses the same URL as both file and directory. The existing directory-prefix check already rejects that input, so it does not demonstrate a need for the new guard. Please also use unique temporary files and clean them up even when an assertion fails. The existing path traversal review remains open.

AI review · gpt-6.2-astra

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants