smolnar82 commented on code in PR #1553:
URL: https://github.com/apache/knox/pull/1553#discussion_r4743271756


##########
gateway-provider-security-jwt/src/main/java/org/apache/knox/gateway/provider/federation/jwt/filter/AbstractJWTFilter.java:
##########
@@ -595,7 +599,7 @@ protected Subject createSubjectFromTokenData(final String 
principal,
                                                final String 
expectedPrincipalClaimValue,
                                                final String tokenId,
                                                final List<Map<String, Object>> 
actorChain) {
-    return createSubjectFromTokenData(principal, expectedPrincipalClaimValue, 
tokenId, actorChain, null);
+    return createSubjectFromTokenData(principal, expectedPrincipalClaimValue, 
tokenId, actorChain, null, null);

Review Comment:
   Correct, and it's not limited to passcodes. Provenance lives only in the 
JWT. Credentials that authenticate by token id (passcodes, API keys, client 
secrets) are resolved from the token state store, which stores no provenance, 
so they don't carry it even when the token they stand for does.
   
   I've scoped this as a documented limit instead of a fix in this PR. 
`security.md` now says so. It advises denying the issuer of every topology that 
mints tokens for such credentials, and notes that a topology with unmanaged 
tokens never issues a passcode. A stamping exchange topology kept unmanaged 
therefore never yields a passcode-backed token.
   
   I also qualified the "neither a second exchange nor a re-mint" sentence: it 
holds only for a token that already carries the claim.
   
   Persisting provenance in the token metadata and restoring it in 
`createSubjectFromTokenIdentifier` would close this properly. I'd rather do 
that as a follow-up JIRA than grow this PR.



##########
gateway-provider-security-jwt/src/main/java/org/apache/knox/gateway/provider/federation/jwt/filter/TokenExchangeHandler.java:
##########
@@ -176,6 +189,23 @@ void handle(HttpServletRequest request, 
HttpServletResponse response, FilterChai
         actorToken = outcome.actorToken;
         conveyRequestedAudiences = outcome.conveyRequestedAudiences;
       } else {
+        // A same-subject exchange normalizes the issuer and audience of the 
output to this topology's own
+        // values. For issuers configured as delegation-required, refuse it 
here so such a token can only be
+        // exchanged on-behalf-of (carrying an act claim a downstream resource 
can reject); see
+        // JWTFederationFilter.TOKEN_EXCHANGE_DELEGATION_REQUIRED_ISSUERS. The 
inherited provenance is
+        // checked too, so a token already laundered once (iss normalized 
away) is still recognized.
+        if (filter.isDelegationRequiredForIssuer(subjectToken.getIssuer())

Review Comment:
   Good catch. This is moot now: Option 1 
(`token.exchange.delegation.required.issuers`) is removed in ebdacd329 along 
with its docs. The provenance claim doesn't depend on an `act` claim being 
built.



##########
gateway-service-knoxtoken/src/main/java/org/apache/knox/gateway/service/knoxtoken/TokenResource.java:
##########
@@ -1351,6 +1351,18 @@ private JWT getJWT(UserContext userContext, long 
issueTime, long expires, String
       customAttributes.putAll(userContext.userParams);
     }
 
+    // KNOX-3623: the knox_token_provenance claim (CommonTokenConstants) is 
reserved -- never taken from
+    // claim mappings or user params. An exchange mint uses the value the 
exchange path resolved; any other
+    // mint carries forward the provenance of the caller's own authenticated 
token, so re-minting a
+    // laundered token outside an exchange does not drop it. A plain mint 
never stamps a new value.
+    
customAttributes.remove(CommonTokenConstants.TOKEN_EXCHANGE_PROVENANCE_CLAIM);
+    final String provenanceIssuer = 
Boolean.TRUE.equals(request.getAttribute(CommonTokenConstants.TOKEN_EXCHANGE_REQUEST_ATTR))
+        ? (String) 
request.getAttribute(CommonTokenConstants.TOKEN_EXCHANGE_PROVENANCE_REQUEST_ATTR)
+        : SubjectUtils.getTokenProvenance(subject);

Review Comment:
   Yes. This was partly in the Limits section already, and I've made it 
explicit. A KNOXTOKEN mint by a user authenticated with an SSO cookie, or with 
a bearer JWT that has no provenance, issues a token with the minting topology's 
`iss` and no provenance claim.
   
   The mitigation is documented in two parts. First, keep the minting 
topology's issuer out of the registration topology's `jwt.expected.issuer`, so 
the minted token can't register directly. Second, list that issuer in the 
deny-list, so anything exchanged from it is refused.
   
   Stamping the caller's `iss` at mint time is a reasonable idea. It would 
change the claims on every KNOXTOKEN-minted token, though, so I'd prefer to 
keep this PR scoped to the exchange and track mint-side stamping separately if 
we want it.



##########
gateway-service-knoxidf/src/main/java/org/apache/knox/gateway/service/knoxidf/RegistrationResource.java:
##########
@@ -334,6 +359,19 @@ boolean delegatedCallerDenied() {
         return delegatedCallersDenied && 
SubjectUtils.getActorChainPrincipal(SubjectUtils.getCurrentSubject()) != null;
     }
 
+    /**
+     * @return {@code true} when the caller's token carries a 
knox_token_provenance claim (its exchange
+     * origin issuer) in the configured denied list -- blocking a login token 
laundered through a
+     * same-subject exchange into a registration-capable credential.
+     */
+    boolean provenanceDenied() {
+        if (deniedProvenanceIssuers.isEmpty()) {
+            return false;
+        }
+        final String provenance = 
SubjectUtils.getTokenProvenance(SubjectUtils.getCurrentSubject());
+        return provenance != null && 
deniedProvenanceIssuers.contains(provenance);

Review Comment:
   That's intended, and the docs now spell it out. The deny-list governs 
derived tokens: those whose provenance names the listed issuer. Which issuers 
may call the registration topology directly is set by its 
`jwt.expected.issuer`. To refuse an issuer's tokens outright, leave it out of 
that list. A token without the claim is never refused by the deny-list, 
whatever its `iss`.



##########
gateway-service-knoxidf/src/main/java/org/apache/knox/gateway/service/knoxidf/RegistrationResource.java:
##########
@@ -334,6 +359,19 @@ boolean delegatedCallerDenied() {
         return delegatedCallersDenied && 
SubjectUtils.getActorChainPrincipal(SubjectUtils.getCurrentSubject()) != null;
     }
 
+    /**
+     * @return {@code true} when the caller's token carries a 
knox_token_provenance claim (its exchange
+     * origin issuer) in the configured denied list -- blocking a login token 
laundered through a
+     * same-subject exchange into a registration-capable credential.
+     */
+    boolean provenanceDenied() {
+        if (deniedProvenanceIssuers.isEmpty()) {
+            return false;
+        }
+        final String provenance = 
SubjectUtils.getTokenProvenance(SubjectUtils.getCurrentSubject());
+        return provenance != null && 
deniedProvenanceIssuers.contains(provenance);

Review Comment:
   @lmccay That check catches only delegated tokens, i.e. a token carrying an 
`act` claim (an `ActorChainPrincipal`). A raw KNOXSSO token has no `act` claim, 
so `delegatedCallerDenied()` doesn't refuse it. Refusing it outright is 
`jwt.expected.issuer`'s job on the registration topology.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to