Skip to content

Commit 6885d0e

Browse files
authored
#20334 Setting COOKIES_HTTP_ONLY=TRUE or COOKIES_SECURE_FLAG=always/https causes cookie duplication
* #20334 the domain set on the cookies on a weird path, setting to / to avoid dupes * #20334 the domain set on the cookies on a weird path, setting to / to avoid dupes * #20334 adding unit test for cookie * #20334 adding the test to the MainSuite
1 parent 7dbde1f commit 6885d0e

5 files changed

Lines changed: 100 additions & 6 deletions

File tree

dotCMS/src/integration-test/java/com/dotcms/MainSuite.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
import com.dotmarketing.util.HashBuilderTest;
103103
import com.dotmarketing.util.TestConfig;
104104
import com.liferay.portal.language.LanguageUtilTest;
105+
import org.apache.velocity.tools.view.tools.CookieToolTest;
105106
import org.junit.runner.RunWith;
106107
import org.junit.runners.Suite.SuiteClasses;
107108

@@ -418,7 +419,8 @@
418419
StaticPushPublishBundleGeneratorTest.class,
419420
Task210520UpdateAnonymousEmailTest.class,
420421
Task210510UpdateStorageTableDropMetadataColumnTest.class,
421-
StaticPushPublishBundleGeneratorTest.class
422+
StaticPushPublishBundleGeneratorTest.class,
423+
CookieToolTest.class
422424
})
423425
public class MainSuite {
424426

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.apache.velocity.tools.view.tools;
2+
3+
import com.dotmarketing.filters.CookieServletResponse;
4+
import org.apache.commons.lang.mutable.MutableObject;
5+
import org.apache.velocity.tools.view.context.ViewContext;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
import org.mockito.Mockito;
9+
10+
import javax.servlet.http.Cookie;
11+
import javax.servlet.http.HttpServletRequest;
12+
import javax.servlet.http.HttpServletResponse;
13+
14+
public class CookieToolTest {
15+
16+
/**
17+
* Method to test: {@link CookieTool#add(String, String)}
18+
* Given Scenario: Adding a cookie should have the path to /
19+
* ExpectedResult: The cookie path should be /
20+
*
21+
*
22+
*/
23+
@Test()
24+
public void test_add_cookie_success () {
25+
26+
final CookieTool cookieTool = new CookieTool();
27+
final ViewContext context = Mockito.mock(ViewContext.class);
28+
final MutableObject cookieHolder = new MutableObject();
29+
final HttpServletResponse response = new CookieServletResponse(Mockito.mock(HttpServletResponse.class), true) {
30+
31+
@Override
32+
public void addCookie(final Cookie cookie) {
33+
cookieHolder.setValue(cookie);
34+
}
35+
};
36+
final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
37+
38+
Mockito.when(context.getResponse()).thenReturn(response);
39+
Mockito.when(context.getRequest()).thenReturn(request);
40+
cookieTool.init(context);
41+
42+
cookieTool.add("name", "value");
43+
Assert.assertNotNull(cookieHolder.getValue());
44+
final Cookie cookie = (Cookie) cookieHolder.getValue();
45+
46+
Assert.assertEquals("name", cookie.getName());
47+
Assert.assertEquals("value", cookie.getValue());
48+
Assert.assertEquals("/", cookie.getPath());
49+
}
50+
51+
/**
52+
* Method to test: {@link CookieTool#add(String, String, int)}
53+
* Given Scenario: Adding a cookie should have the path to / and max age 1000
54+
* ExpectedResult: The cookie path should be / and max age 1000
55+
*
56+
*
57+
*/
58+
@Test()
59+
public void test_add_cookie_max_timesuccess () {
60+
61+
final CookieTool cookieTool = new CookieTool();
62+
final ViewContext context = Mockito.mock(ViewContext.class);
63+
final MutableObject cookieHolder = new MutableObject();
64+
final HttpServletResponse response = new CookieServletResponse(Mockito.mock(HttpServletResponse.class), true) {
65+
66+
@Override
67+
public void addCookie(final Cookie cookie) {
68+
cookieHolder.setValue(cookie);
69+
}
70+
};
71+
final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
72+
73+
Mockito.when(context.getResponse()).thenReturn(response);
74+
Mockito.when(context.getRequest()).thenReturn(request);
75+
cookieTool.init(context);
76+
77+
cookieTool.add("name", "value", 1000);
78+
Assert.assertNotNull(cookieHolder.getValue());
79+
final Cookie cookie = (Cookie) cookieHolder.getValue();
80+
81+
Assert.assertEquals("name", cookie.getName());
82+
Assert.assertEquals("value", cookie.getValue());
83+
Assert.assertEquals("/", cookie.getPath());
84+
Assert.assertEquals(1000, cookie.getMaxAge());
85+
}
86+
}

dotCMS/src/main/java/com/dotmarketing/filters/CookieServletResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public void addCookie(final Cookie cookie) {
5252
if ( SEND_COOKIES_SECURE_ALWAYS || this.requestIsSecure && SEND_COOKIES_SECURE_WHEN_HTTPS){
5353
cookie.setSecure(true);
5454
}
55-
55+
56+
cookie.setPath(CookieUtil.URI);
5657

5758
super.addCookie(cookie);
5859
}

dotCMS/src/main/java/com/dotmarketing/util/CookieUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class CookieUtil {
2323

2424
public static final String ALWAYS = "always";
2525
public static final String HTTPS = "https";
26-
private static final String URI = "/";
26+
public static final String URI = "/";
2727
private static final int MAX_AGE_DAY_MILLIS = 60 * 60 * 24;
2828
public static final int DEFAULT_JWT_MAX_AGE_DAYS = 14;
2929

@@ -175,4 +175,4 @@ public static void setHttpOnlyCookie(final Cookie cookie) {
175175
cookie.setHttpOnly(true);
176176
}
177177
} // setHttpOnlyCookie.
178-
}
178+
}

dotCMS/src/main/java/org/apache/velocity/tools/view/tools/CookieTool.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import javax.servlet.http.HttpServletRequest;
2121
import javax.servlet.http.HttpServletResponse;
2222
import javax.servlet.http.Cookie;
23+
24+
import com.dotmarketing.util.CookieUtil;
2325
import org.apache.velocity.tools.view.context.ViewContext;
2426

2527
/**
@@ -114,7 +116,9 @@ public Cookie get(String name)
114116
*/
115117
public void add(String name, String value)
116118
{
117-
response.addCookie(new Cookie(name, value));
119+
final Cookie c = new Cookie(name, value);
120+
c.setPath(CookieUtil.URI);
121+
response.addCookie(c);
118122
}
119123

120124

@@ -129,7 +133,8 @@ public void add(String name, String value)
129133
public void add(String name, String value, int maxAge)
130134
{
131135
/* c is for cookie. that's good enough for me. */
132-
Cookie c = new Cookie(name, value);
136+
final Cookie c = new Cookie(name, value);
137+
c.setPath(CookieUtil.URI);
133138
c.setMaxAge(maxAge);
134139
response.addCookie(c);
135140
}

0 commit comments

Comments
 (0)