Skip to content

Commit eb3474a

Browse files
rmiccolienricovianello
authored andcommitted
Sanitize registration key in error page
1 parent 4cb0c0c commit eb3474a

3 files changed

Lines changed: 79 additions & 5 deletions

File tree

iam-login-service/src/main/java/it/infn/mw/iam/registration/DefaultRegistrationRequestService.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package it.infn.mw.iam.registration;
1717

18+
import static it.infn.mw.iam.authn.x509.IamX509PreauthenticationProcessingFilter.X509_CREDENTIAL_SESSION_KEY;
1819
import static it.infn.mw.iam.core.IamRegistrationRequestStatus.APPROVED;
1920
import static it.infn.mw.iam.core.IamRegistrationRequestStatus.CONFIRMED;
2021
import static it.infn.mw.iam.core.IamRegistrationRequestStatus.NEW;
@@ -35,7 +36,6 @@
3536

3637
import javax.servlet.http.HttpServletRequest;
3738
import javax.servlet.http.HttpSession;
38-
import static it.infn.mw.iam.authn.x509.IamX509PreauthenticationProcessingFilter.X509_CREDENTIAL_SESSION_KEY;
3939

4040
import org.slf4j.Logger;
4141
import org.slf4j.LoggerFactory;
@@ -124,7 +124,6 @@ public class DefaultRegistrationRequestService
124124

125125
public static final String NICKNAME_ATTRIBUTE_KEY = "nickname";
126126

127-
@Autowired
128127
public DefaultRegistrationRequestService(LabelDTOConverter labelConverter,
129128
IamProperties iamProperties) {
130129
this.labelConverter = labelConverter;
@@ -266,8 +265,7 @@ public List<RegistrationRequestDto> listPendingRequests() {
266265
public RegistrationRequestDto confirmRequest(String confirmationKey) {
267266

268267
IamRegistrationRequest request = requestRepository.findByAccountConfirmationKey(confirmationKey)
269-
.orElseThrow(() -> new ScimResourceNotFoundException(String
270-
.format("No registration request found for registration_key [%s]", confirmationKey)));
268+
.orElseThrow(() -> new ScimResourceNotFoundException("No registration request found"));
271269

272270
return handleConfirm(request);
273271
}

iam-login-service/src/main/webapp/WEB-INF/views/iam/confirmRequest.jsp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
--%>
1818
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
1919
<%@ taglib prefix="t" tagdir="/WEB-INF/tags/iam"%>
20+
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
2021
<t:page title="Verify registration request">
2122
<h1 class="text-center">Verify registration request</h1>
2223
<div id="verify-registration-form">
@@ -25,7 +26,7 @@
2526
<form name="confirmationForm"
2627
action="${pageContext.request.contextPath.endsWith('/') ? pageContext.request.contextPath : pageContext.request.contextPath.concat('/') }registration/verify"
2728
method="post">
28-
<input type="hidden" name="token" value="${token}" />
29+
<input type="hidden" name="token" value="${fn:escapeXml(token)}" />
2930
<input class="btn btn-primary" type="submit" name="confirm_registration_request" value="Confirm Request" />
3031
</form>
3132
</div>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2016-2021
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package it.infn.mw.iam.test.registration;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import org.junit.jupiter.api.BeforeAll;
21+
import org.junit.jupiter.api.Test;
22+
import org.springframework.beans.factory.annotation.Value;
23+
import org.springframework.boot.test.context.SpringBootTest;
24+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
25+
26+
import io.restassured.RestAssured;
27+
import it.infn.mw.iam.test.TestUtils;
28+
29+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
30+
class RegistrationApiControllerTests {
31+
32+
@Value("${local.server.port}")
33+
private Integer iamPort;
34+
35+
@BeforeAll
36+
static void init() {
37+
TestUtils.initRestAssured();
38+
}
39+
40+
@Test
41+
void testTokenIsNotReflectedInErrorPage() {
42+
43+
String response = RestAssured.given()
44+
.port(iamPort)
45+
.contentType("application/x-www-form-urlencoded")
46+
.formParam("token", "<img src=x onerror=alert(1)>")
47+
.when()
48+
.post("/registration/verify")
49+
.then()
50+
.statusCode(200)
51+
.extract()
52+
.asString();
53+
54+
assertThat(response).doesNotContain("<img src=x onerror=alert(1)>")
55+
.contains("No registration request found");
56+
}
57+
58+
@Test
59+
void testTokenIsEscapedInConfirmPage() {
60+
61+
String payload = "\"><img src=x onerror=alert(1)>";
62+
63+
String response = RestAssured.given()
64+
.port(iamPort)
65+
.pathParam("token", payload)
66+
.when()
67+
.get("/registration/verify/{token}")
68+
.then()
69+
.statusCode(200)
70+
.extract()
71+
.asString();
72+
73+
assertThat(response).doesNotContain("<img src=x onerror=alert(1)>");
74+
}
75+
}

0 commit comments

Comments
 (0)