Skip to content

Commit

Permalink
Obfuscate session cookie values for JSON output as well as HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
markt-asf committed Dec 2, 2024
1 parent 40069ce commit f57a9d9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@
Examples. Fix broken links when Servlet Request Info example is called
via a URL that includes a pathInfo component. (markt)
</fix>
<fix>
Examples. Expand the obfuscation of session cookie values in the request
header example to JSON responses. (markt)
</fix>
</changelog>
</subsection>
<subsection name = "Other">
Expand Down
18 changes: 15 additions & 3 deletions webapps/examples/WEB-INF/classes/RequestHeaderExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected boolean prefersJSON(String acceptHeader) {

// text/html, application/html, etc.
if (accept.contains("html")) {
return false;
return true;
}
}
return false;
Expand Down Expand Up @@ -138,8 +138,20 @@ protected void renderJSON(HttpServletRequest request, HttpServletResponse respon
String headerName = e.nextElement();
String headerValue = request.getHeader(headerName);

out.append("{\"").append(JSONFilter.escape(headerName)).append("\":\"")
.append(JSONFilter.escape(headerValue)).append("\"}");
out.append("{\"").append(JSONFilter.escape(headerName)).append("\":\"");


if (headerName.toLowerCase(Locale.ENGLISH).contains("cookie")) {
HttpSession session = request.getSession(false);
String sessionId = null;
if (session != null) {
sessionId = session.getId();
}
out.append(JSONFilter.escape(CookieFilter.filter(headerValue, sessionId)));
} else {
out.append(JSONFilter.escape(headerValue));
}
out.append("\"}");

if (e.hasMoreElements()) {
out.append(',');
Expand Down

0 comments on commit f57a9d9

Please sign in to comment.