Skip to content

fix(auth): add auth scheme hint to token rejected error for alt registries#16794

Open
enricobolzonello wants to merge 4 commits intorust-lang:masterfrom
enricobolzonello:auth-scheme-hint
Open

fix(auth): add auth scheme hint to token rejected error for alt registries#16794
enricobolzonello wants to merge 4 commits intorust-lang:masterfrom
enricobolzonello:auth-scheme-hint

Conversation

@enricobolzonello
Copy link
Copy Markdown

What does this PR try to resolve?

Based on the POC PR #15985, expand token rejected error message with authorization scheme.

Addresses issue #15021.

How to test and review this PR?

Run the tests in tests/testsuite/registry_auth.rs:

  • incorrect_token_unrecognized_scheme
  • incorrect_token_bearer_scheme
  • incorrect_token

@rustbot rustbot added A-registry-authentication Area: registry authentication and authorization (authn authz) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 26, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 26, 2026

r? @weihanglo

rustbot has assigned @weihanglo.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ehuss, @epage, @weihanglo
  • @ehuss, @epage, @weihanglo expanded to ehuss, epage, weihanglo
  • Random selection from ehuss, epage, weihanglo

f,
"\nnote: the token does not include an authorization scheme prefix \
(e.g. `Bearer `); if the registry requires one, prefix the token value \
in credentials.toml"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd prefer we avoid the reference to credentials.toml, and recommend using cargo login to set the token. Directly editing the credentials files doesn't support the more secure credential provider workflow.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

got it, I will remove the mention to credentials.toml

Some((prefix, _)) if !prefix.is_empty() => Some(match prefix {
"Bearer" => AuthorizationScheme::Bearer,
"Basic" => AuthorizationScheme::Basic,
other => AuthorizationScheme::Unrecognized(other.to_string()),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we require that other is also shorter than maybe 20 characters before passing it through? I'm a little worried that this could end up leaking secrets.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I see the concern. We could combine the length check with the token specification (which disallows characters like = or :). The other option would be to remove the Unrecognized entirely, but we would lose the warning which is helpful

)?,
AuthorizationScheme::Unrecognized(x) => write!(
f,
"\nwarning: the token has an unrecognized authorization scheme `{x}`"
Copy link
Copy Markdown
Contributor

@arlosi arlosi Mar 27, 2026

Choose a reason for hiding this comment

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

I'm worried about this as a potential user confusion source. Say for example a registry uses a scheme of AWS4-HMAC-SHA256, but the the token part of the scheme is invalid. In this case, just because Cargo didn't recognize the scheme doesn't mean the server didn't. Only the registry server can say whether a scheme is unrecognized or not (which it can do, since we print the body of the HTTP response).

I think given this situation, I'd prefer that we reduce this to only emit the note if there is no authentication scheme present (the NoScheme case), and don't emit anything extra for the other cases (keep existing behavior).

match scheme {
AuthorizationScheme::NoScheme => write!(
f,
"\nnote: the token does not include an authorization scheme prefix \
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The proper name for a "authorization scheme prefix" seems like an "authentication scheme"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-registry-authentication Area: registry authentication and authorization (authn authz) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants