Skip to content

Commit 7adb4da

Browse files
committed
Always require signature on either response or assertion
Fixes gh-7490 #7490
1 parent d83aa34 commit 7adb4da

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/authentication/OpenSamlAuthenticationProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ private Assertion validateSaml2Response(Saml2AuthenticationToken token,
254254
}
255255
try {
256256
Assertion a = decrypt(token, ea);
257-
validateAssertion(recipient, a, token, false);
257+
validateAssertion(recipient, a, token, !responseSigned);
258258
return a;
259259
} catch (Saml2AuthenticationException e) {
260260
lastValidationError = e;

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/authentication/OpenSamlAuthenticationProviderTests.java

+36-1
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,47 @@ public void authenticateWhenUsernameMissingThenThrowAuthenticationException() th
216216
}
217217

218218
@Test
219-
public void authenticateWhenEncryptedAssertionWithoutSignatureThenItSucceeds() throws Exception {
219+
public void authenticateWhenEncryptedAssertionWithoutSignatureThenItFails() throws Exception {
220220
Response response = response(recipientUri, idpEntityId);
221221
Assertion assertion = defaultAssertion();
222222
EncryptedAssertion encryptedAssertion = encryptAssertion(assertion, assertingPartyCredentials());
223223
response.getEncryptedAssertions().add(encryptedAssertion);
224224
token = responseXml(response, idpEntityId);
225+
exception.expect(
226+
authenticationMatcher(
227+
Saml2ErrorCodes.INVALID_SIGNATURE
228+
)
229+
);
230+
provider.authenticate(token);
231+
}
232+
233+
@Test
234+
public void authenticateWhenEncryptedAssertionWithSignatureThenItSucceeds() throws Exception {
235+
Response response = response(recipientUri, idpEntityId);
236+
Assertion assertion = defaultAssertion();
237+
signXmlObject(
238+
assertion,
239+
assertingPartyCredentials(),
240+
recipientEntityId
241+
);
242+
EncryptedAssertion encryptedAssertion = encryptAssertion(assertion, assertingPartyCredentials());
243+
response.getEncryptedAssertions().add(encryptedAssertion);
244+
token = responseXml(response, idpEntityId);
245+
provider.authenticate(token);
246+
}
247+
248+
@Test
249+
public void authenticateWhenEncryptedAssertionWithResponseSignatureThenItSucceeds() throws Exception {
250+
Response response = response(recipientUri, idpEntityId);
251+
Assertion assertion = defaultAssertion();
252+
EncryptedAssertion encryptedAssertion = encryptAssertion(assertion, assertingPartyCredentials());
253+
response.getEncryptedAssertions().add(encryptedAssertion);
254+
signXmlObject(
255+
response,
256+
assertingPartyCredentials(),
257+
recipientEntityId
258+
);
259+
token = responseXml(response, idpEntityId);
225260
provider.authenticate(token);
226261
}
227262

samples/boot/saml2login/src/integration-test/java/org/springframework/security/samples/Saml2LoginIntegrationTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,15 @@ public void authenticateWhenResponseIsSignedAndAssertionIsEncryptedThenItSucceed
163163
EncryptedAssertion encryptedAssertion =
164164
OpenSamlActionTestingSupport.encryptAssertion(assertion, decodeCertificate(spCertificate));
165165
Response response = buildResponse(encryptedAssertion);
166-
signXmlObject(assertion, getSigningCredential(idpCertificate, idpPrivateKey, UsageType.SIGNING));
166+
signXmlObject(response, getSigningCredential(idpCertificate, idpPrivateKey, UsageType.SIGNING));
167167
sendResponse(response, "/")
168168
.andExpect(authenticated().withUsername(USERNAME));
169169
}
170170

171171
@Test
172-
public void authenticateWhenResponseIsNotSignedAndAssertionIsEncryptedThenItSucceeds() throws Exception {
172+
public void authenticateWhenResponseIsNotSignedAndAssertionIsEncryptedAndSignedThenItSucceeds() throws Exception {
173173
Assertion assertion = buildAssertion(USERNAME);
174+
signXmlObject(assertion, getSigningCredential(idpCertificate, idpPrivateKey, UsageType.SIGNING));
174175
EncryptedAssertion encryptedAssertion =
175176
OpenSamlActionTestingSupport.encryptAssertion(assertion, decodeCertificate(spCertificate));
176177
Response response = buildResponse(encryptedAssertion);

0 commit comments

Comments
 (0)