[ 
https://issues.apache.org/jira/browse/KNOX-3578?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Sandor Molnar updated KNOX-3578:
--------------------------------
    Description: 
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.


  was:
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.

h3. Proposal

Add a new KNOXIDF service param, {{knoxidf.allowed.grant.types}} 
(comma-separated):
 * *Unset or empty:* behaviour stays as today. Every grant is allowed, 
including requests with no {{{}grant_type{}}}.
 * *Set:* {{TokenResource}} ({{{}doPost{}}} and {{{}doGet{}}}) returns {{400}} 
with {{"error": "unsupported_grant_type"}} (RFC 6749 section 5.2) for any 
request whose {{grant_type}} is missing or not on the list. The check runs 
before any grant handling, so a rejected request never consumes an auth code or 
a refresh token.
 * *Discovery:* {{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{}}}.

Example for a topology that serves only token exchange:
{code:xml}
<service>
  <role>KNOXIDF</role>
  <param>
    <name>knoxidf.allowed.grant.types</name>
    <value>urn:ietf:params:oauth:grant-type:token-exchange</value>
  </param>
</service>
{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}} 
return {{{}400 unsupported_grant_type{}}}, and the code or refresh token is not 
consumed;
 ** requests with no {{grant_type}} ({{{}POST{}}} and {{{}GET /token{}}}) 
return {{{}400 unsupported_grant_type{}}};
 ** the discovery documents list only token-exchange and leave out the 
authorization, registration and userinfo endpoints.


> 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
>
>
> 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