-
Notifications
You must be signed in to change notification settings - Fork 0
fix(auth): make the JWT crypto backends additive #85
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| [[package]] | ||
| name = "structured-proxy" | ||
| # cargo-semver-checks builds the baseline from the registry, and every version | ||
| # published so far rejects the feature set it uses (both JWT backends at once). | ||
| # The published artefacts cannot be changed, so the check stays off until a | ||
| # release carrying that fix becomes the baseline; see issue #84. | ||
| semver_check = false | ||
| # Preserve the existing tag scheme (v1.0.0, v1.0.1, ...) instead of the | ||
| # release-plz default of "{package}-v{version}", so tag history stays continuous. | ||
| git_tag_name = "v{{ version }}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| //! Process-wide `jsonwebtoken` crypto provider selection. | ||
| //! | ||
| //! `jsonwebtoken` infers its provider from its own two backend features, and | ||
| //! with both on it cannot: it falls back to a provider that panics on first | ||
| //! use. Cargo features being additive, that combination arrives on its own. | ||
| //! Either this crate's `rust_crypto` and `aws_lc_rs` are both enabled (two | ||
| //! dependents asking for different backends, or `--all-features`, which is what | ||
| //! docs.rs and `cargo-semver-checks` use), or one of ours is enabled while | ||
| //! another crate in the graph turns on the other `jsonwebtoken` feature | ||
| //! directly. The second case looks single-backend from here, so the provider is | ||
| //! installed explicitly whichever backend this crate compiled with. | ||
|
|
||
| #[cfg(test)] | ||
| mod tests; | ||
|
|
||
| /// The provider this crate installs. | ||
| /// | ||
| /// `aws_lc_rs` wins whenever it is compiled in: it is constant-time and | ||
| /// advisory-free, while `rust_crypto` pulls in `rsa` (RUSTSEC-2023-0071). | ||
| #[cfg(feature = "aws_lc_rs")] | ||
| pub(crate) fn preferred_provider() -> &'static jsonwebtoken::crypto::CryptoProvider { | ||
| &jsonwebtoken::crypto::aws_lc::DEFAULT_PROVIDER | ||
| } | ||
|
|
||
| /// The provider this crate installs: RustCrypto, the only backend this build | ||
| /// compiled with. | ||
| #[cfg(all(feature = "rust_crypto", not(feature = "aws_lc_rs")))] | ||
| pub(crate) fn preferred_provider() -> &'static jsonwebtoken::crypto::CryptoProvider { | ||
| &jsonwebtoken::crypto::rust_crypto::DEFAULT_PROVIDER | ||
| } | ||
|
|
||
| /// Select the `jsonwebtoken` crypto provider for this process. | ||
| /// | ||
| /// Call it once at startup, before anything in the process signs or verifies a | ||
| /// JWT. It is idempotent, and installs the backend this crate was built with, | ||
| /// so `jsonwebtoken` never has to infer one. | ||
| /// | ||
| /// A plain [`ProxyServer`](crate::ProxyServer) deployment needs no call: the | ||
| /// server does this while it is being built. It is public for the case the | ||
| /// server cannot cover, which is also how the ambiguity arises in the first | ||
| /// place: another crate in the graph uses `jsonwebtoken` too and may reach it | ||
| /// first. Call this at the top of `main` and every consumer is covered, | ||
| /// whichever runs first. | ||
| /// | ||
| /// With both backends linked the choice is `aws_lc_rs`: constant-time, and free | ||
| /// of the `rsa` advisory `rust_crypto` carries. A process that wants a different | ||
| /// one installs it through | ||
| /// [`CryptoProvider::install_default`](jsonwebtoken::crypto::CryptoProvider::install_default) | ||
| /// before calling this, and the earlier choice stands. | ||
| pub fn install_default_crypto_provider() { | ||
| if preferred_provider().install_default().is_err() { | ||
| // Something installed a provider before us: an embedder that made its | ||
| // own choice, or an earlier call here. Either way it stands: the | ||
| // process gets one provider, and the first explicit choice is the one | ||
| // the caller meant. | ||
| tracing::debug!("jsonwebtoken crypto provider already installed; keeping it"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /// The selection itself, read before anything installs a provider. Verifying a | ||
| /// token proves only that *some* provider is installed (both backends do | ||
| /// EdDSA), so the choice is asserted here at its own seam. | ||
| /// | ||
| /// With both backends linked it must be `aws_lc_rs`, the constant-time and | ||
| /// advisory-free one. | ||
| #[cfg(all(feature = "rust_crypto", feature = "aws_lc_rs"))] | ||
| #[test] | ||
| fn tie_break_selects_aws_lc_rs() { | ||
| let selected = super::preferred_provider(); | ||
| assert!( | ||
| std::ptr::eq(selected, &jsonwebtoken::crypto::aws_lc::DEFAULT_PROVIDER), | ||
| "the tie-break must select the aws_lc_rs provider" | ||
| ); | ||
| assert!( | ||
| !std::ptr::eq( | ||
| selected, | ||
| &jsonwebtoken::crypto::rust_crypto::DEFAULT_PROVIDER | ||
| ), | ||
| "the tie-break must not fall back to RustCrypto" | ||
| ); | ||
| } | ||
|
|
||
| /// A single-backend build installs that backend rather than leaving | ||
| /// `jsonwebtoken` to infer it: another crate in the graph may have turned on the | ||
| /// other `jsonwebtoken` feature directly, which is invisible from here and makes | ||
| /// inference ambiguous. | ||
| #[cfg(all(feature = "aws_lc_rs", not(feature = "rust_crypto")))] | ||
| #[test] | ||
| fn aws_lc_rs_only_build_installs_aws_lc_rs() { | ||
| assert!(std::ptr::eq( | ||
| super::preferred_provider(), | ||
| &jsonwebtoken::crypto::aws_lc::DEFAULT_PROVIDER | ||
| )); | ||
| } | ||
|
|
||
| /// The same for the default build: RustCrypto is named explicitly, not inferred. | ||
| #[cfg(all(feature = "rust_crypto", not(feature = "aws_lc_rs")))] | ||
| #[test] | ||
| fn rust_crypto_only_build_installs_rust_crypto() { | ||
| assert!(std::ptr::eq( | ||
| super::preferred_provider(), | ||
| &jsonwebtoken::crypto::rust_crypto::DEFAULT_PROVIDER | ||
| )); | ||
| } | ||
|
|
||
| /// Installing twice is not an error the caller has to think about: the first | ||
| /// choice stands and the second call is a no-op. | ||
| #[test] | ||
| fn installing_twice_is_harmless() { | ||
| super::install_default_crypto_provider(); | ||
| super::install_default_crypto_provider(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.