Skip to content

Commit 437657d

Browse files
committed
Auto merge of rust-lang#1589 - jtgeibel:deny-alt-registry-deps, r=jtgeibel
Reject publishing of crates that depend on an alternative registry See also rust-lang#1579 and rust-lang/crates-io-cargo-teams#21.
2 parents 776217c + 4df95a2 commit 437657d

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

src/models/dependency.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ pub fn add_dependencies(
8080
let git_and_new_dependencies = deps
8181
.iter()
8282
.map(|dep| {
83+
if let Some(registry) = &dep.registry {
84+
if !registry.is_empty() {
85+
return Err(human(&format_args!("Dependency `{}` is hosted on another registry. Cross-registry dependencies are not permitted on crates.io.", &*dep.name)));
86+
}
87+
}
88+
8389
// Match only identical names to ensure the index always references the original crate name
8490
let krate = Crate::by_exact_name(&dep.name)
8591
.first::<Crate>(&*conn)

src/tests/builders.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ impl PublishBuilder {
522522
/// A builder for constructing a dependency of another crate.
523523
pub struct DependencyBuilder {
524524
name: String,
525+
registry: Option<String>,
525526
explicit_name_in_toml: Option<u::EncodableCrateName>,
526527
version_req: u::EncodableCrateVersionReq,
527528
}
@@ -531,6 +532,7 @@ impl DependencyBuilder {
531532
pub fn new(name: &str) -> Self {
532533
DependencyBuilder {
533534
name: name.to_string(),
535+
registry: None,
534536
explicit_name_in_toml: None,
535537
version_req: u::EncodableCrateVersionReq(semver::VersionReq::parse(">= 0").unwrap()),
536538
}
@@ -542,6 +544,12 @@ impl DependencyBuilder {
542544
self
543545
}
544546

547+
/// Set an alternative registry for this dependency.
548+
pub fn registry(mut self, registry: &str) -> Self {
549+
self.registry = Some(registry.to_string());
550+
self
551+
}
552+
545553
/// Set the version requirement for this dependency.
546554
///
547555
/// # Panics
@@ -567,6 +575,7 @@ impl DependencyBuilder {
567575
target: None,
568576
kind: None,
569577
explicit_name_in_toml: self.explicit_name_in_toml,
578+
registry: self.registry,
570579
}
571580
}
572581
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"request": {
4+
"uri": "http://alexcrichton-test.s3.amazonaws.com/crates/foo/foo-1.0.0.crate",
5+
"method": "PUT",
6+
"headers": [
7+
[
8+
"accept",
9+
"*/*"
10+
],
11+
[
12+
"content-length",
13+
"35"
14+
],
15+
[
16+
"host",
17+
"alexcrichton-test.s3.amazonaws.com"
18+
],
19+
[
20+
"accept-encoding",
21+
"gzip"
22+
],
23+
[
24+
"content-type",
25+
"application/x-tar"
26+
]
27+
],
28+
"body": "H4sIAAAAAAAA/+3AAQEAAACCIP+vbkhQwKsBLq+17wAEAAA="
29+
},
30+
"response": {
31+
"status": 200,
32+
"headers": [],
33+
"body": ""
34+
}
35+
}
36+
]

src/tests/krate.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,37 @@ fn reject_new_krate_with_non_exact_dependency() {
690690
token.publish(crate_to_publish).bad_with_status(200);
691691
}
692692

693+
#[test]
694+
fn new_crate_allow_empty_alternative_registry_dependency() {
695+
let (app, _, user, token) = TestApp::with_proxy().with_token();
696+
697+
app.db(|conn| {
698+
CrateBuilder::new("foo-dep", user.as_model().id).expect_build(conn);
699+
});
700+
701+
let dependency = DependencyBuilder::new("foo-dep").registry("");
702+
let crate_to_publish = PublishBuilder::new("foo").dependency(dependency);
703+
token.publish(crate_to_publish).good();
704+
}
705+
706+
#[test]
707+
fn reject_new_crate_with_alternative_registry_dependency() {
708+
let (_, _, _, token) = TestApp::init().with_token();
709+
710+
let dependency =
711+
DependencyBuilder::new("dep").registry("https://server.example/path/to/registry");
712+
713+
let crate_to_publish = PublishBuilder::new("depends-on-alt-registry").dependency(dependency);
714+
let json = token.publish(crate_to_publish).bad_with_status(200);
715+
assert!(
716+
json.errors[0]
717+
.detail
718+
.contains("Cross-registry dependencies are not permitted on crates.io."),
719+
"{:?}",
720+
json.errors
721+
);
722+
}
723+
693724
#[test]
694725
fn new_krate_with_wildcard_dependency() {
695726
let (app, _, user, token) = TestApp::init().with_token();

src/views/krate_publish.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub struct EncodableCrateDependency {
6363
pub target: Option<String>,
6464
pub kind: Option<DependencyKind>,
6565
pub explicit_name_in_toml: Option<EncodableCrateName>,
66+
pub registry: Option<String>,
6667
}
6768

6869
impl<'de> Deserialize<'de> for EncodableCrateName {

0 commit comments

Comments
 (0)