Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check token validity when loading registry config #15127

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/cargo-test-support/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,10 @@ impl HttpServer {
line.clear();
break;
}
let (name, value) = line.split_once(':').unwrap();
if line.as_bytes().iter().any(|&b| b > 127 || b == 0) {
panic!("Invalid char in HTTP header: {line:?}");
}
let (name, value) = line.split_once(':').expect("http header syntax");
let name = name.trim().to_ascii_lowercase();
let value = value.trim().to_string();
match name.as_str() {
Expand Down
2 changes: 1 addition & 1 deletion crates/crates-io/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ pub fn check_token(token: &str) -> Result<()> {
Ok(())
} else {
Err(Error::InvalidToken(
"token contains invalid characters.\nOnly printable ISO-8859-1 characters \
"token contains invalid characters.\nOnly printable ASCII characters \
are allowed as it is sent in a HTTPS header.",
))
}
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crates_io::check_token;
use std::cell::{Cell, Ref, RefCell, RefMut};
use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
Expand Down Expand Up @@ -743,6 +744,7 @@ impl<'a, 'gctx> Downloads<'a, 'gctx> {
// Add authorization header.
if let Some(authorization) = authorization {
let mut headers = curl::easy::List::new();
check_token(&authorization)?;
headers.append(&format!("Authorization: {}", authorization))?;
handle.http_headers(headers)?;
}
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/sources/registry/http_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::util::{auth, Filesystem, GlobalContext, IntoUrl, Progress, ProgressSt
use anyhow::Context as _;
use cargo_credential::Operation;
use cargo_util::paths;
use crates_io::check_token;
use curl::easy::{Easy, List};
use curl::multi::{EasyHandle, Multi};
use std::cell::RefCell;
Expand Down Expand Up @@ -663,6 +664,7 @@ impl<'gctx> RegistryData for HttpRegistry<'gctx> {
self.auth_error_headers.clone(),
true,
)?;
check_token(&authorization)?;
headers.append(&format!("Authorization: {}", authorization))?;
trace!(target: "network", "including authorization for {}", full_url);
}
Expand Down
12 changes: 12 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,17 @@ 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
)
})?;
}
Comment on lines +240 to +250
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is moving the check from

  • token credential provider

To

  • token credential provider
  • paseto credential provider
  • the enumeration of all credential providers

I'd want to better understand if its a good idea to be running this check in all of those other situations

CC @arlosi

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Construction of RegistryConfig is closer to TOML deserialization of credentials.toml than to implementation of credential providers. The token here is the input for downstream users of the registry config, and does not touch output token of credential providers (that's CredentialResponse).

Paseto uses secret_key and secret_key_subject fields, not token.

This field is only read by TokenCredential, apart from code that warns when cargo::token is not enabled.

cache.insert(*sid, cfg.clone());
return Ok(cfg);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/credential_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn credential_provider_auth_failure() {
.auth_required()
.alternative()
.no_configure_token()
.credential_provider(&["cargo:token-from-stdout", "true"])
.credential_provider(&["cargo:token-from-stdout", "echo", "incorrect token"])
.build();

cargo_process("install libc --registry=alternative")
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ fn invalid_login_token() {

Caused by:
token contains invalid characters.
Only printable ISO-8859-1 characters are allowed as it is sent in a HTTPS header.
Only printable ASCII characters are allowed as it is sent in a HTTPS header.
",
101,
)
Expand Down
7 changes: 2 additions & 5 deletions tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3609,14 +3609,11 @@ 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.
Only printable ISO-8859-1 characters are allowed as it is sent in a HTTPS header.
Only printable ASCII characters are allowed as it is sent in a HTTPS header.

"#]])
.with_status(101)
Expand Down
28 changes: 28 additions & 0 deletions tests/testsuite/registry_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,34 @@ Caused by:
.run();
}

#[cargo_test]
fn syntactically_invalid_token() {
let _registry = RegistryBuilder::new()
.alternative()
.auth_required()
.no_configure_token()
.http_index()
.build();

let p = make_project();
cargo(&p, "build")
.env("CARGO_REGISTRIES_ALTERNATIVE_TOKEN", "\t\n悪")
.with_status(101)
.with_stderr_data(str![[r#"
[UPDATING] `alternative` index
[ERROR] failed to get `bar` as a dependency of package `foo v0.0.1 ([ROOT]/foo)`

Caused by:
Token for registry `alternative` is invalid (defined in environment variable `CARGO_REGISTRIES_ALTERNATIVE_TOKEN`)

Caused by:
token contains invalid characters.
Only printable ASCII characters are allowed as it is sent in a HTTPS header.

"#]])
.run();
}

#[cargo_test]
fn incorrect_token_git() {
let _registry = RegistryBuilder::new()
Expand Down