Skip to content

Commit a1d6c1f

Browse files
committed
FELIX-6756 Cookie name "Path" is a reserved token
- Apply fix from patch - Add unit tests
1 parent 2d37b3c commit a1d6c1f

4 files changed

Lines changed: 186 additions & 40 deletions

File tree

http/wrappers/src/main/java/org/apache/felix/http/jakartawrappers/CookieWrapper.java

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,64 +50,92 @@ public static Cookie[] wrap(final javax.servlet.http.Cookie[] array) {
5050
* @param c Wrapped cookie
5151
*/
5252
public CookieWrapper(@NotNull final javax.servlet.http.Cookie c) {
53-
super(c.getName(), c.getValue());
53+
super("dummy", "dummy");
5454
this.cookie = c;
55-
super.setComment(c.getComment());
56-
if ( c.getDomain() != null ) {
57-
super.setDomain(c.getDomain());
58-
}
59-
super.setHttpOnly(c.isHttpOnly());
60-
super.setMaxAge(c.getMaxAge());
61-
super.setPath(c.getPath());
62-
super.setSecure(c.getSecure());
63-
super.setVersion(c.getVersion());
55+
}
56+
57+
@Override
58+
public String getName() {
59+
return this.cookie.getName();
60+
}
61+
62+
@Override
63+
public String getValue() {
64+
return this.cookie.getValue();
65+
}
66+
67+
@Override
68+
public void setValue(String value) {
69+
this.cookie.setValue(value);
6470
}
6571

6672
@Override
6773
public void setComment(final String purpose) {
6874
this.cookie.setComment(purpose);
69-
super.setComment(purpose);
75+
}
76+
77+
@Override
78+
public String getComment() {
79+
return this.cookie.getComment();
7080
}
7181

7282
@Override
7383
public void setDomain(final String domain) {
7484
this.cookie.setDomain(domain);
75-
super.setDomain(domain);
85+
}
86+
87+
@Override
88+
public String getDomain() {
89+
return this.cookie.getDomain();
7690
}
7791

7892
@Override
7993
public void setMaxAge(final int expiry) {
8094
this.cookie.setMaxAge(expiry);
81-
super.setMaxAge(expiry);
95+
}
96+
97+
@Override
98+
public int getMaxAge() {
99+
return this.cookie.getMaxAge();
82100
}
83101

84102
@Override
85103
public void setPath(final String uri) {
86104
this.cookie.setPath(uri);
87-
super.setPath(uri);
105+
}
106+
107+
@Override
108+
public String getPath() {
109+
return this.cookie.getPath();
88110
}
89111

90112
@Override
91113
public void setSecure(final boolean flag) {
92114
this.cookie.setSecure(flag);
93-
super.setSecure(flag);
94115
}
95116

96117
@Override
97-
public void setValue(final String newValue) {
98-
this.cookie.setValue(newValue);
99-
super.setValue(newValue);
118+
public boolean getSecure() {
119+
return this.cookie.getSecure();
100120
}
101121

102122
@Override
103123
public void setVersion(final int v) {
104124
this.cookie.setVersion(v);
105-
super.setVersion(v);
125+
}
126+
127+
@Override
128+
public int getVersion() {
129+
return this.cookie.getVersion();
106130
}
107131

108132
@Override
109133
public void setHttpOnly(final boolean isHttpOnly) {
110134
this.cookie.setHttpOnly(isHttpOnly);
111-
super.setHttpOnly(isHttpOnly);
135+
}
136+
137+
@Override
138+
public boolean isHttpOnly() {
139+
return this.cookie.isHttpOnly();
112140
}
113141
}

http/wrappers/src/main/java/org/apache/felix/http/javaxwrappers/CookieWrapper.java

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,67 +47,97 @@ public static javax.servlet.http.Cookie[] wrap(final Cookie[] array) {
4747

4848
/**
4949
* Create new cookie
50+
*
5051
* @param c Wrapped cookie
5152
*/
5253
public CookieWrapper(@NotNull final Cookie c) {
53-
super(c.getName(), c.getValue());
54+
super("dummy", "dummy");
5455
this.cookie = c;
55-
super.setComment(c.getComment());
56-
if ( c.getDomain() != null ) {
57-
super.setDomain(c.getDomain());
58-
}
59-
super.setHttpOnly(c.isHttpOnly());
60-
super.setMaxAge(c.getMaxAge());
61-
super.setPath(c.getPath());
62-
super.setSecure(c.getSecure());
63-
super.setVersion(c.getVersion());
56+
}
57+
58+
@Override
59+
public String getName() {
60+
return this.cookie.getName();
61+
}
62+
63+
@Override
64+
public String getValue() {
65+
return this.cookie.getValue();
66+
}
67+
68+
@Override
69+
public void setValue(String value) {
70+
this.cookie.setValue(value);
6471
}
6572

6673
@Override
6774
public void setComment(final String purpose) {
6875
this.cookie.setComment(purpose);
69-
super.setComment(purpose);
76+
}
77+
78+
@Override
79+
public String getComment() {
80+
return this.cookie.getComment();
7081
}
7182

7283
@Override
7384
public void setDomain(final String domain) {
7485
this.cookie.setDomain(domain);
75-
super.setDomain(domain);
86+
}
87+
88+
@Override
89+
public String getDomain() {
90+
return this.cookie.getDomain();
7691
}
7792

7893
@Override
7994
public void setMaxAge(final int expiry) {
8095
this.cookie.setMaxAge(expiry);
81-
super.setMaxAge(expiry);
96+
}
97+
98+
@Override
99+
public int getMaxAge() {
100+
return this.cookie.getMaxAge();
82101
}
83102

84103
@Override
85104
public void setPath(final String uri) {
86105
this.cookie.setPath(uri);
87-
super.setPath(uri);
106+
}
107+
108+
@Override
109+
public String getPath() {
110+
return this.cookie.getPath();
88111
}
89112

90113
@Override
91114
public void setSecure(final boolean flag) {
92115
this.cookie.setSecure(flag);
93-
super.setSecure(flag);
94116
}
95117

96118
@Override
97-
public void setValue(final String newValue) {
98-
this.cookie.setValue(newValue);
99-
super.setValue(newValue);
119+
public boolean getSecure() {
120+
return this.cookie.getSecure();
100121
}
101122

102123
@Override
103124
public void setVersion(final int v) {
104125
this.cookie.setVersion(v);
105-
super.setVersion(v);
126+
}
127+
128+
@Override
129+
public int getVersion() {
130+
return this.cookie.getVersion();
106131
}
107132

108133
@Override
109134
public void setHttpOnly(final boolean isHttpOnly) {
110135
this.cookie.setHttpOnly(isHttpOnly);
111-
super.setHttpOnly(isHttpOnly);
112136
}
137+
138+
@Override
139+
public boolean isHttpOnly() {
140+
return this.cookie.isHttpOnly();
141+
}
142+
113143
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.felix.http.jakartawrappers;
18+
19+
import javax.servlet.http.Cookie;
20+
21+
import org.junit.Test;
22+
import org.mockito.Mockito;
23+
24+
import static org.junit.Assert.assertEquals;
25+
import static org.mockito.Mockito.when;
26+
27+
public class CookieWrapperTest {
28+
@Test
29+
public void testReservedCookieNames() {
30+
testCookie("Path");
31+
testCookie("MaxAge");
32+
testCookie("Comment");
33+
}
34+
35+
private static void testCookie(String cookieName) {
36+
Cookie pathCookie = Mockito.mock(Cookie.class);
37+
when(pathCookie.getName()).thenReturn(cookieName);
38+
39+
// Threw `java.lang.IllegalArgumentException: Cookie name "Path" is a reserved token` before
40+
CookieWrapper cookieWrapper = new CookieWrapper(pathCookie);
41+
42+
assertEquals(cookieName, cookieWrapper.getName());
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.felix.http.javaxwrappers;
18+
19+
import jakarta.servlet.http.Cookie;
20+
21+
import org.junit.Test;
22+
import org.mockito.Mockito;
23+
24+
import static org.junit.Assert.assertEquals;
25+
import static org.mockito.Mockito.when;
26+
27+
public class CookieWrapperTest {
28+
@Test
29+
public void testReservedCookieNames() {
30+
testCookie("Path");
31+
testCookie("MaxAge");
32+
testCookie("Comment");
33+
}
34+
35+
private static void testCookie(String cookieName) {
36+
Cookie pathCookie = Mockito.mock(Cookie.class);
37+
when(pathCookie.getName()).thenReturn(cookieName);
38+
39+
// Threw `java.lang.IllegalArgumentException: Cookie name "Path" is a reserved token` before
40+
CookieWrapper cookieWrapper = new CookieWrapper(pathCookie);
41+
42+
assertEquals(cookieName, cookieWrapper.getName());
43+
}
44+
}

0 commit comments

Comments
 (0)