Skip to content

Commit fcbe47d

Browse files
committed
fix(toml): Reject registry-index in user-written manifests
1 parent e1ac30b commit fcbe47d

File tree

3 files changed

+64
-52
lines changed

3 files changed

+64
-52
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,40 @@ pub fn to_real_manifest(
12761276
gctx: &GlobalContext,
12771277
warnings: &mut Vec<String>,
12781278
_errors: &mut Vec<String>,
1279+
) -> CargoResult<Manifest> {
1280+
to_real_manifest_impl(
1281+
contents,
1282+
document,
1283+
original_toml,
1284+
normalized_toml,
1285+
features,
1286+
workspace_config,
1287+
source_id,
1288+
manifest_file,
1289+
is_embedded,
1290+
gctx,
1291+
warnings,
1292+
_errors,
1293+
false,
1294+
)
1295+
}
1296+
1297+
/// Internal implementation with cargo_generated parameter
1298+
#[tracing::instrument(skip_all)]
1299+
fn to_real_manifest_impl(
1300+
contents: String,
1301+
document: toml::Spanned<toml::de::DeTable<'static>>,
1302+
original_toml: manifest::TomlManifest,
1303+
normalized_toml: manifest::TomlManifest,
1304+
features: Features,
1305+
workspace_config: WorkspaceConfig,
1306+
source_id: SourceId,
1307+
manifest_file: &Path,
1308+
is_embedded: bool,
1309+
gctx: &GlobalContext,
1310+
warnings: &mut Vec<String>,
1311+
_errors: &mut Vec<String>,
1312+
cargo_generated: bool,
12791313
) -> CargoResult<Manifest> {
12801314
let package_root = manifest_file.parent().unwrap();
12811315
if !package_root.is_dir() {
@@ -1582,6 +1616,7 @@ pub fn to_real_manifest(
15821616
warnings,
15831617
platform: None,
15841618
root: package_root,
1619+
cargo_generated,
15851620
};
15861621
gather_dependencies(
15871622
&mut manifest_ctx,
@@ -1977,6 +2012,7 @@ fn to_virtual_manifest(
19772012
warnings,
19782013
platform: None,
19792014
root,
2015+
cargo_generated: false,
19802016
};
19812017
(
19822018
replace(&normalized_toml, &mut manifest_ctx)?,
@@ -2045,6 +2081,7 @@ struct ManifestContext<'a, 'b> {
20452081
warnings: &'a mut Vec<String>,
20462082
platform: Option<Platform>,
20472083
root: &'a Path,
2084+
cargo_generated: bool,
20482085
}
20492086

20502087
#[tracing::instrument(skip_all)]
@@ -2175,6 +2212,7 @@ pub(crate) fn to_dependency<P: ResolveToPath + Clone>(
21752212
warnings,
21762213
platform,
21772214
root,
2215+
cargo_generated: false,
21782216
},
21792217
kind,
21802218
)
@@ -2292,6 +2330,24 @@ fn detailed_dep_to_dependency<P: ResolveToPath + Clone>(
22922330
dep.set_registry_id(registry_id);
22932331
}
22942332
if let Some(registry_index) = &orig.registry_index {
2333+
// `registry-index` is for internal use only.
2334+
// It should not be used in user-written manifests as it bypasses the need for .cargo/config.toml configuration.
2335+
2336+
if !manifest_ctx.source_id.is_registry() && !manifest_ctx.cargo_generated {
2337+
// Check if this is a packaged manifest (in target/package or target\package)
2338+
// by checking if the path contains the pattern
2339+
let path_str = manifest_ctx.root.to_string_lossy();
2340+
let is_packaged_manifest =
2341+
path_str.contains("target/package") || path_str.contains("target\\package");
2342+
2343+
if !is_packaged_manifest {
2344+
bail!(
2345+
"dependency ({}) specification uses `registry-index` which is for internal use only\n\
2346+
help: use `registry = \"<name>\"` and configure the registry in `.cargo/config.toml`",
2347+
name_in_toml
2348+
);
2349+
}
2350+
}
22952351
let url = registry_index.into_url()?;
22962352
let registry_id = SourceId::for_registry(&url)?;
22972353
dep.set_registry_id(registry_id);
@@ -2936,7 +2992,7 @@ pub fn prepare_for_publish(
29362992
let mut warnings = Default::default();
29372993
let mut errors = Default::default();
29382994
let gctx = ws.gctx();
2939-
let manifest = to_real_manifest(
2995+
let manifest = to_real_manifest_impl(
29402996
contents.to_owned(),
29412997
document.clone(),
29422998
original_toml,
@@ -2949,6 +3005,7 @@ pub fn prepare_for_publish(
29493005
gctx,
29503006
&mut warnings,
29513007
&mut errors,
3008+
true, // cargo_generated - this is a Cargo-generated manifest
29523009
)?;
29533010
let new_pkg = Package::new(manifest, me.manifest_path());
29543011
Ok(new_pkg)

tests/testsuite/alt_registry.rs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -282,34 +282,14 @@ fn registry_index_not_allowed_in_user_manifests() {
282282
.file("src/lib.rs", "")
283283
.build();
284284

285-
// FIXME: This currently allows `registry-index` which is a bug.
286-
// It should error during manifest parsing because `registry-index` is for internal use only.
287-
// Instead, it tries to fetch from the URL and fails with a network error.
288285
p.cargo("check")
289286
.with_status(101)
290287
.with_stderr_data(str![[r#"
291-
[UPDATING] `https://example.com/index` index
292-
[WARNING] spurious network error (3 tries remaining): unexpected http status code: 404; class=Http (34)
293-
[WARNING] spurious network error (2 tries remaining): unexpected http status code: 404; class=Http (34)
294-
[WARNING] spurious network error (1 try remaining): unexpected http status code: 404; class=Http (34)
295-
[ERROR] failed to get `bar` as a dependency of package `foo v0.0.0 ([ROOT]/foo)`
296-
297-
Caused by:
298-
failed to load source for dependency `bar`
299-
300-
Caused by:
301-
Unable to update registry `https://example.com/index`
302-
303-
Caused by:
304-
failed to fetch `https://example.com/index`
305-
306-
Caused by:
307-
network failure seems to have happened
308-
if a proxy or similar is necessary `net.git-fetch-with-cli` may help here
309-
https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli
288+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
310289
311290
Caused by:
312-
unexpected http status code: 404; class=Http (34)
291+
dependency (bar) specification uses `registry-index` which is for internal use only
292+
[HELP] use `registry = "<name>"` and configure the registry in `.cargo/config.toml`
313293
314294
"#]])
315295
.run();

tests/testsuite/publish.rs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4623,39 +4623,14 @@ fn registry_index_not_allowed_in_publish() {
46234623
.file("src/main.rs", "fn main() {}")
46244624
.build();
46254625

4626-
// FIXME: This currently allows `registry-index` which is a bug.
4627-
// It should error during manifest parsing because `registry-index` is for internal use only.
4628-
// Instead, it tries to fetch from the URL and fails with a network error.
46294626
p.cargo("publish --registry alternative")
46304627
.with_status(101)
46314628
.with_stderr_data(str![[r#"
4632-
[UPDATING] `alternative` index
4633-
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
4634-
[UPDATING] `https://example.com/index` index
4635-
[WARNING] spurious network error (3 tries remaining): unexpected http status code: 404; class=Http (34)
4636-
[WARNING] spurious network error (2 tries remaining): unexpected http status code: 404; class=Http (34)
4637-
[WARNING] spurious network error (1 try remaining): unexpected http status code: 404; class=Http (34)
4638-
[ERROR] failed to prepare local package for uploading
4639-
4640-
Caused by:
4641-
failed to get `bar` as a dependency of package `foo v0.0.1 ([ROOT]/foo)`
4642-
4643-
Caused by:
4644-
failed to load source for dependency `bar`
4645-
4646-
Caused by:
4647-
Unable to update registry `https://example.com/index`
4648-
4649-
Caused by:
4650-
failed to fetch `https://example.com/index`
4651-
4652-
Caused by:
4653-
network failure seems to have happened
4654-
if a proxy or similar is necessary `net.git-fetch-with-cli` may help here
4655-
https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli
4629+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
46564630
46574631
Caused by:
4658-
unexpected http status code: 404; class=Http (34)
4632+
dependency (bar) specification uses `registry-index` which is for internal use only
4633+
[HELP] use `registry = "<name>"` and configure the registry in `.cargo/config.toml`
46594634
46604635
"#]])
46614636
.run();

0 commit comments

Comments
 (0)