Skip to content

Commit

Permalink
Check token validity when loading registry config
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Jan 31, 2025
1 parent ca6274c commit a3aedb6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/cargo/util/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use cargo_credential::{
Action, CacheControl, Credential, CredentialResponse, LoginOptions, Operation, RegistryInfo,
Secret,
};
use crates_io::check_token;

use core::fmt;
use serde::Deserialize;
Expand Down Expand Up @@ -236,6 +237,11 @@ pub fn registry_credential_config_raw(
return Ok(cfg.clone());
}
let cfg = registry_credential_config_raw_uncached(gctx, sid)?;
if let Some(RegistryConfig { token: Some(token), .. }) = &cfg {
check_token(&token.val.as_deref().expose()).with_context(|| {
format!("Token for {sid} is invalid (defined in {})", token.definition)
})?;
}
cache.insert(*sid, cfg.clone());
return Ok(cfg);
}
Expand Down
5 changes: 1 addition & 4 deletions tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3609,10 +3609,7 @@ fn invalid_token() {
.env("CARGO_REGISTRY_TOKEN", "\x16")
.with_stderr_data(str![[r#"
[UPDATING] crates.io index
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
[UPLOADING] foo v0.0.1 ([ROOT]/foo)
[ERROR] failed to publish to registry at http://127.0.0.1:[..]/
[ERROR] Token for registry `crates-io` is invalid (defined in environment variable `CARGO_REGISTRY_TOKEN`)
Caused by:
token contains invalid characters.
Expand Down

0 comments on commit a3aedb6

Please sign in to comment.