Skip to content

Return an error from unimplemented JWK key utils - #541

Open
SebTardif wants to merge 2 commits into
Keats:masterfrom
SebTardif:fix/jwk-utils-return-error
Open

SebTardif wants to merge 2 commits into
Keats:masterfrom
SebTardif:fix/jwk-utils-return-error

Conversation

@SebTardif

@SebTardif SebTardif commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Rebased onto current master after #540 merged (bf5f17f). Follow-up 4c15dc7 puts the ErrorKind::Provider sentence back. The doc is that line, then "Use this if your [CryptoProvider] does not support JWKs." The paragraph about how Result works stays out. The stub returns ErrorKind::Provider with UNIMPLEMENTED_ERROR set to "This CryptoProvider does not support JWKs".

The change

The five fn pointers in KeyUtils all return Result:

pub rsa_pub_components_from_private_key: fn(&[u8]) -> Result<(Vec<u8>, Vec<u8>)>,
pub rsa_pub_components_from_public_key:  fn(&[u8]) -> Result<(Vec<u8>, Vec<u8>)>,
pub ec_pub_components_from_private_key:  fn(&[u8], Algorithm) -> Result<(EllipticCurve, Vec<u8>, Vec<u8>)>,
pub ed_pub_components_from_private_key:  fn(&[u8], &EllipticCurve) -> Result<Vec<u8>>,
pub compute_digest:                      fn(&[u8], ThumbprintHash) -> Result<Vec<u8>>,

new_unimplemented filled them with stubs that panic instead. The error channel already exists; the stubs were throwing it away. This makes them return ErrorKind::Provider, the variant that already exists for exactly this ("Custom provider error: …").

Why this one isn't the same as #540

I argued for returning an error in #540 and was wrong: a missing process-level provider is a static configuration error, there is nothing to do at the call site, and panicking is right.

This is a different situation, and the distinction is whether the failure is decided by Cargo.toml or by runtime data:

  • A provider that opts out of JWK support is correctly configured. Nothing is wrong with the build.
  • Whether a caller reaches a stub depends on the key it passes at runtime. Jwk::from_encoding_key with an RSA key hits rsa_pub_components_from_private_key; the same call with an HMAC key never does, because that arm returns early. So the same correct configuration succeeds or fails depending on the input.
  • A caller handing a JWK endpoint keys of mixed types has something sensible to do with the error — skip the key, return a 400, fall back — without the answer being "go edit your Cargo.toml".

This reads to me like the same line drawn in #524, where thumbprint stopped panicking on a curve/parameter mismatch and returned ErrorKind::InvalidKeyFormat. Tell me if I'm reading that wrong.

Interaction with #540

Before #540, new_unimplemented had two callers with very different meanings: the crate's own placeholder provider (a config error) and user-supplied providers (a runtime condition). One message had to cover both, which is why it currently reads:

Could not automatically determine the process-level CryptoProvider from jsonwebtoken crate features, or your CryptoProvider does not support JWKs. Call CryptoProvider::install_default() before this point…

#540 removes the placeholder, leaving no callers inside the crate. The advice about crate features and install_default then has nothing to do with why the stub fires, and the only consumer left is a user's own provider. That is why this is worth doing after #540 and not before — on master it would change the no-provider path back to an error, which is the thing we just agreed not to do.

Validation

Config Result
cargo test --features aws_lc_rs pass
cargo test --features rust_crypto pass
cargo test --features aws_lc_rs --test unimplemented_jwk pass
cargo clippy --all-targets -- -D warnings, all three configs clean
cargo fmt --check clean

@arckoor arckoor left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks mostly fine, a few nits and needs a rebase to master. Obviously breaking, but should hopefully be mergeable without major conflicts when the time comes.

Comment thread src/crypto/mod.rs Resolved
Comment thread src/crypto/mod.rs Resolved
The five fn pointers in KeyUtils already return Result. new_unimplemented
filled them with stubs that panic.

A provider that opts out of JWK support is configured correctly. Whether
a caller hits a stub depends on the key passed at runtime, so the stubs
return ErrorKind::Provider. The message is UNIMPLEMENTED_ERROR:
"This CryptoProvider does not support JWKs".

After Keats#540, new_unimplemented has no callers inside the crate.
@SebTardif
SebTardif force-pushed the fix/jwk-utils-return-error branch from 8054b91 to bf5f17f Compare October 4, 2026 14:59
@SebTardif
SebTardif marked this pull request as ready for review October 4, 2026 15:00
The first rustdoc sentence says the stubs return ErrorKind::Provider.
The short "Use this if" sentence stays. The paragraph about how
Result works stays out.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants