Skip to content

Commit bdbc8da

Browse files
committed
Validate that the credential process only outputs a single line (token).
1 parent 69c5af8 commit bdbc8da

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/cargo/ops/registry/auth.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ fn run_command(
164164
)
165165
})?;
166166
if let Some(end) = buffer.find('\n') {
167+
if buffer.len() > end + 1 {
168+
bail!(
169+
"credential process `{}` returned more than one line of output; \
170+
expected a single token",
171+
exe.display()
172+
);
173+
}
167174
buffer.truncate(end);
168175
}
169176
token = Some(buffer);

tests/testsuite/credential_process.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,3 +448,45 @@ Caused by:
448448
)
449449
.run();
450450
}
451+
452+
#[cargo_test]
453+
fn invalid_token_output() {
454+
// Error when credential process does not output the expected format for a token.
455+
registry::init();
456+
paths::home().join(".cargo/credentials").rm_rf();
457+
let cred_proj = project()
458+
.at("cred_proj")
459+
.file("Cargo.toml", &basic_manifest("test-cred", "1.0.0"))
460+
.file("src/main.rs", r#"fn main() { print!("a\nb\n"); } "#)
461+
.build();
462+
cred_proj.cargo("build").run();
463+
464+
cargo::util::paths::append(
465+
&paths::home().join(".cargo/config"),
466+
format!(
467+
r#"
468+
[registry]
469+
credential-process = ["{}"]
470+
"#,
471+
toml_bin(&cred_proj, "test-cred")
472+
)
473+
.as_bytes(),
474+
)
475+
.unwrap();
476+
477+
let p = project()
478+
.file("Cargo.toml", &basic_manifest("foo", "1.0.0"))
479+
.file("src/lib.rs", "")
480+
.build();
481+
482+
p.cargo("publish --no-verify --registry alternative -Z credential-process")
483+
.masquerade_as_nightly_cargo()
484+
.with_status(101)
485+
.with_stderr(
486+
"\
487+
[UPDATING] [..]
488+
[ERROR] credential process `[..]test-cred[EXE]` returned more than one line of output; expected a single token
489+
",
490+
)
491+
.run();
492+
}

0 commit comments

Comments
 (0)