Skip to content

Commit f41097c

Browse files
authored
Add safeguards to all cookie text fields (#32)
1 parent 2979b7c commit f41097c

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

src/main/java/ee/eesti/authentication/configuration/jwt/JwtUtils.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ public Cookie getJwtCookie(SignedJWT signedJWT) {
149149
public Cookie getLegacySessionCookie(HttpServletRequest request, SessionsEntity sessionsEntity, boolean alwaysCreateCookie) {
150150

151151
Supplier<Cookie> cookieSupplier = () -> {
152-
Cookie sessionCookie = new Cookie(legacyPortalIntegrationConfig.getSessionCookieName(),
153-
sessionsEntity.getSessionId().replaceAll("[\n\r]+"," "));
152+
Cookie sessionCookie = new Cookie(removeNewlines(legacyPortalIntegrationConfig.getSessionCookieName()),
153+
removeNewlines(sessionsEntity.getSessionId()));
154154
sessionCookie.setHttpOnly(true);
155155
sessionCookie.setSecure(secureCookie);
156156
sessionCookie.setPath("/");
157-
sessionCookie.setDomain(legacyPortalIntegrationConfig.getSessionCookieDomain());
157+
sessionCookie.setDomain(removeNewlines(legacyPortalIntegrationConfig.getSessionCookieDomain()));
158158
return sessionCookie;
159159
};
160160

@@ -268,5 +268,8 @@ public static RSAKey getJwtSignKeyFromKeystore(String keyStoreType, InputStream
268268
return (RSAKey) jwkSet.getKeyByKeyId(keyAlias);
269269
}
270270

271+
public static String removeNewlines(String in) {
272+
return in.replaceAll("[\n\r]+"," ");
273+
}
271274

272275
}

src/main/java/ee/eesti/authentication/controller/CustomJwtController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ public ResponseEntity<?> createCustomJwtToken(@RequestBody @Valid CustomJwtToken
9292
return emptyOkResponse;
9393
}
9494

95-
Cookie cookie = new Cookie(request.getJwtName(), signedJWT.serialize().replaceAll("[\n\r]+"," "));
95+
Cookie cookie = new Cookie(JwtUtils.removeNewlines(request.getJwtName()), JwtUtils.removeNewlines(signedJWT.serialize()));
9696
cookie.setHttpOnly(true);
9797
cookie.setSecure(secureCookie);
98-
cookie.setDomain(legacyPortalIntegrationConfig.getSessionCookieDomain().replaceAll("[\n\r]+"," "));
98+
cookie.setDomain(JwtUtils.removeNewlines(legacyPortalIntegrationConfig.getSessionCookieDomain()));
9999

100100
response.addCookie(cookie);
101101

src/main/java/ee/eesti/authentication/controller/JwtController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,11 @@ public ResponseEntity<?> performBlacklist(
431431
}
432432

433433
private void removeCookie(HttpServletResponse response, Cookie c, String domain, String path) {
434-
Cookie cookie = new Cookie(c.getName(), null);
435-
cookie.setDomain(domain.replaceAll("[\n\r]+"," "));
434+
Cookie cookie = new Cookie(JwtUtils.removeNewlines(c.getName()), null);
435+
cookie.setDomain(JwtUtils.removeNewlines(domain));
436436
cookie.setMaxAge(0);
437437
cookie.setSecure(c.getSecure());
438-
cookie.setPath(path.replaceAll("[\n\r]+"," "));
438+
cookie.setPath(JwtUtils.removeNewlines(path));
439439
response.addCookie(cookie);
440440
}
441441

0 commit comments

Comments
 (0)