Problem/Motivation

Currently the VendorHarderingPlugin is only able to process packages installed in the vendor directory. That very strongly limits it's usage. It would be nice if it could harden Composer packages in any directories. For instance Drupal core ships with 4 MB demo_umami installation profile. Given it's a demo profile why should any Drupal site have it on production?

Proposed resolution

Process all packages regardless of their location.

$composer = $event->getComposer();
$installation_manager = $composer->getInstallationManager();
$package_path = $installation_manager->getInstallPath($package);
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Chi created an issue. See original summary.

Chi’s picture

Issue summary: View changes
Mile23’s picture

The vendor hardening plugin does, indeed, assume that the package is in the vendor directory. https://git.drupalcode.org/project/drupal/blob/8.8.x/composer/Plugin/Ven...

If the package is symlinked then that works to remove the cleanup directories regardless where the real directory lives.

I'm not entirely sure we can get the new package location from composer/installers, but it should be possible.

Also, in the case of drupal/core, there's this issue: #2385395: Make Drupal core folder agnostic and allow it to be placed in vendor/drupal/core

Mile23’s picture

Status: Active » Needs review
FileSize
9.81 KB

OK, given that cleanup happens in post-comand and post-update/install events, composer/installers has always already modified the installation path. This means we can use the installation manager to get that path.

Here's a patch which does this.

Edit:

+++ b/composer/Plugin/VendorHardening/VendorHardeningPlugin.php
@@ -257,20 +263,23 @@ public function cleanAllPackages($vendor_dir) {
+    /* @var $packages_to_be_cleaned \Composer\Plugin\PluginInterface[] */
+    $packages_to_be_cleaned = array_intersect_key($cleanup_paths, $installed_packages);

Need to fix this. The inline doc is wrong. It's an array of strings.

Mile23’s picture

Chi’s picture

Mile23’s picture

Marked that one duplicate.

Mile23’s picture

FileSize
10.19 KB
1.65 KB

Updated docblocks, fixed inline documentation.

Chi’s picture

I think README.txt needs to be updated.

Mile23’s picture

Updates the readme.

This issue is targeted for 8.9.x, but it would be great to have it in 8.8.3+. This is a new behavior, but it's not exactly a new feature. We can consult findings in #3103918: [policy + docs] Decide on backwards compatibility policy for Composer plugins in Drupal 8 to figure out when this gets unleashed.

Chi’s picture

Status: Needs review » Reviewed & tested by the community

Works as expected. I was able to remove directories from Drupal core and contributed modules.

Chi’s picture

Does this need a change record?

Mile23’s picture

Issue tags: +needs documentation updates

Added a CR: https://www.drupal.org/node/3112213

Adding needs docs update tag because we'll need to change that once we know which versions of core support this behavior. We'll probably also need to update the CR with this version information if we get a backport to 8.8.x.

andypost’s picture

Issue tags: -needs documentation updates +Needs documentation updates

Looking at docs it looks totally fine https://www.drupal.org/docs/develop/using-composer/using-drupals-vendor-...

PS: guess only CR will need version update

AdamPS’s picture

Great many thanks @Mile23.

For anyone that wishes to apply this patch using composer and cweagans/composer-patches note that you will need to edit the patch first. Search and replace /composer/Plugin/VendorHardening with blank and delete the test change at the end.

I would support backporting to 8 as this removes a significant limitation in the existing code. I am using it to remove Core tests and the demo/testing profiles.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 10: 3108262_10.patch, failed testing. View results

andypost’s picture

Version: 8.9.x-dev » 9.1.x-dev
Status: Needs work » Needs review
FileSize
12.05 KB

re-roll for 9.1.x

andypost’s picture

piggito’s picture

I couldn't apply last patch using cweagans/composer-patches cause it includes changes in core tests which are part of a different composer package.

Therefore, I'm attaching a new patch which just removes test changes so that we can apply it with cweagans/composer-patches by adding

"patchLevel": {
  "drupal/core": "-p2",
  "drupal/core-vendor-hardening": "-p4"
},
andypost’s picture

Status: Needs review » Needs work
greg.1.anderson’s picture

This came up in discussion again recently. I am a weak -1 on this enhancement. If we supported removal of dev files from non-vendor locations, then modules might also use this feature for security, which would mean that sites based on drupal/recommended-project would have to start using the vendor hardening tool as well.

A better solution would be for modules to mode their dev bits into a Composer dependency installed in the vendor directory.

I do realize that a lot of modules want to work in both Composer and non-Composer environments, so this might be an additional burden for the non-Composer case. However, requiring Composer for development only doesn't seem too bad to me.

jungle’s picture

Drupal core ships with 4 MB demo_umami installation profile. Given it's a demo profile why should any Drupal site have it on production

@andypost in slack: In a long run it would be great to decouple tests our of core, but package 10 extra megabytes of umami to live sites also needs work

At least, it‘s worthy allowing to remove demo_umami profile optionally.

jungle’s picture

https://github.com/skilld-labs/drupal-cleanup, @andypost shared the composer plugin in slack, which is able to remove "profiles/demo_umami"

AdamPS’s picture

A better solution would be for modules to mode their dev bits into a Composer dependency installed in the vendor directory.

True but it depends on how the plug-in is being used. Here are two ways of using it where that won't work.

  1. I use the hardening plug-in to remove "junk" files from modules that I don't maintain. Do you see this as a valid usage? Is there a better way?
  2. #3067979: Exclude test files from release packages Suggests an option to use the plug-in to remove test files from core, and it's unlikely that core would split the codebase.
greg.1.anderson’s picture

#24.1: That's reasonable. I still feel that module maintainers should try to move away from shipping dev components, and rely on require-dev instead.

#24.2: Long term, all of core should move to vendor. This will happen eventually, but I don't know how long it will take.

Mile23’s picture

If we supported removal of dev files from non-vendor locations, then modules might also use this feature for security.

+1 thank you, sounds great. :-)

I'm currently using oomphinc/composer-installers-extender to build a libraries/ directory on a D7 site. I'd love to be able to remove test directories from those packages.

which would mean that sites based on drupal/recommended-project would have to start using the vendor hardening tool as well.

I'm not sure how that follows. You could optionally add vendor-hardening after you've created your project if you want to.

A better solution would be for modules to mode their dev bits into a Composer dependency installed in the vendor directory.

If we're going there, then the solution is for extensions to be able to live in vendor/.

greg.1.anderson’s picture

Yeah I guess we need this, because it will take a while before core and extensions live in vendor, and we probably won't get half solutions very quickly either.

Mile23’s picture

Status: Needs work » Needs review

I guess that leads us back to #18 needing review. #19 is bespoke for people to patch their core.

andypost’s picture

fgm’s picture

Tangentially related is #3165183: Do not remove vendor/bin/composer. Discussion on #drupal with @andypost and @Mile23 suggest we might want to allow modifying the removal list, both to add and remove cleaning targets, not just add more.

Mile23’s picture

This issue is about 'hardening' any installed package, regardless of whether it's under vendor/.

Changing the way the plugin is configured would be out of scope, so add a follow-up for that.

fgm’s picture

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

AdamPS’s picture

Status: Needs review » Reviewed & tested by the community

I confirm that I used this patch and it worked for me. I don't understand composer enough to comment on the details of the code in the patch - however the maintainer/commiter will have that knowledge. It seems fair to set RTBC on this basis, let's see if we can get this useful patch in.

catch’s picture

Status: Reviewed & tested by the community » Needs work

#18 isn't passing - custom commands failed.

Pooja Ganjage’s picture

Hi,

Creating a patch as suggested in #35 comment.

Please review the patch.

Thanks.

Pooja Ganjage’s picture

Status: Needs work » Needs review
Spokje’s picture

Not quite sure on what patch #36 was based on, #18 or #19.

@Pooja Ganjage: An interdiff would help with that.

Anyway, since #36 didn't pass and #35 mentions #18 here's a patch based on that one which should pass Custom Commands

Spokje’s picture

Note: I couldn't find the test-file VendorHardeningPluginTest actually being ran on TestBot: https://dispatcher.drupalci.org/job/drupal_patches/72210/consoleText

AdamPS’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @Spokje Looks good to me

catch’s picture

Title: Support directories other than vendor in the Hardering Plugin » Support directories other than vendor in the Harderning Plugin
fgm’s picture

Title: Support directories other than vendor in the Harderning Plugin » Support directories other than vendor in the Hardening Plugin

  • catch committed 216dc5c on 9.2.x
    Issue #3108262 by Mile23, andypost, Spokje, Pooja Ganjage, piggito, Chi...
catch’s picture

Status: Reviewed & tested by the community » Fixed

This changes a public method on the vendor hardening plugin, but it's explicitly marked @internal so that's OK.

I have some of the same concerns as @greg.1.anderson here, however the changes required to support this are very minimal, so I guess I'm +0.1 or something.

Committed 216dc5c and pushed to 9.2.x. Thanks!

Status: Fixed » Closed (fixed)

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