Skip to content

Commit 1d88dd3

Browse files
committed
Obfuscate session cookie values for JSON output as well as HTML
1 parent 8769fef commit 1d88dd3

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

webapps/docs/changelog.xml

+4
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@
246246
Examples. Fix broken links when Servlet Request Info example is called
247247
via a URL that includes a pathInfo component. (markt)
248248
</fix>
249+
<fix>
250+
Examples. Expand the obfuscation of session cookie values in the request
251+
header example to JSON responses. (markt)
252+
</fix>
249253
</changelog>
250254
</subsection>
251255
<subsection name = "Other">

webapps/examples/WEB-INF/classes/RequestHeaderExample.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected boolean prefersJSON(String acceptHeader) {
7373

7474
// text/html, application/html, etc.
7575
if (accept.contains("html")) {
76-
return false;
76+
return true;
7777
}
7878
}
7979
return false;
@@ -138,8 +138,20 @@ protected void renderJSON(HttpServletRequest request, HttpServletResponse respon
138138
String headerName = e.nextElement();
139139
String headerValue = request.getHeader(headerName);
140140

141-
out.append("{\"").append(JSONFilter.escape(headerName)).append("\":\"")
142-
.append(JSONFilter.escape(headerValue)).append("\"}");
141+
out.append("{\"").append(JSONFilter.escape(headerName)).append("\":\"");
142+
143+
144+
if (headerName.toLowerCase(Locale.ENGLISH).contains("cookie")) {
145+
HttpSession session = request.getSession(false);
146+
String sessionId = null;
147+
if (session != null) {
148+
sessionId = session.getId();
149+
}
150+
out.append(JSONFilter.escape(CookieFilter.filter(headerValue, sessionId)));
151+
} else {
152+
out.append(JSONFilter.escape(headerValue));
153+
}
154+
out.append("\"}");
143155

144156
if (e.hasMoreElements()) {
145157
out.append(',');

0 commit comments

Comments
 (0)