-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Properly validate crate names in cargo install
#16314
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
Properly validate crate names in cargo install
#16314
Conversation
|
r? @weihanglo rustbot has assigned @weihanglo. Use |
cargo install
|
If you see any weird characters, vim seems to have mangled some of the characters when I was making a PR. I think I fixed them all, but just a heads up For example, – turned into ΓÇô |
| } | ||
| } | ||
|
|
||
| if crate_name != "." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
. is special cased later to warn that cargo install . is deprecated
tests/testsuite/install.rs
Outdated
| } | ||
|
|
||
| #[cargo_test] | ||
| fn feature_name_dep_colon_case() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some cases like this already behaved correctly, but I didn't see any test coverage for them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is covered in
cargo/tests/testsuite/features_namespaced.rs
Lines 525 to 541 in ebd20d2
| p.cargo("check --features dep:bar") | |
| .with_status(101) | |
| .with_stderr_data(str![[r#" | |
| [ERROR] feature `dep:bar` is not allowed to use explicit `dep:` syntax | |
| "#]]) | |
| .run(); | |
| switch_to_resolver_2(&p); | |
| p.cargo("check --features dep:bar") | |
| .with_status(101) | |
| .with_stderr_data(str![[r#" | |
| [ERROR] feature `dep:bar` is not allowed to use explicit `dep:` syntax | |
| "#]]) | |
| .run(); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, for bugfixes, we usually follow a variant of atomic commit pattern:
- Commit a test that asserts the current buggy behavior (passes).
- In the next commit, fix the bug and update the test/snapshot.
Every commit passes, and the test/snapshot diff shows the behavior change.
Would you mind rearranging your commit history to follow that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@weihanglo just pushed up the changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the update!
tests/testsuite/install.rs
Outdated
| } | ||
|
|
||
| #[cargo_test] | ||
| fn feature_name_dep_colon_case() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is covered in
cargo/tests/testsuite/features_namespaced.rs
Lines 525 to 541 in ebd20d2
| p.cargo("check --features dep:bar") | |
| .with_status(101) | |
| .with_stderr_data(str![[r#" | |
| [ERROR] feature `dep:bar` is not allowed to use explicit `dep:` syntax | |
| "#]]) | |
| .run(); | |
| switch_to_resolver_2(&p); | |
| p.cargo("check --features dep:bar") | |
| .with_status(101) | |
| .with_stderr_data(str![[r#" | |
| [ERROR] feature `dep:bar` is not allowed to use explicit `dep:` syntax | |
| "#]]) | |
| .run(); | |
| } |
cfda2d9 to
805c6fb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the update!
Update cargo submodule 14 commits in 2a7c4960677971f88458b0f8b461a866836dff59..bd979347d814dfe03bba124165dbce9554d0b4d8 2025-11-25 19:58:07 +0000 to 2025-12-02 16:03:50 +0000 - fix(completion): Put host-tuple before actual tuples (rust-lang/cargo#16327) - fix(lints): use plural form correctly (rust-lang/cargo#16324) - fix(completions): include `all` in `cargo tree --target` candidates (rust-lang/cargo#16322) - fix(lints): show lint error number (rust-lang/cargo#16320) - chore(deps): update compatible (rust-lang/cargo#16318) - chore(deps): update crate-ci/typos action to v1.40.0 (rust-lang/cargo#16316) - Do not lock the artifact-dir for check builds + fix uplifting (rust-lang/cargo#16307) - Properly validate crate names in `cargo install` (rust-lang/cargo#16314) - Support --filter-platform=host for cargo metadata rust-lang/cargo#9423 (rust-lang/cargo#16312) - Update to mdbook 0.5 (rust-lang/cargo#16292) - refactor(clean): Better divide old / new layout (rust-lang/cargo#16304) - update: silent failure on non-matching package specs with --breaking (rust-lang/cargo#16276) - fix(log): break timing-info message to multiple (rust-lang/cargo#16303) - fix(clean): Clean hosts builds with new layout (rust-lang/cargo#16300)
What does this PR try to resolve?
cargo installdid not properly validate package names it received causingOut of bounds indexing
When
cargo installis passed a string starting with a character with more than 2 bytes, it would index out of bounds and panicThis often manifests itself through copying from another program that changes the characters or typing on mobile. For example, the below error is caused by using an en dash instead of a dash, so it's interpreted as a crate name since clap does not parse it as a flag. The code that panics assumes it will only receive ascii string slices, the cargo install code does validate naming errors, but it swallows errors regarding invalid characters
After my changes, this is the new result
Erroneous searches
The registry is erroneously searched for other invalid package names when attempting to install
After my changes, it bails before searching
Reserved names behavior preseved
These changes continue to allow installing reserved names such as
nulandstdcargo run -q -- install std Updating crates.io index error: could not find `std` in registry `crates-io` with version `*`How to test and review this PR?
Run
cargo installwith values that do not conform to the package name identifier specificationsPotential Followup
I'd argue the error message could be improved with a help diagnostic like the following. Would this be something y'all would be interested in? If so, would there be a more general place to provide this diagnostic, i.e. are there places name validations are done in a non-cli context?
Concerns
EDIT: CI passes, so likely just a setup issue
There were around 90 tests that failed even when running tests from master. I figure this is an issue with my own setup. All tests in the area I worked in were passing