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

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

                Author: ASF GitHub Bot
            Created on: 17/Nov/26 01:34
            Start Date: 17/Nov/26 01:34
    Worklog Time Spent: 10m 
      Work Description: smolnar82 commented on code in PR #1515:
URL: https://github.com/apache/knox/pull/1515#discussion_r4594113552


##########
gateway-provider-security-jwt/src/main/java/org/apache/knox/gateway/provider/federation/jwt/filter/JWTFederationFilter.java:
##########
@@ -318,7 +343,8 @@ public void doFilter(ServletRequest request, 
ServletResponse response, FilterCha
     // RFC 8693 token exchange: getWireToken flags this via 
TokenType.TokenExchange when the
     // grant_type is in the request body. The subject_token/actor_token are 
read from the unwrapped
     // request by the handler. Reading the body only happens on this 
(header-less) grant-flow path,
-    // so a proxied backend's body is never consumed by the 
header-authenticated path.
+    // so a proxied backend's body is never consumed by the 
header-authenticated path (unless
+    // ALLOWED_GRANT_TYPES is set).
     if (wireToken != null && 
TokenType.TokenExchange.equals(wireToken.getType())) {

Review Comment:
   > So this means that any use of allowed grant types setting restricts the 
entire topology to only Knox's own APIs. If you try to proxy access to another 
service, it will have the body consumed already and fail. Right?
   > 
   > Would this be sufficiently clear to an admin?
   
   I hope this is clear enough. I updated the documentation and called it out.





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

    Worklog Id:     (was: 1055156)
    Time Spent: 1h 10m  (was: 1h)

> 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: 1h 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