Skip to content

Commit b08b412

Browse files
authored
Merge pull request #11189 from LawnGnome/no-patch-for-you
crates_io_tarball: prevent publication of crates with `[patch]` sections
2 parents 5889a7e + bba3417 commit b08b412

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

crates/crates_io_tarball/src/manifest.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ pub fn validate_manifest(manifest: &Manifest) -> Result<(), Error> {
77
// does not accept workspace manifests.
88
let package = package.ok_or(Error::Other("missing field `package`".to_string()))?;
99

10+
// We don't want to allow [patch] sections in manifests at all.
11+
if matches!(&manifest.patch, Some(patch) if !patch.is_empty()) {
12+
return Err(Error::Other(
13+
"crates cannot be published with `[patch]` tables".to_string(),
14+
));
15+
}
16+
1017
validate_package(package)?;
1118

1219
// These checks ensure that dependency workspace inheritance has been

src/tests/krate/publish/dependencies.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,38 @@ async fn new_krate_with_wildcard_dependency() {
285285
assert_that!(app.stored_files().await, empty());
286286
}
287287

288+
#[tokio::test(flavor = "multi_thread")]
289+
async fn new_krate_with_patch() {
290+
let (app, _, user, token) = TestApp::full().with_token().await;
291+
let mut conn = app.db_conn().await;
292+
293+
// Insert a crate directly into the database so that new_wild can depend on it
294+
CrateBuilder::new("foo_patch", user.as_model().id)
295+
.expect_build(&mut conn)
296+
.await;
297+
298+
let manifest = r#"
299+
[package]
300+
name = "new_patch"
301+
version = "1.0.0"
302+
description = "foo?!"
303+
license = "MIT"
304+
305+
[dependencies]
306+
foo_patch = "1.0.0"
307+
308+
[patch.crates-io]
309+
foo_patch = { git = "https://github.com/foo/patch.git" }
310+
"#;
311+
312+
let crate_to_publish = PublishBuilder::new("new_patch", "1.0.0").custom_manifest(manifest);
313+
314+
let response = token.publish_crate(crate_to_publish).await;
315+
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
316+
assert_snapshot!(response.text(), @r###"{"errors":[{"detail":"failed to parse `Cargo.toml` manifest file\n\ncrates cannot be published with `[patch]` tables"}]}"###);
317+
assert_that!(app.stored_files().await, empty());
318+
}
319+
288320
#[tokio::test(flavor = "multi_thread")]
289321
async fn new_krate_dependency_missing() {
290322
let (app, _, _, token) = TestApp::full().with_token().await;

0 commit comments

Comments
 (0)