Skip to content

Commit 2a9e995

Browse files
feat: make effective constraint on multiple signatures in SamlResponse (#879)
1 parent d02fd86 commit 2a9e995

File tree

3 files changed

+44
-41
lines changed

3 files changed

+44
-41
lines changed

src/oneid/oneid-ecs-core/src/main/java/it/pagopa/oneid/service/utils/SAMLUtilsExtendedCore.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,7 @@ private Response unmarshallResponse(byte[] decodedSamlResponse) throws OneIdenti
209209
} catch (SAXException | IOException | ParserConfigurationException |
210210
XPathExpressionException e) {
211211
Log.error("XML parsing exception " + e.getMessage());
212-
// TODO uncomment once feature is active
213-
// throw new OneIdentityException(e);
212+
throw new OneIdentityException(e);
214213
}
215214

216215
try {

src/oneid/oneid-ecs-core/src/main/java/it/pagopa/oneid/web/controller/SAMLController.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ public Response samlACS(@BeanParam @Valid SAMLResponseDTO samlResponseDTO) {
9595
cloudWatchConnectorImpl.sendIDPErrorMetricData(
9696
samlSession.getAuthorizationRequestDTOExtended().getIdp(),
9797
ErrorCode.IDP_ERROR_MULTIPLE_SAMLRESPONSE_SIGNATURES_PRESENT);
98-
// TODO uncomment to activate the check for multiple signatures
99-
// throw new GenericHTMLException(ErrorCode.IDP_ERROR_MULTIPLE_SAMLRESPONSE_SIGNATURES_PRESENT);
98+
throw new GenericHTMLException(ErrorCode.IDP_ERROR_MULTIPLE_SAMLRESPONSE_SIGNATURES_PRESENT);
10099
}
101100

102101
// 1d. Check status, will raise CustomException in case of error mapped to a custom html error page

src/oneid/oneid-ecs-core/src/test/java/it/pagopa/oneid/web/controller/SAMLControllerTest.java

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.nimbusds.oauth2.sdk.id.State;
1111
import io.quarkus.test.InjectMock;
1212
import io.quarkus.test.common.http.TestHTTPEndpoint;
13+
import io.quarkus.test.junit.QuarkusMock;
1314
import io.quarkus.test.junit.QuarkusTest;
1415
import io.quarkus.test.junit.TestProfile;
1516
import it.pagopa.oneid.common.model.exception.OneIdentityException;
@@ -23,6 +24,7 @@
2324
import it.pagopa.oneid.service.SessionServiceImpl;
2425
import it.pagopa.oneid.web.controller.interceptors.CurrentAuthDTO;
2526
import it.pagopa.oneid.web.controller.mock.SAMLControllerTestProfile;
27+
import it.pagopa.oneid.web.dto.AuthorizationRequestDTOExtended;
2628
import jakarta.inject.Inject;
2729
import java.net.URLEncoder;
2830
import java.nio.charset.StandardCharsets;
@@ -413,43 +415,46 @@ void samlACS_exceptionInCreatingCallbackURI() {
413415
Assertions.assertTrue(location.contains(headerLocation));
414416
}
415417

416-
// TODO re-enable this test when the feature is back
417-
// @Test
418-
// @SneakyThrows
419-
// void samlACS_SAMLResponseWithMultipleSignatures() {
420-
// // given
421-
// CurrentAuthDTO mockAuthDTO = Mockito.mock(CurrentAuthDTO.class);
422-
// QuarkusMock.installMockForType(mockAuthDTO, CurrentAuthDTO.class);
423-
//
424-
// Map<String, String> samlResponseDTO = new HashMap<>();
425-
// samlResponseDTO.put("SAMLResponse", "dummySAMLResponse");
426-
// samlResponseDTO.put("RelayState", "dummyRelayState");
427-
//
428-
// // Mock CurrentAuthDTO to simulate multiple signatures scenario
429-
// Mockito.when(mockAuthDTO.isResponseWithMultipleSignatures()).thenReturn(true);
430-
//
431-
// // Setup mocks for response and samlSession as usual, but flow will stop at the multiple signatures check
432-
// Response response = Mockito.mock(Response.class);
433-
// Mockito.when(response.getInResponseTo()).thenReturn("Dummy");
434-
// Mockito.when(samlServiceImpl.getSAMLResponseFromString(Mockito.any())).thenReturn(response);
435-
// Mockito.when(mockAuthDTO.getResponse()).thenReturn(response);
436-
//
437-
// AuthorizationRequestDTOExtended dto = Mockito.mock(AuthorizationRequestDTOExtended.class);
438-
// Mockito.when(dto.getIdp()).thenReturn("dummy-idp"); // Stub idp for cloudwatch metrics
439-
// SAMLSession samlSession = Mockito.mock(SAMLSession.class);
440-
// Mockito.when(samlSession.getAuthorizationRequestDTOExtended()).thenReturn(dto);
441-
// Mockito.when(mockAuthDTO.getSamlSession()).thenReturn(samlSession);
442-
//
443-
// // HTTP 302
444-
// given()
445-
// .formParams(samlResponseDTO)
446-
// .when()
447-
// .post("/acs")
448-
// .then()
449-
// .statusCode(302)
450-
// .header("Location", containsString(
451-
// ErrorCode.IDP_ERROR_MULTIPLE_SAMLRESPONSE_SIGNATURES_PRESENT.getErrorCode()));
452-
// }
418+
@Test
419+
@SneakyThrows
420+
void samlACS_SAMLResponseWithMultipleSignatures() {
421+
// given
422+
CurrentAuthDTO mockAuthDTO = Mockito.mock(CurrentAuthDTO.class);
423+
QuarkusMock.installMockForType(mockAuthDTO, CurrentAuthDTO.class);
424+
425+
Map<String, String> samlResponseDTO = new HashMap<>();
426+
samlResponseDTO.put("SAMLResponse", "dummySAMLResponse");
427+
samlResponseDTO.put("RelayState", "dummyRelayState");
428+
429+
// Mock CurrentAuthDTO to simulate multiple signatures scenario
430+
Mockito.when(mockAuthDTO.isResponseWithMultipleSignatures()).thenReturn(true);
431+
432+
// Setup mocks for response and samlSession as usual, but flow will stop at the multiple signatures check
433+
Response response = Mockito.mock(Response.class);
434+
Mockito.when(response.getInResponseTo()).thenReturn("Dummy");
435+
Mockito.when(samlServiceImpl.getSAMLResponseFromString(Mockito.any())).thenReturn(response);
436+
Mockito.when(mockAuthDTO.getResponse()).thenReturn(response);
437+
438+
AuthorizationRequestDTOExtended dto = Mockito.mock(AuthorizationRequestDTOExtended.class);
439+
Mockito.when(dto.getIdp()).thenReturn("dummy-idp"); // Stub idp for cloudwatch metrics
440+
SAMLSession samlSession = Mockito.mock(SAMLSession.class);
441+
Mockito.when(samlSession.getAuthorizationRequestDTOExtended()).thenReturn(dto);
442+
Mockito.when(mockAuthDTO.getSamlSession()).thenReturn(samlSession);
443+
444+
// HTTP 302
445+
String location = given()
446+
.formParams(samlResponseDTO)
447+
.when()
448+
.post("/acs")
449+
.then()
450+
.statusCode(302)
451+
.extract()
452+
.header("location");
453+
454+
Assertions.assertTrue(location.contains(
455+
ErrorCode.IDP_ERROR_MULTIPLE_SAMLRESPONSE_SIGNATURES_PRESENT.getErrorCode()));
456+
457+
}
453458

454459
@Test
455460
void assertion_ok() {

0 commit comments

Comments
 (0)