Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patches from drupal.org merge request URLs are dangerous? #347

Closed
JohnAlbin opened this issue Dec 16, 2020 · 11 comments
Closed

Patches from drupal.org merge request URLs are dangerous? #347

JohnAlbin opened this issue Dec 16, 2020 · 11 comments

Comments

@JohnAlbin
Copy link

JohnAlbin commented Dec 16, 2020

composer-patches allows developers to specify patches to download and use via HTTP/S, which is a super useful feature that we use all the time!

This is totally safe to do when the resource at the end of that URL is static (unchanging). For example, a URL to an actual .patch file on a website is safe to use as long as the website owner is not malicious.

The new drupal.org merge request URLs are not safe, however. For example: https://git.drupalcode.org/project/drupal/-/merge_requests/121.diff

Anyone with commit access to Drupal issues can change the code on that the URL above references. That’s a malicious code injection vector.

The docs on drupal.org describing how to create a patch file from a merge request specifically mention that the URL is static, but the code is not:

WARNING: The URL for a Merge Request patch file will be named after the MR and will not change as more and more commits are being added in the MR. The contents of the patch file will change however.

However, those docs assume that a developer would review the patch before applying it manually. They don't take into this feature of composer-patches that allows you to automatically apply whatever patch is at the end point.

Anyone with commit access to Drupal issues can change the code on that the URL above references.

It wouldn't be that hard for a malicious user to pretend to be an open-source developer. If they knew a particular website was using a merge request URL for production builds, they could target that site. Or, theoretically, they could insert some phone-home code that exploits and then contacts the malicious user afterwards.

Obviously, I haven't tried this yet. Maybe I'm missing something and it's not even theoretically possible. But I'd love to see some eyeballs on this to make sure a scenario like this isn't possible.

@JohnAlbin JohnAlbin changed the title Patches from drupal.org merge request URLs are dangerous Patches from drupal.org merge request URLs are dangerous? Dec 16, 2020
@cweagans
Copy link
Owner

I think you're right -- this probably warrants at least a warning for people using that kind of URL. It may also be a good idea to see if Gitlab has some way to pin a merge request URL to a specific commit or something (so that you can say I've reviewed the MR patch at this specific point in time and that's what I'm going to apply to my codebase). That seems like a really specific feature that Gitlab may not have thought to implement though.

Longer term, this plugin will start storing SHA-256 hashes of all patches in the lock file (if not manually specified, then the file will be hashed when the patch is first applied), so if something changes on the server side that changes that patch contents, there will be an error.

@jonathanjfshaw
Copy link

Regardless of the security aspect, this dynamic aspect of gitlab MR patches also breaks the reliability of deployments. Every time you redeploy a site and redownload the patches, you may get different code that is now untested in you project and may not even merge properly.

AFAICT Gitlab does not provide a url that allows you to access the diff of a particular commit in a way we can use, and the Drupal.org team's attitude seems to be "you should be downloading the patches and commiting them anyway, instead of relying on our servers at deployment time".

Can we improve the documentation on how to reference a local patch? The readme examples here all use urls, and I've not managed to get local patches working. See e.g. #315

@phil-s
Copy link

phil-s commented Sep 28, 2021

Even if one can obtain a GitLab URL for diff between specific commits, that's not useful unless GitLab is permanently maintaining the refs for all the commits ever pushed to a branch, even if they've been replaced, as otherwise git's garbage-collection will delete those commits at some point (typically after 30 days), at which point the URL would stop working.

Patch files with permanent URLs are more useful; and if the various attempts to allow checksums were to reach fruition there would be a way to validate their contents.

@mxr576
Copy link

mxr576 commented Oct 20, 2021

If you work for enterprise, it is also a regular security requirement that you only install external libs from signed sources. By using Packagist and Composer you get that. But pulling an unpinned patch state from an external server breaks that requirement 2 times at least - also creates a snowflake without warranty, but that is an other topic.
As a workaround we started to download patches on all projects, store them in VCS and apply them from local fs. The problem is that you cannot do that if you are maintaining an upstream project/distro/install profile, whatever, you a can only use remote references. We have an idea how to combine composer patches with other tools to serve patches from trusted sources, but an OOTB support for this would be better. (or if we would not need to maintain a software with 10+ patches , but that is an another topic 😁)

This issue also explains what I did not want to disclose here, how patching can be a backdoor: https://twitter.com/IEMIXER/status/1435247235613306882?t=Rg5K-iFv37pU7H-VoSuL3A&s=19

@greggles
Copy link

Did this:

Longer term, this plugin will start storing SHA-256 hashes of all patches in the lock file (if not manually specified, then the file will be hashed when the patch is first applied), so if something changes on the server side that changes that patch contents, there will be an error.

Ever happen?

@greggles
Copy link

Answering my own question: the work on hashes is linked in Phil-s' comment and is still open/in progress.

@ceesgeene
Copy link

I've posted a new PR: #388

@mxr576
Copy link

mxr576 commented Jan 4, 2022

First of all, I am thrilled that there is some movement on this topic 🎉 How Composer Patches could be leveraged in supply chain attacks was one of those problems that concerned me the most in the past year(s), but my focus was always shifted so I could not work on these problems. Also, @cweagans made it quite clear on Twitter that no matter how much the PHP community (not just Drupal developers) depends on this package, it is minimally maintained, and there is no problem with that. Although, that made me realize that a community effort and support are needed to provide at least the minimum necessary care to this valuable library. Something similar that was formulated around https://github.com/php-tuf/php-tuf.

Maybe that will happen now, and I would be glad if I could be part of that effort.

@mxr576
Copy link

mxr576 commented Jan 4, 2022

Post a long review on the solution implemented in #388 which may contains ideas how this can be solved differently, so I am leaving a reference here: #388 (comment)

Ambient-Impact added a commit to neurocracy/drupal-omnipedia-access that referenced this issue Jun 4, 2022
cweagans/composer-patches#347

Note that patch paths are currently relative to the root composer.json,
so the paths start with "drupal/modules/omnipedia/". This will hopefully
be fixed in some fashion either when we open source or when a solution
to resolve the paths is found, whichever comes first.
Ambient-Impact added a commit to neurocracy/drupal-omnipedia-content that referenced this issue Jun 4, 2022
cweagans/composer-patches#347

Note that patch paths are currently relative to the root composer.json,
so the paths start with "drupal/modules/omnipedia/". This will hopefully
be fixed in some fashion either when we open source or when a solution
to resolve the paths is found, whichever comes first.
Ambient-Impact added a commit to neurocracy/drupal-omnipedia-media that referenced this issue Jun 4, 2022
cweagans/composer-patches#347

Note that patch paths are currently relative to the root composer.json,
so the paths start with "drupal/modules/omnipedia/". This will hopefully
be fixed in some fashion either when we open source or when a solution
to resolve the paths is found, whichever comes first.
Ambient-Impact added a commit to neurocracy/drupal-omnipedia-user that referenced this issue Jun 4, 2022
cweagans/composer-patches#347

Note that patch paths are currently relative to the root composer.json,
so the paths start with "drupal/modules/omnipedia/". This will hopefully
be fixed in some fashion either when we open source or when a solution
to resolve the paths is found, whichever comes first.
@cweagans
Copy link
Owner

cweagans commented Feb 7, 2023

main now caches patches locally. I still need to add a warning and maybe some kind of automated "download all of my PR/MR-based patches, store them in a patches dir, and change my composer.json/patches.json accordingly" functionality. I've made a note to do that.

@cweagans cweagans closed this as completed Feb 7, 2023
@cweagans cweagans mentioned this issue Feb 7, 2023
31 tasks
Ambient-Impact added a commit to Ambient-Impact/drupal-ambientimpact-media that referenced this issue Jun 13, 2023
Ambient-Impact added a commit to Ambient-Impact/drupal-ambientimpact-migrate that referenced this issue Jun 13, 2023
Ambient-Impact added a commit to Ambient-Impact/drupal-ambientimpact-paragraphs that referenced this issue Jun 13, 2023
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

No branches or pull requests

8 participants