You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by jh...@apache.org on 2009/03/02 06:12:46 UTC
svn commit: r749188 - in /incubator/jsecurity/import/trunk:
src/org/jsecurity/web/attr/CookieAttribute.java
test/org/jsecurity/web/attr/CookieAttributeTest.java
Author: jhaile
Date: Mon Mar 2 05:12:46 2009
New Revision: 749188
URL: http://svn.apache.org/viewvc?rev=749188&view=rev
Log:
Applied patch from JSEC-64 and added a unit test to verify fix.
Modified:
incubator/jsecurity/import/trunk/src/org/jsecurity/web/attr/CookieAttribute.java
incubator/jsecurity/import/trunk/test/org/jsecurity/web/attr/CookieAttributeTest.java
Modified: incubator/jsecurity/import/trunk/src/org/jsecurity/web/attr/CookieAttribute.java
URL: http://svn.apache.org/viewvc/incubator/jsecurity/import/trunk/src/org/jsecurity/web/attr/CookieAttribute.java?rev=749188&r1=749187&r2=749188&view=diff
==============================================================================
--- incubator/jsecurity/import/trunk/src/org/jsecurity/web/attr/CookieAttribute.java (original)
+++ incubator/jsecurity/import/trunk/src/org/jsecurity/web/attr/CookieAttribute.java Mon Mar 2 05:12:46 2009
@@ -20,6 +20,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.jsecurity.util.StringUtils;
import static org.jsecurity.web.WebUtils.toHttp;
import javax.servlet.ServletRequest;
@@ -59,6 +60,10 @@
*/
public static final String DEFAULT_PATH = null;
/**
+ * The root path that should be set on cookies when context root is null and no path is explicitly set.
+ */
+ public static final String ROOT_PATH = "/";
+ /**
* <code>-1</code>, indicating the cookie should expire when the browser closes.
*/
public static final int DEFAULT_MAX_AGE = -1;
@@ -162,6 +167,29 @@
return path;
}
+
+ /**
+ * Returns the Cookie's calculated path setting. If {@link Cookie#getPath() path} <tt>null</tt>, then the
+ * <tt>request</tt>'s {@link javax.servlet.http.HttpServletRequest#getContextPath() context path}
+ * will be returned. If getContextPath() is the empty string or null then the ROOT_PATH constant is returned.
+ * <p/>
+ * <p>The default is <code>null</code>.</p>
+ *
+ * @return the path to be used as the path when the cookie is created or removed.
+ */
+ public String calculatePath(HttpServletRequest request) {
+ String calculatePath = getPath() != null ? getPath() : request.getContextPath();
+
+ //fix for http://issues.apache.org/jira/browse/JSEC-34:
+ calculatePath = StringUtils.clean(calculatePath);
+ if (calculatePath == null) {
+ calculatePath = ROOT_PATH;
+ }
+ log.trace ("calculatePath: returning=" + calculatePath);
+ return calculatePath;
+ }
+
+
/**
* Sets the Cookie's {@link Cookie#getPath() path} setting. If the argument is <tt>null</tt>, the <tt>request</tt>'s
* {@link javax.servlet.http.HttpServletRequest#getContextPath() context path} will be used.
@@ -255,7 +283,7 @@
String name = getName();
int maxAge = getMaxAge();
- String path = getPath() != null ? getPath() : request.getContextPath();
+ String path = calculatePath(request);
String stringValue = toStringValue(value);
Cookie cookie = new Cookie(name, stringValue);
@@ -266,6 +294,7 @@
}
response.addCookie(cookie);
+
if (log.isTraceEnabled()) {
log.trace("Added Cookie [" + name + "] to path [" + path + "] with value [" +
stringValue + "] to the HttpServletResponse.");
@@ -275,13 +304,17 @@
public void removeValue(ServletRequest servletRequest, ServletResponse response) {
HttpServletRequest request = toHttp(servletRequest);
Cookie cookie = getCookie(request, getName());
+
if (cookie != null) {
cookie.setMaxAge(0);
+ cookie.setValue("forgetme");
//JSEC-94: Must set the path on the outgoing cookie (some browsers don't retain it from the
//retrieved cookie?)
- cookie.setPath(getPath() == null ? request.getContextPath() : getPath());
+ // my testing shows none of these browsers will remove cookie if setPath() is not invoked: FF3, Chrome, IE7, Safari windows
+ cookie.setPath(calculatePath(request));
cookie.setSecure(isSecure());
toHttp(response).addCookie(cookie);
+ log.trace("Removed cookie[" + getName() + "] with path [" + calculatePath(request) + "] from HttpServletResponse.");
}
}
}
Modified: incubator/jsecurity/import/trunk/test/org/jsecurity/web/attr/CookieAttributeTest.java
URL: http://svn.apache.org/viewvc/incubator/jsecurity/import/trunk/test/org/jsecurity/web/attr/CookieAttributeTest.java?rev=749188&r1=749187&r2=749188&view=diff
==============================================================================
--- incubator/jsecurity/import/trunk/test/org/jsecurity/web/attr/CookieAttributeTest.java (original)
+++ incubator/jsecurity/import/trunk/test/org/jsecurity/web/attr/CookieAttributeTest.java Mon Mar 2 05:12:46 2009
@@ -54,7 +54,7 @@
expect(mockRequest.getCookies()).andReturn(cookies);
//no path set on the cookie, so we expect to retrieve it from the context path
- expect(mockRequest.getContextPath()).andReturn("/somepath");
+ expect(mockRequest.getContextPath()).andReturn("/somepath").times(2);
mockResponse.addCookie(cookie);
replay(mockRequest);
replay(mockResponse);
@@ -67,4 +67,28 @@
assertTrue(cookie.getMaxAge() == 0);
assertTrue(cookie.getPath().equals("/somepath"));
}
+
+ @Test
+ //Verifies fix for JSEC-64
+ public void testRemoveValueWithNullContext() throws Exception {
+
+ Cookie cookie = new Cookie("test", "blah");
+ cookie.setMaxAge(2351234); //doesn't matter what the time is
+ Cookie[] cookies = new Cookie[]{cookie};
+
+ expect(mockRequest.getCookies()).andReturn(cookies);
+ //no path set on the cookie, so we expect to retrieve it from the context path
+ expect(mockRequest.getContextPath()).andReturn(null).times(2);
+ mockResponse.addCookie(cookie);
+ replay(mockRequest);
+ replay(mockResponse);
+
+ cookieAttribute.removeValue(mockRequest, mockResponse);
+
+ verify(mockRequest);
+ verify(mockResponse);
+
+ assertTrue(cookie.getMaxAge() == 0);
+ assertTrue(cookie.getPath().equals("/"));
+ }
}