[ 
https://issues.apache.org/jira/browse/KNOX-3578?focusedWorklogId=1055142&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-1055142
 ]

ASF GitHub Bot logged work on KNOX-3578:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 16/Nov/26 23:33
            Start Date: 16/Nov/26 23:33
    Worklog Time Spent: 10m 
      Work Description: smolnar82 opened a new pull request, #1515:
URL: https://github.com/apache/knox/pull/1515

   [KNOX-3578](https://issues.apache.org/jira/browse/KNOX-3578) - Add 
jwt.allowed.grant.types to restrict token endpoint grant types
   
   ## What changes were proposed in this pull request?
   
   A new `JWTProvider` topology param, `jwt.allowed.grant.types`, takes a 
comma-separated list of the `grant_type` values the topology's token endpoint 
accepts. When it is unset or blank, nothing changes.
   
   Today a topology such as an exchange-only KnoxIDF token endpoint accepts 
every grant. `JWTFederationFilter` also does each grant's credential work 
(passcode, `client_assertion`, refresh token or `subject_token` validation, 
possibly a JWKS fetch) before KnoxIDF ever sees the request.
   
   With the param set:
   
   - **JWTProvider**
     - A request with a present, non-allowed `grant_type` is rejected with `400 
{"error":"unsupported_grant_type"}` **before any credential is validated**. 
This applies even when an `Authorization` header is present.
     - The rejection is audited as `event=token_grant grant_type=<grant> 
reason=grant_type_not_allowed`. Unknown grant values are logged as `other`, so 
caller input never reaches the audit log.
     - The parsed list is passed to the service in the `X-Allowed-Grant-Types` 
request attribute. The filter sets it before the unauthenticated-path bypass, 
so anonymous discovery sees it too.
     - A missing grant_type is not rejected by the filter, since it can't tell 
a token request from other traffic on the topology; KnoxIDF's token endpoint 
rejects it instead.
   - **KnoxIDF**
     - The token endpoint (GET and POST) rejects a missing or non-allowed 
`grant_type`. This closes the bearer-token re-mint on restricted topologies.
     - The discovery documents (`openid-configuration` and 
`oauth-authorization-server`) advertise only the listed grants in 
`grant_types_supported`. Without `authorization_code`, 
`authorization_endpoint`, `registration_endpoint` and `userinfo_endpoint` are 
omitted.
     - A KnoxIDF behind another provider sees no attribute and keeps today's 
behaviour.
   - **Shared code:** the shared parser `AllowedGrantTypes` lives in 
`gateway-util-common`.
   - **Docs:** the new page `knox-site/docs/config_jwt_allowed_grant_types.md`, 
listed under Providers in `mkdocs.yml`. It includes a caveat: with the param 
set, the provider reads the form body of every authenticated request, so it is 
meant for token-endpoint topologies, not ones that proxy form posts.
   
   ## How was this patch tested?
   
   - New unit tests:
     - `AllowedGrantTypesTest`.
     - `JWTFederationFilterAllowedGrantTypesTest`. It covers:
       - rejection before credential parsing for `refresh_token`, 
`authorization_code` and `client_credentials` (malformed secret, HTTP Basic, 
`client_assertion`);
       - a bearer token with a disallowed grant;
       - pass-through of a missing grant;
       - the attribute on unauthenticated paths, without reading the body;
       - no behaviour change when the param is unset;
       - the audit record.
     - `TokenResourceAllowedGrantTypesTest`.
   - Extended unit tests: `OAuthServerMetadataTest` and 
`DiscoveryResourceMetadataTest`.
   - `mvn -pl 
gateway-util-common,gateway-provider-security-jwt,gateway-service-knoxidf -am 
verify` is green: 194, 740 and 274 tests with 0 failures, and checkstyle, PMD, 
spotbugs and forbiddenapis pass.
   
   ## Integration Tests
   
   - A new topology, `knoxidf-token-exchange-only.xml`. It is 
`knoxidf-token.xml` restricted to 
`urn:ietf:params:oauth:grant-type:token-exchange`, with anonymous discovery and 
JWKS. It is added to the test image's `Dockerfile`.
   - Five new tests in `test_token_exchange.py`:
     - same-subject exchange still succeeds;
     - `refresh_token`, `client_credentials` and `authorization_code` are 
rejected with `unsupported_grant_type`;
     - the rejection is audited;
     - a bearer request without `grant_type` is rejected;
     - discovery advertises only token exchange.
   - Results: the full Docker Compose suite passed 162 tests, 
`test_token_exchange.py` passed 17/17, and pylint scored 10/10.
   
   ```
   tests-1  | Your code has been rated at 10.00/10
   tests-1  | 
   tests-1  | Waiting for knox...
   tests-1  | ============================= test session starts 
==============================
   tests-1  | platform linux 

Issue Time Tracking
-------------------

            Worklog Id:     (was: 1055142)
    Remaining Estimate: 0h
            Time Spent: 10m

> KnoxIDF: allow restricting the grant types accepted by the token endpoint
> -------------------------------------------------------------------------
>
>                 Key: KNOX-3578
>                 URL: https://issues.apache.org/jira/browse/KNOX-3578
>             Project: Apache Knox
>          Issue Type: Improvement
>            Reporter: Sandor Molnar
>            Assignee: Sandor Molnar
>            Priority: Major
>             Fix For: 3.1.0
>
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> h3. Problem
> The KnoxIDF {{TokenResource}} accepts every grant type on every topology that 
> serves the KNOXIDF service:
> * {{authorization_code}} and {{refresh_token}} are handled by KnoxIDF itself.
> * Every other request goes to the KNOXTOKEN mint ({{super.doPost()}} / 
> {{doGet()}}). That includes {{client_credentials}}, 
> {{urn:ietf:params:oauth:grant-type:token-exchange}}, and requests with no 
> {{grant_type}} at all (a bearer {{POST /token}}, or {{GET /token}}).
> Auth codes, refresh tokens and registered clients are kept in the 
> gateway-wide token state service. 
> {{PasscodeTokenResourceBase#setupTokenStateService}} always enables 
> server-managed state for KnoxIDF. As a result, a code issued by 
> {{/authorize}} on one topology can be redeemed on any other topology that 
> serves KNOXIDF. The tokens it gets back use that topology's issuer and 
> audience policy.
> This becomes a problem when a deployment splits KnoxIDF across topologies, 
> for example one topology for the authorization code flow and another only for 
> RFC 8693 token exchange with 
> {{knoxidf.knox.token.audience.validator=passthrough}}. On the token exchange 
> topology:
> * An auth code obtained on the authorization topology can be redeemed. The 
> resulting tokens carry the token exchange topology's issuer, and any 
> requested {{resource}} becomes the {{aud}}.
> * Any accepted JWT presented as a bearer with no {{grant_type}} is re-minted 
> with any requested audience. The incoming {{act}} chain is kept, so a 
> delegated token can be re-targeted without the requested-audience 
> authorization that {{TokenExchangeHandler}} enforces.
> * Discovery advertises all four grant types, because 
> {{AuthorizationServerMetadata}} hardcodes {{grant_types_supported}}. It also 
> advertises the authorization, registration and userinfo endpoints, even 
> though this topology isn't meant to serve them.
> A check in the KnoxIDF service alone would come too late. By the time 
> {{TokenResource}} runs, {{JWTFederationFilter}} has already validated the 
> grant's credentials: the {{client_secret}} or {{client_assertion}}, the 
> refresh token, or the {{subject_token}} (possibly fetching a JWKS). A grant 
> the topology doesn't serve should be refused before any of that work.
> h3. Proposal
> Add a new JWTProvider param, {{jwt.allowed.grant.types}} (comma-separated):
> * *Unset or empty:* behaviour stays as today. Every grant is allowed, 
> including requests with no {{grant_type}}.
> * *Set, in {{JWTFederationFilter}}:* a request whose {{grant_type}} (body or 
> query) is present but not on the list gets {{400}} with {{"error": 
> "unsupported_grant_type"}} (RFC 6749 section 5.2). This happens before any 
> credential is validated, so nothing is verified, looked up or consumed. The 
> filter reads {{grant_type}} even when an {{Authorization}} header is present. 
> Requests with no {{grant_type}} pass through unchanged, because the filter 
> can't tell a token request from a userinfo, registration or proxied request.
> * *Set, in KnoxIDF:* the filter passes the parsed list to the service as a 
> request attribute, also on {{jwt.unauthenticated.path.list}} paths. KnoxIDF 
> uses that list and has no setting of its own:
> ** {{TokenResource}} ({{doPost}} and {{doGet}}) returns {{400 
> unsupported_grant_type}} when {{grant_type}} is missing or not on the list. 
> This closes the bearer re-mint.
> ** {{AuthorizationServerMetadata}} advertises only the allowed grants in 
> {{grant_types_supported}}. When {{authorization_code}} isn't allowed, it also 
> leaves out {{authorization_endpoint}}, {{registration_endpoint}} and 
> {{userinfo_endpoint}}.
> * When set, the filter parses the form body of requests on that topology to 
> read {{grant_type}}. The param is meant for topologies that serve the token 
> endpoint, not for ones that proxy form posts to backends.
> * KnoxIDF behind a different federation provider gets no list and behaves as 
> today. That includes a discovery document served from an SSOCookieProvider 
> topology.
> Example for a topology that serves only token exchange:
> {code:xml}
> <provider>
>   <role>federation</role>
>   <name>JWTProvider</name>
>   <enabled>true</enabled>
>   <param>
>     <name>jwt.allowed.grant.types</name>
>     <value>urn:ietf:params:oauth:grant-type:token-exchange</value>
>   </param>
>   <param>
>     <name>jwt.unauthenticated.path.list</name>
>     
> <value>/knoxidf/api/v1/.well-known/openid-configuration,/knoxidf/api/v1/jwks</value>
>   </param>
> </provider>
> {code}
> h3. Tests
> * Param unset: behaviour is unchanged for every grant type.
> * Param set to token-exchange:
> ** token exchange still succeeds;
> ** {{authorization_code}}, {{refresh_token}} and {{client_credentials}} 
> (client secret in the body or over HTTP Basic, or {{client_assertion}}) 
> return {{400 unsupported_grant_type}} from the filter. No credential is 
> validated and the code or refresh token is not consumed;
> ** the same holds when a bearer {{Authorization}} header is also present;
> ** requests with no {{grant_type}} pass the filter, and the KnoxIDF token 
> endpoint rejects them ({{POST}} and {{GET /token}}) with {{400 
> unsupported_grant_type}};
> ** the discovery documents, served on unauthenticated paths, list only 
> token-exchange and leave out the authorization, registration and userinfo 
> endpoints.
> * KnoxIDF without the request attribute (behind another provider) behaves as 
> today.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to