Skip to content

Commit 5317093

Browse files
preserveCookiePath configuration parameter support added. (#231)
1 parent 7ad6d44 commit 5317093

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Version 1.13 (unreleased)
22

3-
_(no changes)_
3+
\#231: Added support of preserveCookiePath configuration parameter. It allows to keep cookie path unchanged in Set-Cookie server response header.
44

55
# Version 1.12.1 released on 2021-12-28
66

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ The following is a list of parameters that can be configured
7272
+ forwardip: A boolean parameter name to enable forwarding of the client IP
7373
+ preserveHost: A boolean parameter name to keep HOST parameter as-is
7474
+ preserveCookies: A boolean parameter name to keep COOKIES as-is
75+
+ preserveCookiePath: A boolean parameter name to keep cookie path unchanged in Set-Cookie server response header
7576
+ http.protocol.handle-redirects: A boolean parameter name to have auto-handle redirects
7677
+ http.socket.timeout: A integer parameter name to set the socket connection timeout (millis)
7778
+ http.read.timeout: A integer parameter name to set the socket read timeout (millis)

src/main/java/org/mitre/dsmiley/httpproxy/ProxyServlet.java

100644100755
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ public class ProxyServlet extends HttpServlet {
8484
/** A boolean parameter name to keep COOKIES as-is */
8585
public static final String P_PRESERVECOOKIES = "preserveCookies";
8686

87+
/** A boolean parameter name to keep COOKIE path as-is */
88+
public static final String P_PRESERVECOOKIEPATH = "preserveCookiePath";
89+
8790
/** A boolean parameter name to have auto-handle redirects */
8891
public static final String P_HANDLEREDIRECTS = "http.protocol.handle-redirects"; // ClientPNames.HANDLE_REDIRECTS
8992

@@ -121,6 +124,7 @@ public class ProxyServlet extends HttpServlet {
121124
protected boolean doSendUrlFragment = true;
122125
protected boolean doPreserveHost = false;
123126
protected boolean doPreserveCookies = false;
127+
protected boolean doPreserveCookiePath = false;
124128
protected boolean doHandleRedirects = false;
125129
protected boolean useSystemProperties = true;
126130
protected boolean doHandleCompression = false;
@@ -182,6 +186,11 @@ public void init() throws ServletException {
182186
this.doPreserveCookies = Boolean.parseBoolean(preserveCookiesString);
183187
}
184188

189+
String preserveCookiePathString = getConfigParam(P_PRESERVECOOKIEPATH);
190+
if (preserveCookiePathString != null) {
191+
this.doPreserveCookiePath = Boolean.parseBoolean(preserveCookiePathString);
192+
}
193+
185194
String handleRedirectsString = getConfigParam(P_HANDLEREDIRECTS);
186195
if (handleRedirectsString != null) {
187196
this.doHandleRedirects = Boolean.parseBoolean(handleRedirectsString);
@@ -590,7 +599,10 @@ protected void copyProxyCookie(HttpServletRequest servletRequest,
590599
protected Cookie createProxyCookie(HttpServletRequest servletRequest, HttpCookie cookie) {
591600
String proxyCookieName = getProxyCookieName(cookie);
592601
Cookie servletCookie = new Cookie(proxyCookieName, cookie.getValue());
593-
servletCookie.setPath(buildProxyCookiePath(servletRequest)); //set to the path of the proxy servlet
602+
servletCookie.setPath(this.doPreserveCookiePath ?
603+
cookie.getPath() : // preserve original cookie path
604+
buildProxyCookiePath(servletRequest) //set to the path of the proxy servlet
605+
);
594606
servletCookie.setComment(cookie.getComment());
595607
servletCookie.setMaxAge((int) cookie.getMaxAge());
596608
// don't set cookie domain

src/test/java/org/mitre/dsmiley/httpproxy/ProxyServletTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,35 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
402402
assertEquals("JSESSIONID=1234; COOKIE2=567", captureCookieValue.toString());
403403
}
404404

405+
@Test
406+
public void testPreserveCookiePath() throws Exception {
407+
servletRunner = new ServletRunner();
408+
409+
Properties servletProps = new Properties();
410+
servletProps.setProperty("http.protocol.handle-redirects", "false");
411+
servletProps.setProperty(ProxyServlet.P_LOG, "true");
412+
servletProps.setProperty(ProxyServlet.P_FORWARDEDFOR, "true");
413+
servletProps.setProperty(ProxyServlet.P_PRESERVECOOKIES, "true");
414+
servletProps.setProperty(ProxyServlet.P_PRESERVECOOKIEPATH, "true");
415+
setUpServlet(servletProps);
416+
417+
sc = servletRunner.newClient();
418+
sc.getClientProperties().setAutoRedirect(false);//don't want httpunit itself to redirect
419+
420+
final String HEADER = "Set-Cookie";
421+
localTestServer.register("/targetPath*", new RequestInfoHandler() {
422+
public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
423+
response.setHeader(HEADER, "JSESSIONID=1234; Path=/proxy/path/that/we/want; Expires=Wed, 13 Jan 2021 22:23:01 GMT; Domain=.foo.bar.com; HttpOnly");
424+
super.handle(request, response, context);
425+
}
426+
});
427+
428+
GetMethodWebRequest req = makeGetMethodRequest(sourceBaseUri);
429+
WebResponse rsp = execAndAssert(req, "");
430+
// note httpunit doesn't set all cookie fields, ignores max-agent, secure, etc.
431+
assertEquals("JSESSIONID=1234;path=/proxy/path/that/we/want", rsp.getHeaderField(HEADER));
432+
}
433+
405434
/**
406435
* If we're proxying a remote service that tries to set cookies, we need to make sure the cookies are not captured
407436
* by the httpclient in the ProxyServlet, otherwise later requests from ALL users will all access the remote proxy

0 commit comments

Comments
 (0)