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

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

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

   [KNOX-3626](https://issues.apache.org/jira/browse/KNOX-3626) - Let public 
PKCE clients refresh without a client_secret
   
   ## What changes were proposed in this pull request?
   
   The KnoxIDF discovery document advertises `none` in 
`token_endpoint_auth_methods_supported`, and a public client can redeem an 
authorization code with PKCE and no `client_secret`. The `refresh_token` grant, 
however, always required a `client_secret`, so a public client (e.g. an MCP 
connector) got a refresh token it could never use and went invalid when the 
access token expired, about an hour after connecting.
   
   The token endpoint now records how the client authenticated when it started 
the token chain, and stores it server-side as `client_auth_method` metadata on 
the issued refresh token:
   - `none` when the code was redeemed with PKCE and no `client_secret`.
   - `client_secret_post` otherwise (including PKCE plus a `client_secret`).
   - Every rotation carries the presented token's value forward to the new 
refresh token.
   
   On the `refresh_token` grant:
   - The `client_secret` may be omitted only when the stored value is `none`. 
The value comes from the stored metadata, never from the request, so a 
confidential chain cannot be downgraded.
   - A `client_secret` that is presented is always verified, also on a public 
chain.
   - A refresh token without the metadata (issued before this change) still 
requires the `client_secret`. Public clients holding such a token have to 
re-authorize once after the upgrade.
   - `client_id` binding and single-use rotation are unchanged.
   
   Docs updated: `knoxidf/security.md` and `knoxidf/endpoints.md`.
   
   ## How was this patch tested?
   
   ### Unit tests
   
   `mvn -o -pl gateway-service-knoxidf verify -DskipITs` is green (354 tests, 
checkstyle, PMD, SpotBugs).
   - `TokenResourceRefreshTokenClientAuthTest`: a confidential chain without a 
secret is rejected; a public chain refreshes without a secret, rejects a wrong 
secret and still requires the matching `client_id`; PKCE without a secret 
starts a public chain, PKCE with a secret and plain secret redemption start a 
confidential chain; the refresh grant carries the method forward; a legacy 
token records none.
   - `TokenResourceRefreshTokenRotationTest`: rotation of a public chain 
carries `none` forward to the new refresh token.
   
   I also checked that disabling the new early return makes the public-chain 
tests fail.
   
   ### Manual tests
   
   Ran against a local gateway backed by PostgreSQL, using the 
`knoxidf-auth-clientregistration` (Basic auth), `knoxidf-auth-sso` (KnoxSSO 
cookie) and `knoxidf-auth-token` (JWT) topologies.
   
   Setup: register a client, log in through KnoxSSO and get an authorization 
code with an S256 challenge (consent accepted).
   
   ```bash
   GW=https://www.local.com:8443/gateway
   
   curl -sk -u admin:admin-password -X POST \
     "$GW/knoxidf-auth-clientregistration/knoxidf/api/v1/client/register" \
     --data-urlencode redirect_uris=https://localhost/callback \
     --data-urlencode allowed_scopes=openid,profile,offline_access > reg.json
   CID=$(jq -r .client_id reg.json)
   SECRET=$(jq -r .client_secret reg.json)
   
   VERIFIER=$(openssl rand -base64 48 | tr '+/' '-_' | tr -d '=\n')
   CHALLENGE=$(printf %s "$VERIFIER" | openssl dgst -sha256 -binary | base64 | 
tr '+/' '-_' | tr -d '=\n')
   
   curl -sk -u admin:admin-password -c cookies -o /dev/null \
     "$GW/knoxsso/api/v1/websso?originalUrl=$GW/knoxidf-auth-sso/"
   
   curl -sk -b cookies -G "$GW/knoxidf-auth-sso/knoxidf/api/v1/authorize" \
     --data-urlencode response_type=code \
     --data-urlencode client_id=$CID \
     --data-urlencode redirect_uri=https://localhost/callback \
     --data-urlencode "scope=openid profile offline_access" \
     --data-urlencode state=xyz \
     --data-urlencode code_challenge=$CHALLENGE \
     --data-urlencode code_challenge_method=S256
   # follow the redirect to the consent page, then:
   curl -sk -b cookies -X POST \
     "$GW/knoxidf-auth-sso/knoxidf/api/v1/authorize/consentAccepted" \
     -d state=$STATE \
     -d action=accept
   # CODE = the code parameter of the Location header
   ```
   
   The refresh token's id is its `knox.id` claim; its metadata is checked with:
   
   ```sql
   select token_id, md_name, md_value
     from knox_token_metadata
    where token_id = '<refresh token knox.id>'
      and md_name in ('client_auth_method', 'type', 'client_id');
   ```
   
   #### Positive case: public client (PKCE, no client_secret)
   
   Redeem the code with PKCE only:
   
   ```bash
   curl -sk -X POST "$GW/knoxidf-auth-token/knoxidf/api/v1/token" \
     -d grant_type=authorization_code \
     --data-urlencode code=$CODE \
     --data-urlencode client_id=$CID \
     --data-urlencode redirect_uri=https://localhost/callback \
     --data-urlencode code_verifier=$VERIFIER
   ```
   
   Refresh without a `client_secret`, then replay the old refresh token:
   
   ```bash
   curl -sk -X POST "$GW/knoxidf-auth-token/knoxidf/api/v1/token" \
     -d grant_type=refresh_token \
     --data-urlencode client_id=$CID \
     --data-urlencode refresh_token=$RT
   ```
   
   | Step | Result |
   |---|---|
   | Redeem code with PKCE, no `client_secret` | 200; refresh token `d757165e…` 
stored with `client_auth_method=none` |
   | Refresh, no `client_secret` | 200; rotated refresh token `969ca1dd…` 
stored with `client_auth_method=none` |
   | Replay the old refresh token | 401 `Unknown token` (consumed by rotation) |
   
   #### Negative cases
   
   Redeem a new code with PKCE **and** the `client_secret`, to start a 
confidential chain:
   
   ```bash
   curl -sk -X POST "$GW/knoxidf-auth-token/knoxidf/api/v1/token" \
     -d grant_type=authorization_code \
     --data-urlencode code=$CODE \
     --data-urlencode client_id=$CID \
     --data-urlencode client_secret=$SECRET \
     --data-urlencode redirect_uri=https://localhost/callback \
     --data-urlencode code_verifier=$VERIFIER
   ```
   
   Refresh with no, a wrong, and the correct `client_secret`. The wrong secret 
has the wire format of a real one 
(`base64(base64(client_id)::base64(passcode))`) with a bogus passcode:
   
   ```bash
   WRONG=$(printf '%s::%s' "$(printf %s $CID | base64)" "$(printf %s 
not-the-passcode | base64)" | base64 | tr -d '\n')
   
   curl -sk -X POST "$GW/knoxidf-auth-token/knoxidf/api/v1/token" \
     -d grant_type=refresh_token \
     --data-urlencode client_id=$CID \
     --data-urlencode refresh_token=$RT \
     --data-urlencode client_secret=$WRONG   # or $SECRET, or omitted
   ```
   
   To simulate a refresh token issued before this change, the 
`client_auth_method` row of the public-chain token `969ca1dd…` was deleted:
   
   ```sql
   delete from knox_token_metadata
    where token_id = '969ca1dd-735a-4f34-a9a9-cedd63f24c97'
      and md_name = 'client_auth_method';
   ```
   
   | # | Step | Result |
   |---|---|---|
   | 0 | Redeem code with PKCE **and** `client_secret` | 200; refresh token 
`d4ec0432…` stored with `client_auth_method=client_secret_post` |
   | 1 | Confidential chain, no `client_secret` | 400 `invalid_grant` ("Invalid 
grant: client authentication failed") |
   | 2 | Confidential chain, wrong `client_secret` | 400 `invalid_grant` |
   | 3 | Confidential chain, correct `client_secret` | 200 (audit 
`reason=rotated`); rotated refresh token `5256b09a…` keeps 
`client_auth_method=client_secret_post` |
   | 4 | Public chain, wrong `client_secret` | 400 `invalid_grant` |
   | 5 | Pre-change refresh token (no `client_auth_method`), no `client_secret` 
| 400 `invalid_grant` |
   
   The gateway audit log showed the matching `validation_failed` entries for 1, 
2 and 4 and the `rotated` entry for 3.
   
   ## Integration Tests
   
   None added. The existing `test_knoxidf.py` refresh tests use a 
`client_secret`, so they are unaffected (not re-run locally); the public-client 
flow is covered by the unit tests and the manual run above. No opt-in label 
suites are needed.
   
   ## Performance implications
   
   None. Issuing a refresh token stores one more metadata entry with it, and 
the refresh grant reads it from metadata it already loads. No new remote or DB 
calls.
   




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

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

> KnoxIDF: Allow the refresh_token grant without client_secret for public 
> (PKCE) clients
> --------------------------------------------------------------------------------------
>
>                 Key: KNOX-3626
>                 URL: https://issues.apache.org/jira/browse/KNOX-3626
>             Project: Apache Knox
>          Issue Type: Improvement
>          Components: KnoxIDF
>            Reporter: Sandor Molnar
>            Assignee: Sandor Molnar
>            Priority: Major
>             Fix For: 3.1.0
>
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> h3. Problem
> The KnoxIDF authorization server metadata advertises {{"none"}} in 
> {{token_endpoint_auth_methods_supported}}, and the authorization_code grant 
> honours it: a client that presents a valid PKCE {{code_verifier}} gets tokens 
> without a {{client_secret}} ({{TokenResource#validateAuthCode}}).
> The refresh_token grant does not honour it. 
> {{TokenResource#validateRefreshTokenGrant}} always calls 
> {{isValidClientSecret}}, which returns false for a blank secret, so every 
> secretless refresh fails with {{invalid_grant}} ("Invalid grant: client 
> authentication failed", audited as {{reason=validation_failed}}).
> Per RFC 8414, {{token_endpoint_auth_methods_supported}} describes the token 
> endpoint as a whole, not individual grants. A public client that relies on 
> the advertised {{"none"}} method therefore connects successfully, then loses 
> access when its first access token expires (after 
> {{knoxidf.knox.token.ttl}}), because it can never refresh.
> h3. Steps to reproduce
> # Register a client via KnoxIDF client registration.
> # Use it as a public client: authorization_code + PKCE (S256), no 
> {{client_secret}}. The token request succeeds and returns an access token and 
> a refresh token.
> # Call the token endpoint with {{grant_type=refresh_token}}, {{client_id}} 
> and the refresh token, without {{client_secret}}.
> *Expected:* new access token and rotated refresh token.
> *Actual:* HTTP 400 {{invalid_grant}}. The refresh token is valid (enabled, 
> not expired, matching {{client_id}}).
> Workaround: send the {{client_secret}} from the registration response as 
> {{client_secret_post}} on the refresh call.
> h3. Proposed fix
> Removing the secret check unconditionally is not acceptable: it guards 
> against redeeming a stolen refresh token from a confidential client, since 
> the JWTFederationFilter Bearer path forwards to the token endpoint without 
> checking {{client_secret}}. Instead, bind the client-authentication method to 
> the refresh token server-side:
> # On the authorization_code grant, record how the client authenticated (PKCE 
> only vs. {{client_secret}}) in the issued refresh token's metadata, e.g. 
> {{client_auth=none|client_secret_post}}.
> # On the refresh_token grant, skip {{isValidClientSecret}} only when the 
> presented refresh token's stored metadata says {{none}}; otherwise keep 
> requiring the secret. Carry the value forward on every rotation.
> # Never derive this from a request parameter, so a refresh token from a 
> secret-authenticated chain cannot be downgraded to secretless redemption.
> Behaviour for confidential clients is unchanged. Do not remove {{"none"}} 
> from the metadata: the PKCE-only authorization_code path is correct and used.
> h3. Related hardening (can be split out)
> RFC 9700 §4.14.2 allows secretless refresh for public clients when refresh 
> tokens are sender-constrained or rotated with reuse detection. KnoxIDF 
> already rotates refresh tokens single-use ({{consumeToken}}), but a replayed 
> token only gets {{invalid_grant}}; the descendant token stays valid. Revoking 
> the refresh-token chain on detected reuse would make the public-client path 
> fully compliant.



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

Reply via email to