Skip to content

Commit 515fd0c

Browse files
committed
Apply the WSS4J SAML checks in the STS token validators (#3792)
* Apply the WSS4J SAML checks in the STS token validators STSStaxTokenValidator validates a SAML assertion with its own copy of an old version of SamlTokenValidatorImpl.validate(). It did not check the audience restrictions, the AuthnStatements or the Subject Confirmation requirements, and it cached the OneTimeUse ID before the other checks. It now makes the same checks as SamlTokenValidatorImpl, in the same order. STSSamlAssertionValidator lets SamlAssertionValidator go on when the signature of an assertion is not trusted locally, so that STSTokenValidator can validate the assertion with the STS. The OneTimeUse ID of such an assertion was cached before the STS validated it, so an assertion that the STS rejects could take the ID of a genuine one. It is now only cached once the STS has validated the assertion. * Cache the OneTimeUse ID only once the STS token validators have validated an assertion STSTokenValidator now checks the OneTimeUse Condition itself, once the assertion has been validated locally or by the STS, including with alwaysValidateToSts, where it was never checked. This no longer depends on the order in which SamlAssertionValidator calls verifySignedAssertion and checkOneTimeUse. With alwaysValidateToSts, STSStaxTokenValidator again leaves the validation of the assertion to the STS, as STSTokenValidator(true) does, and checks the OneTimeUse Condition after the STS has validated it. The tests now go through STSTokenValidator.validate with an STS that accepts or rejects the assertion, and the audience tests have a positive control. (cherry picked from commit 6fb2f24) (cherry picked from commit b0e99e6)
1 parent 47e6b43 commit 515fd0c

5 files changed

Lines changed: 294 additions & 20 deletions

File tree

‎rt/ws/security/src/main/java/org/apache/cxf/ws/security/trust/STSSamlAssertionValidator.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ protected Credential verifySignedAssertion(
6464
}
6565
}
6666

67+
/**
68+
* The OneTimeUse Condition is not checked here. STSTokenValidator checks it with checkOneTimeUseCondition
69+
* once the assertion has been validated, locally or by the STS, as caching the ID of an assertion that is
70+
* then rejected (e.g. one whose signature is not trusted locally and that the STS rejects) would let an
71+
* attacker poison the cache against a genuine assertion with the same ID. This also does not depend on
72+
* whether SamlAssertionValidator calls checkOneTimeUse before or after verifySignedAssertion.
73+
*/
74+
@Override
75+
protected void checkOneTimeUse(SamlAssertionWrapper samlAssertion, RequestData data)
76+
throws WSSecurityException {
77+
// checked by STSTokenValidator once the assertion has been validated
78+
}
79+
80+
/**
81+
* Check the OneTimeUse Condition of an assertion once it has been validated, locally or by the STS
82+
*/
83+
public void checkOneTimeUseCondition(SamlAssertionWrapper samlAssertion, RequestData data)
84+
throws WSSecurityException {
85+
super.checkOneTimeUse(samlAssertion, data);
86+
}
87+
6788
/**
6889
* Return if trust verification on the signature of the assertion succeeded.
6990
* @return if trust verification on the signature of the assertion succeeded

‎rt/ws/security/src/main/java/org/apache/cxf/ws/security/trust/STSStaxTokenValidator.java‎

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,33 +96,48 @@ public <T extends SamlSecurityToken & InboundSecurityToken> T validate(
9696
final SamlAssertionWrapper samlAssertionWrapper,
9797
final InboundSecurityToken subjectSecurityToken,
9898
final TokenContext tokenContext) throws WSSecurityException {
99-
// Check conditions
100-
checkConditions(samlAssertionWrapper);
99+
final SoapMessage message =
100+
(SoapMessage)tokenContext.getWssSecurityProperties().getMsgContext();
101+
102+
final boolean stsValidated;
103+
if (alwaysValidateToSts) {
104+
// The STS validates the assertion, as with STSTokenValidator(true), so only its validity period and
105+
// its schema are checked locally
106+
checkConditions(samlAssertionWrapper);
107+
validateAssertion(samlAssertionWrapper);
108+
109+
validateTokenToSTS(samlAssertionWrapper.getElement(), message);
110+
stsValidated = true;
111+
} else {
112+
// Check conditions, including the audience restrictions
113+
checkConditions(samlAssertionWrapper,
114+
tokenContext.getWssSecurityProperties().getAudienceRestrictions());
101115

102-
// Check OneTimeUse Condition
116+
// Check the AuthnStatements of the assertion (if any)
117+
checkAuthnStatements(samlAssertionWrapper);
118+
119+
// Check the Subject Confirmation requirements
120+
verifySubjectConfirmationMethod(samlAssertionWrapper);
121+
122+
// Validate the assertion against schemas/profiles
123+
validateAssertion(samlAssertionWrapper);
124+
stsValidated = false;
125+
}
126+
127+
// Check OneTimeUse Condition. This must come after the other checks, and after the STS has validated the
128+
// assertion, as it caches the Assertion ID, and caching the ID of an Assertion that is then rejected
129+
// would let an attacker poison the cache against a genuine Assertion with the same ID. WSS4J has already
130+
// verified the signature of the assertion and the trust of its issuer before calling this validator.
131+
// verify() below only checks the trust of the subject's key, and is only called when the token is used
132+
// for a signature, so this check can not be moved there without losing it for other assertions.
103133
checkOneTimeUse(samlAssertionWrapper,
104134
tokenContext.getWssSecurityProperties().getSamlOneTimeUseReplayCache());
105135

106-
// Validate the assertion against schemas/profiles
107-
validateAssertion(samlAssertionWrapper);
108-
109136
Crypto sigVerCrypto = null;
110137
if (samlAssertionWrapper.isSigned()) {
111138
sigVerCrypto = tokenContext.getWssSecurityProperties().getSignatureVerificationCrypto();
112139
}
113140

114-
final SoapMessage message =
115-
(SoapMessage)tokenContext.getWssSecurityProperties().getMsgContext();
116-
117-
// Validate to STS if required
118-
boolean valid = false;
119-
if (alwaysValidateToSts) {
120-
Element tokenElement = samlAssertionWrapper.getElement();
121-
validateTokenToSTS(tokenElement, message);
122-
valid = true;
123-
}
124-
final boolean stsValidated = valid;
125-
126141
SamlSecurityTokenImpl securityToken = new SamlSecurityTokenImpl(
127142
samlAssertionWrapper, subjectSecurityToken,
128143
tokenContext.getWsSecurityContext(),

‎rt/ws/security/src/main/java/org/apache/cxf/ws/security/trust/STSTokenValidator.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,19 @@ public STSTokenValidator(boolean alwaysValidateToSts) {
7272

7373
public Credential validate(Credential credential, RequestData data) throws WSSecurityException {
7474

75+
final Credential validatedCredential;
7576
if (isValidatedLocally(credential, data)) {
76-
return credential;
77+
validatedCredential = credential;
78+
} else {
79+
validatedCredential = validateWithSTS(credential, (Message)data.getMsgContext());
7780
}
7881

79-
return validateWithSTS(credential, (Message)data.getMsgContext());
82+
// The OneTimeUse ID of an assertion is only cached once the assertion has been validated, locally or by
83+
// the STS, so that an assertion that is rejected can not take the ID of a genuine one
84+
if (credential.getSamlAssertion() != null) {
85+
new STSSamlAssertionValidator().checkOneTimeUseCondition(credential.getSamlAssertion(), data);
86+
}
87+
return validatedCredential;
8088
}
8189

8290
public Credential validateWithSTS(Credential credential, Message message) throws WSSecurityException {

‎rt/ws/security/src/test/java/org/apache/cxf/ws/security/trust/STSTokenValidatorTest.java‎

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.apache.cxf.ws.security.trust;
2020

21+
import java.util.UUID;
2122
import java.util.concurrent.Callable;
2223
import java.util.concurrent.ExecutorService;
2324
import java.util.concurrent.Executors;
@@ -28,12 +29,17 @@
2829
import org.w3c.dom.Element;
2930

3031
import org.apache.cxf.helpers.DOMUtils;
32+
import org.apache.cxf.message.Message;
3133
import org.apache.cxf.ws.security.wss4j.saml.SAML2CallbackHandler;
34+
import org.apache.wss4j.common.cache.MemoryReplayCache;
35+
import org.apache.wss4j.common.cache.ReplayCache;
3236
import org.apache.wss4j.common.crypto.Crypto;
3337
import org.apache.wss4j.common.crypto.CryptoFactory;
38+
import org.apache.wss4j.common.ext.WSSecurityException;
3439
import org.apache.wss4j.common.saml.SAMLCallback;
3540
import org.apache.wss4j.common.saml.SAMLUtil;
3641
import org.apache.wss4j.common.saml.SamlAssertionWrapper;
42+
import org.apache.wss4j.common.saml.bean.ConditionsBean;
3743
import org.apache.wss4j.dom.engine.WSSConfig;
3844
import org.apache.wss4j.dom.handler.RequestData;
3945
import org.apache.wss4j.dom.saml.WSSSAMLKeyInfoProcessor;
@@ -42,14 +48,18 @@
4248
import org.junit.BeforeClass;
4349
import org.junit.Test;
4450

51+
import static org.junit.Assert.assertEquals;
4552
import static org.junit.Assert.assertFalse;
4653
import static org.junit.Assert.assertTrue;
54+
import static org.junit.Assert.fail;
4755

4856
/**
4957
* Tests for local validation of SAML Assertions in the STSTokenValidator.
5058
*/
5159
public class STSTokenValidatorTest {
5260

61+
private static final String STS_REJECTED = "rejected by the STS";
62+
5363
private static Crypto signingCrypto;
5464
private static Crypto trustedCrypto;
5565
private static Crypto untrustedCrypto;
@@ -134,6 +144,101 @@ public void testConcurrentTrustedAndUntrustedAssertions() throws Exception {
134144
}
135145
}
136146

147+
/**
148+
* The OneTimeUse ID of an Assertion that is trusted locally is cached, so it can not be replayed.
149+
*/
150+
@Test
151+
public void testOneTimeUseLocallyTrustedAssertion() throws Exception {
152+
STSTokenValidator validator = new StubSTSTokenValidator(false, false);
153+
RequestData data = createRequestData(trustedCrypto, new MemoryReplayCache());
154+
Credential credential = createCredential(true, data, "_" + UUID.randomUUID());
155+
156+
validator.validate(credential, data);
157+
assertReplayRejected(validator, credential, data);
158+
data.getSamlOneTimeUseReplayCache().close();
159+
}
160+
161+
/**
162+
* An Assertion whose signature is not trusted locally is passed on to the STS. Its OneTimeUse ID must not
163+
* be cached if the STS rejects it: otherwise an attacker could send an Assertion signed with an untrusted
164+
* key that carries the ID of a genuine one, so that the genuine Assertion is then rejected as a replay.
165+
*/
166+
@Test
167+
public void testOneTimeUseNotCachedWhenSTSRejects() throws Exception {
168+
ReplayCache replayCache = new MemoryReplayCache();
169+
String id = "_" + UUID.randomUUID();
170+
171+
RequestData untrustedData = createRequestData(untrustedCrypto, replayCache);
172+
try {
173+
new StubSTSTokenValidator(false, false).validate(createCredential(true, untrustedData, id),
174+
untrustedData);
175+
fail("Expected the STS to reject the Assertion");
176+
} catch (WSSecurityException ex) {
177+
assertEquals(STS_REJECTED, ex.getMessage());
178+
}
179+
180+
// The genuine Assertion is accepted, and is cached so that it can not be replayed
181+
STSTokenValidator validator = new StubSTSTokenValidator(false, false);
182+
RequestData trustedData = createRequestData(trustedCrypto, replayCache);
183+
Credential credential = createCredential(true, trustedData, id);
184+
validator.validate(credential, trustedData);
185+
assertReplayRejected(validator, credential, trustedData);
186+
replayCache.close();
187+
}
188+
189+
/**
190+
* The OneTimeUse ID of an Assertion that is not trusted locally is cached once the STS has validated it.
191+
*/
192+
@Test
193+
public void testOneTimeUseCachedWhenSTSValidates() throws Exception {
194+
STSTokenValidator validator = new StubSTSTokenValidator(false, true);
195+
RequestData data = createRequestData(untrustedCrypto, new MemoryReplayCache());
196+
Credential credential = createCredential(true, data, "_" + UUID.randomUUID());
197+
198+
validator.validate(credential, data);
199+
assertReplayRejected(validator, credential, data);
200+
data.getSamlOneTimeUseReplayCache().close();
201+
}
202+
203+
/**
204+
* With alwaysValidateToSts, the OneTimeUse ID is cached once the STS has validated the Assertion, and not
205+
* if the STS rejects it.
206+
*/
207+
@Test
208+
public void testOneTimeUseAlwaysValidateToSts() throws Exception {
209+
ReplayCache replayCache = new MemoryReplayCache();
210+
RequestData data = createRequestData(trustedCrypto, replayCache);
211+
Credential credential = createCredential(true, data, "_" + UUID.randomUUID());
212+
213+
try {
214+
new StubSTSTokenValidator(true, false).validate(credential, data);
215+
fail("Expected the STS to reject the Assertion");
216+
} catch (WSSecurityException ex) {
217+
assertEquals(STS_REJECTED, ex.getMessage());
218+
}
219+
220+
STSTokenValidator validator = new StubSTSTokenValidator(true, true);
221+
validator.validate(credential, data);
222+
assertReplayRejected(validator, credential, data);
223+
replayCache.close();
224+
}
225+
226+
private static void assertReplayRejected(STSTokenValidator validator, Credential credential, RequestData data)
227+
throws Exception {
228+
try {
229+
validator.validate(credential, data);
230+
fail("Expected failure on a replayed OneTimeUse Assertion");
231+
} catch (WSSecurityException ex) {
232+
assertEquals(WSSecurityException.ErrorCode.INVALID_SECURITY, ex.getErrorCode());
233+
}
234+
}
235+
236+
private static RequestData createRequestData(Crypto sigVerCrypto, ReplayCache replayCache) {
237+
RequestData data = createRequestData(sigVerCrypto);
238+
data.setSamlOneTimeUseReplayCache(replayCache);
239+
return data;
240+
}
241+
137242
private static RequestData createRequestData(Crypto sigVerCrypto) {
138243
RequestData data = new RequestData();
139244
data.setSigVerCrypto(sigVerCrypto);
@@ -142,10 +247,27 @@ private static RequestData createRequestData(Crypto sigVerCrypto) {
142247
}
143248

144249
private static Credential createCredential(boolean signed, RequestData data) throws Exception {
250+
return createCredential(signed, data, null);
251+
}
252+
253+
/**
254+
* Create a Credential holding an Assertion, a OneTimeUse Assertion with the given ID if it is not null
255+
*/
256+
private static Credential createCredential(boolean signed, RequestData data, String oneTimeUseId)
257+
throws Exception {
145258
SAML2CallbackHandler callbackHandler = new SAML2CallbackHandler();
146259
SAMLCallback samlCallback = new SAMLCallback();
147260
SAMLUtil.doSAMLCallback(callbackHandler, samlCallback);
261+
if (oneTimeUseId != null) {
262+
ConditionsBean conditions = new ConditionsBean();
263+
conditions.setTokenPeriodMinutes(5);
264+
conditions.setOneTimeUse(true);
265+
samlCallback.setConditions(conditions);
266+
}
148267
SamlAssertionWrapper assertion = new SamlAssertionWrapper(samlCallback);
268+
if (oneTimeUseId != null) {
269+
assertion.getSaml2().setID(oneTimeUseId);
270+
}
149271
if (signed) {
150272
assertion.signAssertion("myalias", "myAliasPassword", signingCrypto, false);
151273
}
@@ -164,4 +286,25 @@ private static Credential createCredential(boolean signed, RequestData data) thr
164286
credential.setSamlAssertion(receivedAssertion);
165287
return credential;
166288
}
289+
290+
/**
291+
* An STSTokenValidator whose STS accepts or rejects every token
292+
*/
293+
private static final class StubSTSTokenValidator extends STSTokenValidator {
294+
private final boolean stsAccepts;
295+
296+
StubSTSTokenValidator(boolean alwaysValidateToSts, boolean stsAccepts) {
297+
super(alwaysValidateToSts);
298+
this.stsAccepts = stsAccepts;
299+
}
300+
301+
@Override
302+
public Credential validateWithSTS(Credential credential, Message message) throws WSSecurityException {
303+
if (!stsAccepts) {
304+
throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, "empty",
305+
new Object[] {STS_REJECTED});
306+
}
307+
return credential;
308+
}
309+
}
167310
}

0 commit comments

Comments
 (0)