Problem/Motivation

\Drupal\Core\File\FileSystem::deleteRecursive() can result in data loss due to symlinks to directories outside of the directory being deleted. If the link is to a file required for the running of your site this could result in data loss or the site breaking.

Additionally, some kernel tests override \Drupal\KernelTests\KernelTestBase::setUpFilesystem() to use a real file system for testing things that cannot be tested on vfs. For example any test that extends \Drupal\KernelTests\Core\File\FileTestBase. These tests do not currently clean-up after themselves. This was noted in #3482283: Symlinking a module breaks HookCollectorPass

Steps to reproduce

Run the new test added in the MR.
Run \Drupal\KernelTests\Core\Archiver\ZipTest and see the directory left in sites/simpletest

Proposed resolution

Fix deleteRecursive to work with symlinks correctly.
Add functionality to KernelTestBase to do the cleanup if necessary

Remaining tasks

User interface changes

N/a

Introduced terminology

N/a

API changes

N/a

Data model changes

N/a

Release notes snippet

N/a

Issue fork drupal-3482449

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

alexpott created an issue. See original summary.

nicxvan’s picture

Do we want a flag to allow them to stay for local debugging if you need to see the actual files created for some reason?
And maybe a way to confirm that cleanup happened so future tests have to clean up after themselves.

alexpott’s picture

Status: Active » Needs review

@nicxvan no - we don't offer that in WebDriverTestBase which does the same thing. You can always preserve the environment by adding an exit() to the test.

FWIW the fix to \Drupal\Core\File\FileSystem::deleteRecursive() almost makes me want to make this issue about that and not KTB and tidying up. This feels like a serious data destruction bug - we should not be recursing into links and removing files...

nicxvan’s picture

exit() makes sense, my only thought was sometimes you might need to see more broadly, but that is probably super edge case and not worth the effort.

alexpott’s picture

Title: Cleanup real files in KernelTestBase tests » \Drupal\Core\File\FileSystem::deleteRecursive() will follow symlinks and remove files outside the directory it is deleting
Priority: Normal » Major
Issue summary: View changes

Changing the focus of the issue to be the data loss bug discovered here by trying to cleanup the new \Drupal\KernelTests\Core\Hook\HookCollectorPassTest::testSymlink() test.

alexpott’s picture

I extended test coverage to include links to files outside of the directory being deleted. This is not currently broken but it feels good to have a test for.

alexpott’s picture

So added some test coverage of the expanded delete functionality. Which opens up more worms because of the way that streams work with links and resolve to the real file or directory and not link. Which imo results in very odd behaviour when you call delete.

nicxvan’s picture

I took a look at this again, it seems pretty straightforward.

The only question I have is this worth a comment mentioning the risk to consider if this gets changed in the future?
Unless you think the comment for the tests is enough: Tests symlinks in directories do not result in unexpected deletions.
It might be helpful for people using this as example code for themselves to see what to consider.

alexpott’s picture

@nicxvan I think the additional test coverage added here is fine.

smustgrave made their first commit to this issue’s fork.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Rebased because it was 190 commits back.

Funny I got 6 random errors bur re-running twice fixed those

All the new tests appear to fail in the test-only job https://git.drupalcode.org/issue/drupal-3482449/-/jobs/3688988

Dont't see any open threads believe this is good.

  • catch committed addee909 on 10.5.x
    Issue #3482449 by alexpott, smustgrave, nicxvan: \Drupal\Core\File\...

  • catch committed ed78b812 on 11.x
    Issue #3482449 by alexpott, smustgrave, nicxvan: \Drupal\Core\File\...
catch’s picture

Version: 11.x-dev » 10.5.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x and cherry-picked to 10.5.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

cilefen’s picture