diff --git a/toolkit/src/main/java/org/codelibs/saml2/Auth.java b/toolkit/src/main/java/org/codelibs/saml2/Auth.java index aa225c5..d55689d 100644 --- a/toolkit/src/main/java/org/codelibs/saml2/Auth.java +++ b/toolkit/src/main/java/org/codelibs/saml2/Auth.java @@ -1149,7 +1149,11 @@ public void processResponse(final String requestId) { errorReason = samlResponse.getError(); validationException = samlResponse.getValidationException(); final SamlResponseStatus samlResponseStatus = samlResponse.getResponseStatus(); - if (samlResponseStatus.getStatusCode() == null || !Constants.STATUS_SUCCESS.equals(samlResponseStatus.getStatusCode())) { + // The status is read after the version and the ID are checked, so it is null when the + // response failed one of those (a document that is not a SAML response at all, for example). + // That is an invalid response like any other, not a status the IdP reported. + if (samlResponseStatus != null + && (samlResponseStatus.getStatusCode() == null || !Constants.STATUS_SUCCESS.equals(samlResponseStatus.getStatusCode()))) { errors.add("response_not_success"); // Debug rather than warn: "response_not_success" is already in getErrors(), and // the detail is in getLastErrorReason() and getLastValidationException(). Logging diff --git a/toolkit/src/test/java/org/codelibs/saml2/test/AuthTest.java b/toolkit/src/test/java/org/codelibs/saml2/test/AuthTest.java index 6714507..1425f37 100644 --- a/toolkit/src/test/java/org/codelibs/saml2/test/AuthTest.java +++ b/toolkit/src/test/java/org/codelibs/saml2/test/AuthTest.java @@ -996,6 +996,27 @@ public void testProcessSLOResponseStatusResponder() throws Exception { * @see org.codelibs.saml2.Auth#getErrors * @see org.codelibs.saml2.Auth#getLastErrorReason */ + /** + * A well-formed document that is not a SAML response has no Version and no Status, so the response + * is refused before its status is read. processResponse must report that as an invalid response, not + * throw a NullPointerException on the missing status. + */ + @Test + public void testProcessResponseWithoutStatus() throws Exception { + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + String samlResponseEncoded = java.util.Base64.getEncoder().encodeToString("".getBytes(java.nio.charset.StandardCharsets.UTF_8)); + when(request.getParameterMap()).thenReturn(singletonMap("SAMLResponse", new String[] { samlResponseEncoded })); + when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/java-saml-jspsample/acs.jsp")); + + Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build(); + Auth auth = new Auth(settings, request, response); + auth.processResponse(); + assertFalse(auth.isAuthenticated()); + assertEquals(java.util.Collections.singletonList("invalid_response"), auth.getErrors()); + assertTrue(auth.getLastValidationException() instanceof ValidationException); + } + @Test public void testIsAuthenticated() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class);