Make WordPress Core

Opened 4 years ago

Last modified 8 days ago

#57081 new defect (bug)

test for wp_mkdir_p

Reported by: pbearne Owned by:
Priority: normal Milestone: 7.2
Component: General Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses: tests

Description (last modified by pbearne)

Added test
and hardened the function

Change History (16)

This ticket was mentioned in ​PR #3610 on ​WordPress/wordpress-develop by ​@pbearne.


4 years ago
#1

  • Keywords has-patch has-unit-tests added

Added test
and hardened the function

Trac ticket: 57081

#2 @pbearne
4 years ago

  • Description modified (diff)

Added test
and hardened the function

#3 @pbearne
4 years ago

I hardened the function will revert the changes if need

#4 @desrosj
6 months ago

  • Focuses tests added

Adding the tests focus, which is used to indicate a ticket is solely focused on adding tests.

#5 @pbearne
4 months ago

  • Milestone Awaiting Review → 7.1

This ticket was mentioned in ​Slack in #core by adrianduffell. ​View the logs.


4 months ago

​@adrianduffell commented on ​PR #3610:


4 months ago
#7

@pbearne It looks like there is an issue in the PHP 7.4 checks. Could you please take a look?

​@wildworks commented on ​PR #3610:


4 months ago
#8

@pbearne, Can you address the feedback from Copilot? Both seem like reasonable suggestions.

#9 @wildworks
3 months ago

  • Milestone 7.1 → 7.2

I'd like to punt this as 7.1 RC1 is imminent.

#10 @pbearne
2 months ago

updated to meet #65208 standards

Last edited 2 months ago by pbearne (previous) (diff)

This ticket was mentioned in ​PR #15033 on ​WordPress/wordpress-develop by ​@dhrupo.


3 weeks ago
#11

Use unique directory names and ensure cleanup in tear_down().

This addresses feedback from the review of PR #3610.

​@lancewillett commented on ​PR #15033:


2 weeks ago
#12

The existing-directory test returns before mkdir(), so it does not exercise the new mkdir()-fails-but-directory-exists branch. The added tests cover ordinary behavior, not the race fix. Could you provide a focused reproduction of that failure and the patched result? Avoid adding a large filesystem mock solely to justify this small change.

<sub>AI review · <code>gpt-6</code></sub>

​@dhrupo commented on ​PR #15033:


2 weeks ago
#13

Both fixed in 5fc11dc04cff3c5e8c2b0e955395f7752547a7c0.

Permissions on a directory this call did not create. You're right. is_dir() inside the condition meant the permission block ran for a directory another process created, and that block's loop walks every component below $target_parent, so it could widen more than the target itself. The fallback is now after the block:

if ( @mkdir( $target, $dir_perms, true ) ) {
                // ... permission adjustment, unchanged ...
                return true;
        }

        /*
         * Another process may have created the directory since the check above. Accept
         * it, with the permissions that process set: this call did not create it.
         */
        return is_dir( $target );

The net change against trunk is now that one return.

The race cannot be produced in-process on a real filesystem: a dangling symlink, an unreadable parent and a stat-cache trick all end in false rather than the "mkdir failed but the directory is there" state, and WP_Test_Stream reports a directory as soon as its trailing-slash key is set, so file_exists() returns first. So the regression test points wp_mkdir_p() at a wrapper whose mkdir() fails and reports the directory present from that call onwards. It counts permission changes instead of applying them, so it asserts the behaviour rather than OS permission bits and reads the same on Windows, and it asserts the race path ran so it cannot pass without exercising it. On the previous commit it fails with Failed asserting that 1 is identical to 0.

One thing worth flagging rather than folding in: recursive mkdir() can create intermediate directories and still return false — @mkdir( "$base/x/y", 0444, true ) returns false with $base/x created. In the two-uploads-at-month-start case, a process that creates uploads/2026 and then loses the race on uploads/2026/11 leaves uploads/2026 at umask-reduced bits. Trunk already skips the permission block on every mkdir() failure, so that part is not new; what this patch changes is that the call now reports success, so the state is no longer surfaced as "Unable to create directory". Distinguishing the components this call actually created would mean recording them before the call or creating level by level, which is more than this ticket needs — happy to open a separate ticket for it if you agree it is worth one.

Traversal targets. Also right: an existing path is reported before traversal is rejected, so ../wp-admin could pass for the wrong reason. The rejection cases now build on a unique component asserted missing first, and the test asserts nothing was created:

$missing = 'wp-mkdir-p-missing-' . wp_generate_password( 6, false );
$this->assertFileDoesNotExist( ABSPATH . $missing, '...' );

Relative targets are still covered, using that same component, since it cannot exist relative to any working directory — otherwise every remaining target would be ABSPATH-prefixed and a guard that only matched after an absolute prefix would pass. The precedence you mentioned is kept as its own test, named for what it asserts:

public function test_existing_target_with_a_traversal_segment_is_accepted() {
        $this->assertTrue( wp_mkdir_p( ABSPATH . 'wp-admin/../wp-admin' ) );
}

No test matrix added.

Tests: WpMkdirPTest 7 tests / 17 assertions; --group functions 1272 tests, no failures; upload.php, file.php, media.php and image/functions.php all pass. PHPCS clean on both changed files.

One loose end from my side: an earlier commit on this branch moved the test to tests/phpunit/tests/wp-includes/functions/, which is the only file under that tree, while tests/phpunit/tests/functions/ holds the other ~80. Happy to move it back if you prefer the conventional location.

AI assistance: Claude Code (Claude Opus 5) for the patch, the tests and this comment. I reviewed every line, ran the tests, and verified each claim against core myself.

#14 @lancewillett
2 weeks ago

In 65175:

Filesystem: Handle concurrent directory creation in wp_mkdir_p().

Accept a directory created by another process after mkdir() fails, without changing its permissions. Add coverage for directory creation, existing paths, traversal rejection, and the race fallback.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/15033

Props pbearne, wildworks, dhrupo.
See #57081.

#16 @lancewillett
8 days ago

In 65329:

Tests: Move the wp_mkdir_p() tests to the current folder layout.

Move the tests added in [65175] to tests/phpunit/tests/functions/, without a namespace, to match the guidance for new tests during 7.2 on #65208. Rename the race test's stream wrapper class to match.

Follow-up to [65175].

See #57081, #65208.

Note: See TracTickets for help on using tickets.