-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
[rust-server] Restrict from_headers matches to the intended auth scheme #24607
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
Open
twistali
wants to merge
2
commits into
OpenAPITools:master
Choose a base branch
from
twistali:fix/rust-server-untyped-from-headers-auth-bypass
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+269
−8
Open
Changes from all commits
Commits
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
43 changes: 43 additions & 0 deletions
43
modules/openapi-generator/src/test/resources/3_0/rust-server/overlapping-auth-schemes.yaml
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,43 @@ | ||
| openapi: 3.0.1 | ||
| info: | ||
| title: overlapping auth schemes test | ||
| version: '1.0' | ||
| servers: | ||
| - url: 'http://localhost:8080/' | ||
| paths: | ||
| /ping: | ||
| get: | ||
| operationId: pingGet | ||
| responses: | ||
| '201': | ||
| description: OK | ||
| components: | ||
| # This spec exists to exercise the auth-scheme blocks generated into context.rs when | ||
| # several schemes compete for the same request. See issue #24095. | ||
| # | ||
| # Two properties matter, and no other rust-server fixture has both: | ||
| # | ||
| # * `basicAuth` and `bearerAuth` are HTTP schemes that both read the `Authorization` | ||
| # header, so an unrestricted block for either one also matches the other. This is | ||
| # the `isBasicBasic` / `isBasicBearer` pairing; the petstore fixture only covers | ||
| # `isBasicBasic` alongside `isOAuth`. | ||
| # * `apiKeyAuth` is declared last. Blocks are emitted in declaration order and each | ||
| # returns early, so an unrestricted Basic or Bearer block does not merely pick the | ||
| # wrong scheme - it makes the apiKey block below it unreachable, which is an | ||
| # authorization bypass rather than a mislabelling. | ||
| securitySchemes: | ||
| basicAuth: | ||
| scheme: basic | ||
| type: http | ||
| bearerAuth: | ||
| scheme: bearer | ||
| bearerFormat: token | ||
| type: http | ||
| apiKeyAuth: | ||
| type: apiKey | ||
| name: x-api-key | ||
| in: header | ||
| security: | ||
| - basicAuth: [] | ||
| - bearerAuth: [] | ||
| - apiKeyAuth: [] |
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
116 changes: 116 additions & 0 deletions
116
...er/output/petstore-with-fake-endpoints-models-for-testing/tests/auth_scheme_precedence.rs
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,116 @@ | ||
| //! Runtime regression tests for auth-scheme precedence in the generated `AddContext` middleware. | ||
| //! | ||
| //! `swagger::auth::from_headers` returns an *untyped* `AuthData`, matching an | ||
| //! `Authorization` header that carries either `Basic` or `Bearer` credentials. Every | ||
| //! generated auth block returns early once it matches, so a block that does not check | ||
| //! which variant it received will claim credentials belonging to a different scheme and | ||
| //! prevent every later block - including API-key blocks - from ever running. | ||
| //! | ||
| //! This spec generates the blocks in the following order, which is what makes the | ||
| //! behaviour observable from the outside: | ||
| //! | ||
| //! 1. `petstore_auth` - OAuth2, reads `Authorization: Bearer` | ||
| //! 2. `api_key` - API key, reads the `api_key` header | ||
| //! 3. `api_key_query` - API key, reads the `api_key_query` query parameter | ||
| //! 4. `http_basic_test` - HTTP Basic, reads `Authorization: Basic` | ||
| //! | ||
| //! Presenting Basic credentials alongside an API key therefore proves whether block 1 | ||
| //! stays in its lane: if it wrongly claims the Basic credentials it also swallows | ||
| //! blocks 2 and 3. | ||
|
|
||
| #![cfg(feature = "server")] | ||
|
|
||
| use std::sync::{Arc, Mutex}; | ||
|
|
||
| use hyper::service::Service; | ||
| use hyper::{Request, Response}; | ||
| use petstore_with_fake_endpoints_models_for_testing::context::AddContext; | ||
| use swagger::auth::AuthData; | ||
| use swagger::{EmptyContext, Has}; | ||
|
|
||
| /// Innermost service: records the `Option<AuthData>` that `AddContext` pushed onto the context. | ||
| #[derive(Clone, Default)] | ||
| struct CaptureAuthData(Arc<Mutex<Option<AuthData>>>); | ||
|
|
||
| impl<C, ReqBody> Service<(Request<ReqBody>, C)> for CaptureAuthData | ||
| where | ||
| C: Has<Option<AuthData>>, | ||
| { | ||
| type Response = Response<String>; | ||
| type Error = std::convert::Infallible; | ||
| type Future = std::future::Ready<Result<Self::Response, Self::Error>>; | ||
|
|
||
| fn call(&self, (_request, context): (Request<ReqBody>, C)) -> Self::Future { | ||
| let auth_data: &Option<AuthData> = context.get(); | ||
| *self.0.lock().expect("lock poisoned") = auth_data.clone(); | ||
| std::future::ready(Ok(Response::new(String::new()))) | ||
| } | ||
| } | ||
|
|
||
| /// Drives a request through `AddContext` and returns the `AuthData` it resolved. | ||
| fn resolve_auth_data(uri: &str, headers: &[(&str, &str)]) -> Option<AuthData> { | ||
| let capture = CaptureAuthData::default(); | ||
| let service = AddContext::<_, EmptyContext>::new(capture.clone()); | ||
|
|
||
| let mut builder = Request::get(uri); | ||
| for (name, value) in headers { | ||
| builder = builder.header(*name, *value); | ||
| } | ||
| let request = builder.body(()).expect("request should build"); | ||
|
|
||
| futures::executor::block_on(service.call(request)).expect("service call should succeed"); | ||
|
|
||
| let resolved = capture.0.lock().expect("lock poisoned").clone(); | ||
| resolved | ||
| } | ||
|
|
||
| /// `dXNlcjpwYXNzd29yZA==` is `user:password`. | ||
| const BASIC_HEADER: &str = "Basic dXNlcjpwYXNzd29yZA=="; | ||
|
|
||
| #[test] | ||
| fn bearer_block_does_not_claim_basic_credentials() { | ||
| // The OAuth2 (Bearer) block is generated first. It must ignore Basic credentials and | ||
| // let them fall through to the HTTP Basic block generated last. | ||
| assert_eq!( | ||
| resolve_auth_data("/", &[("authorization", BASIC_HEADER)]), | ||
| Some(AuthData::Basic("user".to_owned(), "password".to_owned())), | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn basic_block_does_not_claim_bearer_credentials() { | ||
| assert_eq!( | ||
| resolve_auth_data("/", &[("authorization", "Bearer some-token")]), | ||
| Some(AuthData::Bearer("some-token".to_owned())), | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn header_api_key_is_reachable_when_basic_credentials_are_also_present() { | ||
| // Regression test: an unguarded Bearer block matches the Basic credentials, returns | ||
| // early, and the `api_key` header block below it never runs. | ||
| assert_eq!( | ||
| resolve_auth_data( | ||
| "/", | ||
| &[("authorization", BASIC_HEADER), ("api_key", "header-key")], | ||
| ), | ||
| Some(AuthData::ApiKey("header-key".to_owned())), | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn query_api_key_is_reachable_when_basic_credentials_are_also_present() { | ||
| // Same regression, for the query-parameter API-key block. | ||
| assert_eq!( | ||
| resolve_auth_data( | ||
| "/?api_key_query=query-key", | ||
| &[("authorization", BASIC_HEADER)], | ||
| ), | ||
| Some(AuthData::ApiKey("query-key".to_owned())), | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn no_credentials_resolve_to_no_auth_data() { | ||
| assert_eq!(resolve_auth_data("/", &[]), None); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: The Basic+Bearer coexistence case is only validated by string-exact assertions in this Java test; there is no runtime regression test proving the
isBasicBasicandisBasicBearerblocks (the two HTTP schemes that both read the Authorization header) fall through to each other's credentials. The added petstore runtime tests cover OAuth(Bearer)+Basic where the bearer block is generated first, so they don't exercise a Basic block preceding a Bearer block. Since this is precisely the pairing the PR fixes, adding a request-level runtime test for it (mirroring the fallthrough assertions already used here) would close the gap.Prompt for AI agents