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

cargo vendor --versioned-dirs doesn't notice that local vendored content has diverged from crate registry #11897

Open
jhfrontz opened this issue Mar 27, 2023 · 4 comments
Labels
C-bug Category: bug Command-vendor S-needs-team-input Status: Needs input from team on whether/how to proceed.

Comments

@jhfrontz
Copy link

jhfrontz commented Mar 27, 2023

Problem

I had been hoping that I could use cargo vendor to quickly identify any unofficial/local changes to a crate.

To test this, I made an innocuous change to a local copy of a crate -- I was expecting when I did cargo vendor that it would either point out the difference (or even just silently overwrite it). However, it doesn't seem to do either.

The workaround seems to be to delete the entire vendor directory and then compare the result to what had been checked in (a la git diff --exit-code) -- after ignoring any semi-ephemeral content (e.g., git add $(find . -type f -name Cargo.lock -o -name .cargo-checksum.json))

Steps

No response

Possible Solution(s)

It's not clear to me whether this is the intended behavior for cargo vendor or not -- but if it is, there should be a major note on the man page about cargo vendor retaining local changes.

Notes

No response

Version

$ cargo version --verbose
cargo 1.65.0
release: 1.65.0
host: x86_64-unknown-linux-gnu
libgit2: 1.5.0 (sys:0.15.0 vendored)
libcurl: 7.29.0 (sys:0.4.55+curl-7.83.1 system ssl:NSS/3.53.1)
os: CentOS 7.9.2009 (Core) [64-bit]
@jhfrontz jhfrontz added the C-bug Category: bug label Mar 27, 2023
@weihanglo
Copy link
Member

Hmm… cargo vendor does check the integrity of vendored packages. It also overwrites files if you modify them manually. Can you provide steps to help the reproduction?

BTW, to make vendoring take effect, you need to put some configs under .cargo/config.toml first, like

[source.crates-io]
replace-with = "vendored-sources"

[source.vendored-sources]
directory = "vendor"

@weihanglo weihanglo added the S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request. label Apr 12, 2023
@weihanglo
Copy link
Member

This is a reproducer that verifies Cargo is aware of vendored source being modified:

cargo new cargo-issue-11897
pushd cargo-issue-11897
cargo add empty-library
mkdir -p .cargo
cargo vendor > .cargo/config.toml
echo "pub fn f() {}" > vendor/empty-library/src/lib.rs
cargo c

You'll get an error like

error: the listed checksum of `/projects/cargo-issue-11897/vendor/empty-library/src/lib.rs` has changed:
expected: b81956f17c56fffd4b5e81f93eb647dd8ce78a89bccec826b5525368f172baba
actual:   16c2fca9e371936458d01576b4ca311c22d166a45539ccbc9104823d0b10db47

The things you bumped into might be #9455, which registry sources are not readonly. I believe this is not cargo-vendor's fault — cargo-vendor always re-unpack from tarballs when vendoring. See #12509.

I am going to close due to lack of a minimal set of steps to reproduce, and it doesn't look like an issue of cargo-vendor. Here are tips about how to create a minimal, complete, and verifiable example. Let us know if there is any update on your side.

@weihanglo weihanglo closed this as not planned Won't fix, can't repro, duplicate, stale Aug 16, 2023
@apoelstra
Copy link

apoelstra commented Aug 16, 2023

Hi @weihanglo. Thanks for the minimal example! Here is a tweaked version which shows the bug we intended to report:

Here is an example:

cargo new cargo-issue-11897
pushd cargo-issue-11897
cargo add empty-library
mkdir -p .cargo
cargo vendor --versioned-dirs > .cargo/config.toml
echo "pub fn f() {}" > vendor/empty-library-1.0.0/src/lib.rs
sed -i 's/b81956f17c56fffd4b5e81f93eb647dd8ce78a89bccec826b5525368f172baba/16c2fca9e371936458d01576b4ca311c22d166a45539ccbc9104823d0b10db47/' vendor/empty-library-1.0.0/.cargo-checksum.json
cargo c # this passes, unsurprisingly

So far so good -- I don't think anybody expects cargo to be able to locally detect tampering when you've updated the checksum file. Within a CI setup that has no Internet access this would be literaly impossible (absent some sort of WoT rooted inside of cargo itself I guess.)

This bug report is about the following behavior though:

# ...execute same commands as above...
cargo vendor --versioned-dirs > .cargo/config.toml
cargo c # still passes...
cat vendor/empty-library-1.0.0/src/lib.rs #...even though the file is still compromised

If you do exactly the same steps but without --versioned-dirs (and without the -1.0.0 in the paths) then we see the expected behavior, which is that the compromised lib.rs file gets overwritten by re-running cargo vendor.

@weihanglo
Copy link
Member

Nice catch, and thanks for the repro!

The quickest fix is removing these lines. Cargo assumes that vendor sources never changes, but you never know what users would do.

diff --git a/src/cargo/ops/vendor.rs b/src/cargo/ops/vendor.rs
index 3ee46db32..a9d32dc02 100644
--- a/src/cargo/ops/vendor.rs
+++ b/src/cargo/ops/vendor.rs
@@ -212,10 +212,6 @@ fn sync(
         let dst = canonical_destination.join(&dst_name);
         to_remove.remove(&dst);
         let cksum = dst.join(".cargo-checksum.json");
-        if dir_has_version_suffix && cksum.exists() {
-            // Always re-copy directory without version suffix in case the version changed
-            continue;
-        }
 
         config.shell().status(
             "Vendoring",

Given people shouldn't modify the content in the first place, I am not sure if this is a good way to resolve this. Maybe, when cargo vendors stuff, it should mark all files as read-only. Though it will lead us to #9455 I guess.

@weihanglo weihanglo reopened this Aug 16, 2023
@weihanglo weihanglo added Command-vendor S-needs-team-input Status: Needs input from team on whether/how to proceed. and removed S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request. labels Aug 16, 2023
@weihanglo weihanglo changed the title cargo vendor doesn't notice that local vendored content has diverged from crate registry cargo vendor --versioned-dirs doesn't notice that local vendored content has diverged from crate registry Aug 16, 2023
bors added a commit that referenced this issue Sep 13, 2024
fix(vendor): trust crate version only when coming from registries

### What does this PR try to resolve?

Fixes #8181
Relates to #11897 and #14525

### How should we test and review this PR?

As mentioned in the contribution guide, I made a first commit adding a test that passes with the actual behaviour. Then, I made a second commit with a fix and modified the test with the new expected  behaviour.

### Additional information

The fix doesn't take into account switching from a git dependency to crates.io, which is not handled correctly on master either, and would probably require the vendoring to serialize the source ID to detect source changes.

I specifically limited the trust of immutable version to crates.io, but it could be extended to other registries.
bors added a commit that referenced this issue Sep 14, 2024
fix(vendor): trust crate version only when coming from registries

### What does this PR try to resolve?

Fixes #8181
Relates to #11897 and #14525

### How should we test and review this PR?

As mentioned in the contribution guide, I made a first commit adding a test that passes with the actual behaviour. Then, I made a second commit with a fix and modified the test with the new expected  behaviour.

### Additional information

The fix doesn't take into account switching from a git dependency to crates.io, which is not handled correctly on master either, and would probably require the vendoring to serialize the source ID to detect source changes.

I specifically limited the trust of immutable version to crates.io, but it could be extended to other registries.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug Command-vendor S-needs-team-input Status: Needs input from team on whether/how to proceed.
Projects
None yet
Development

No branches or pull requests

3 participants