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

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

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

   [KNOX-3601](https://issues.apache.org/jira/browse/KNOX-3601) - Reject 
CLIENTID client secrets presented as access tokens
   
   ## What changes were proposed in this pull request?
   
   A client secret issued by the CLIENTID service is a long-lived grant 
credential: it should be exchanged for a short-lived access token through the 
OAuth 2.0 `client_credentials` grant. Until now `JWTFederationFilter` also 
accepted it directly as an access token, as `Authorization: Bearer 
<client_secret>` or as HTTP Basic with the `Passcode` username, because client 
secrets and API keys share the same passcode encoding.
   
   With this change:
   - `JWTFederationFilter` rejects a CLIENTID client secret presented directly 
as an access token with `401 Unauthorized` and the message "A client secret is 
not an access token; exchange it for one using the client_credentials grant".
   - The check runs right after the passcode is decoded and before 
`validateToken`, so a secret that is going to be rejected is never verified.
   - Client secrets are told apart from API keys by 
`TokenMetadata.isClientId()`; API keys keep working as Bearer / Basic 
`Passcode` tokens.
   - Client secrets are still accepted in the `client_credentials` grant, both 
in the form body (`client_id` / `client_secret`) and as HTTP Basic 
`client_id:client_secret`. `WireToken` now records whether the token came from 
a `client_credentials` grant (`WireToken.clientSecret(...)`), and the grant 
parsing methods return `WireToken` directly.
   - A new `JWTProvider` param, 
`knox.token.allow.client.secret.as.access.token` (default `false`), restores 
the old behavior on a topology that still needs it.
   - The rejection is logged at WARN with the token id display text only 
(`Rejected client secret of {0} used as an access token`); the param name is 
deliberately not in the log, it is documented instead.
   - The check needs the token state service, so it only applies when 
`knox.token.exp.server-managed` is on; without it a client secret cannot be 
identified as such and nothing changes.
   - Docs: new "Using the client secret" section in 
`config_client_credentials.md` with the grant examples, the 401 behavior and 
the new param.
   
   Known limitation (not addressed here): `getParameter()` merges the query 
string with the form body, so HTTP Basic `client_id:client_secret` with 
`?grant_type=client_credentials` in the URL counts as a `client_credentials` 
grant on any path. That form has worked since KNOX-3260 / KNOX-3312 and shipped 
in 3.0.0, so closing it would be a separate, compatibility-relevant decision. 
The impact is low: whoever holds the secret can always exchange it for an 
access token.
   
   ## How was this patch tested?
   
   - Unit tests:
     - `JWTFederationFilterTest`: a client secret is rejected as Bearer and as 
Basic `Passcode` (and is never validated), is still rejected as Bearer when 
`grant_type=client_credentials` is present, and is accepted in both forms when 
`knox.token.allow.client.secret.as.access.token=true`. The existing passcode 
tests cover API keys and non-server-managed topologies.
     - `OAuthFlowsFederationFilterTest`: a client secret is accepted in the 
`client_credentials` grant, in the form body and as HTTP Basic.
     - `HadoopAuthFilterTest`: the strict filter config mock expects the new 
param.
   - `mvn verify` on `gateway-provider-security-jwt` and 
`gateway-provider-security-hadoopauth`.
   - Manual tests against a local gateway with CLIENTID and APIKEY credentials:
     - `client_credentials` grant at `oauth/v1/token`, body and HTTP Basic: 200
     - client secret as Bearer and as Basic `Passcode`: 401, with the WARN log 
line
     - API key as Bearer: 200
     - client secret as Bearer on a topology with 
`knox.token.allow.client.secret.as.access.token=true`: 200
   
   ## Integration Tests
   
   Extended `test_clientid_credentials.py` and added the 
`knoxtoken-allow-client-secret` topology (`knoxtoken` plus 
`knox.token.allow.client.secret.as.access.token=true`):
   - `test_supplied_client_id_authenticates` used to present the client secret 
as Basic `Passcode` to `knoxtoken`, which is now rejected. It now exchanges the 
secret through the `client_credentials` grant, checks that the access token's 
`sub` is the custom client id, and authenticates with that access token.
   - `test_client_secret_exchanged_through_client_credentials_grant`: the grant 
accepts the secret in the body and as HTTP Basic.
   - `test_client_secret_rejected_as_access_token`: Bearer and Basic `Passcode` 
get 401 with the error message on `knoxtoken`.
   - `test_client_secret_accepted_as_access_token_when_allowed`: both forms are 
accepted on `knoxtoken-allow-client-secret`.
   - `test_api_key_accepted_as_access_token`: an API key (minted at 
`tokenlimitbytype` for a fresh `doAs` user) is still accepted in both forms.
   
   ```
   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: 1056198)
    Remaining Estimate: 0h
            Time Spent: 10m

> JWTFederationFilter accepts a CLIENT_ID client_secret as a direct access token
> ------------------------------------------------------------------------------
>
>                 Key: KNOX-3601
>                 URL: https://issues.apache.org/jira/browse/KNOX-3601
>             Project: Apache Knox
>          Issue Type: Improvement
>          Components: JWT, KnoxIDF
>            Reporter: Sandor Molnar
>            Assignee: Sandor Molnar
>            Priority: Major
>             Fix For: 3.1.0
>
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> h3. Problem
> Client secrets minted by the CLIENTID service are stored in TokenStateService 
> as server-managed passcodes (TokenMetadataType CLIENT_ID). They use the same 
> encoded form as API keys: Base64(Base64(tokenId)::Base64(passcode)). On a 
> topology with {{{}knox.token.exp.server-managed=true{}}}, JWTFederationFilter 
> accepts that secret as {{Authorization: Bearer <client_secret>}} (or 
> {{{}Basic Passcode:<client_secret>{}}}) and establishes a full session. The 
> intended OAuth flow is mint -> client_credentials grant -> short-lived JWT, 
> but here the secret itself works as the access token.
> This does not grant extra privileges, because the subject and roles are the 
> same as for the exchanged JWT. It does mean the long-lived secret travels on 
> every API call instead of only to the token endpoint, which increases the 
> chance of it leaking.
> h3. Steps to reproduce
>  # Mint credentials via the CLIENTID API.
>  # Call a protected resource on a server-managed JWTProvider topology with 
> {{{}Authorization: Bearer <client_secret>{}}}, without calling the token 
> endpoint -> 200 as the credential owner.
>  # {{client_id}} alone as Bearer -> 401 (expected).
>  # After revoke, the same Bearer secret -> 401 (expected).
>  # {{grant_type=client_credentials}} with client_id/client_secret (body or 
> Basic) -> JWT issued (expected, must keep working).
> h3. Root cause
> In the passcode branch of JWTFederationFilter.doFilter, validateToken() 
> checks expiry, enabled state, idle timeout and the passcode, and 
> createSubjectFromTokenIdentifier() builds the subject. Neither one 
> distinguishes CLIENT_ID from API_KEY metadata when the passcode is the 
> caller's own credential.
> h3. Proposed fix
>  * After passcode validation succeeds, reject (401) a CLIENT_ID passcode 
> unless parseWireToken read it from a client_credentials grant (body 
> {{{}client_secret{}}}, or Basic {{<client_id>:<secret>}} with 
> {{{}grant_type=client_credentials{}}}). Bearer and Basic {{Passcode:}} are 
> both rejected.
>  * Record that provenance during parsing, as WireToken already does. Do not 
> re-derive it later from {{{}request.getParameter("grant_type"){}}}: otherwise 
> {{?grant_type=client_credentials}} on a Bearer request would bypass the check.
>  * Apply the check to all CLIENT_ID metadata, regardless of the thirdPartyApp 
> flag.
>  * Add a provider param, e.g. 
> {{knox.token.allow.client.secret.as.access.token}} (default false). Setting 
> it to true restores the current behavior for compatibility.
>  * Leave API_KEY passcodes, JWT bearer tokens, refresh_token and 
> token-exchange unchanged.
> h3. Acceptance criteria
>  * CLIENT_ID passcode presented as Bearer or Basic {{Passcode}} -> 401 with a 
> clear message (e.g. "exchange client credentials for an access token first").
>  * Same request with a {{grant_type=client_credentials}} query param -> still 
> 401.
>  * client_credentials grant with client_id/client_secret (body or Basic) -> 
> 200 + JWT.
>  * API_KEY passcode as Bearer -> 200 (unchanged).
>  * JWT Bearer -> 200 (unchanged).
>  * Opt-in param set to true -> CLIENT_ID passcode as Bearer accepted (current 
> behavior).
>  * Unit tests cover each case above.



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

Reply via email to