Skip to content

Commit

Permalink
FELIX-6756 Cookie name "Path" is a reserved token
Browse files Browse the repository at this point in the history
- Apply fix from patch
- Add unit tests
  • Loading branch information
paulrutter committed Mar 10, 2025
1 parent 2d37b3c commit a1d6c1f
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,64 +50,92 @@ public static Cookie[] wrap(final javax.servlet.http.Cookie[] array) {
* @param c Wrapped cookie
*/
public CookieWrapper(@NotNull final javax.servlet.http.Cookie c) {
super(c.getName(), c.getValue());
super("dummy", "dummy");
this.cookie = c;
super.setComment(c.getComment());
if ( c.getDomain() != null ) {
super.setDomain(c.getDomain());
}
super.setHttpOnly(c.isHttpOnly());
super.setMaxAge(c.getMaxAge());
super.setPath(c.getPath());
super.setSecure(c.getSecure());
super.setVersion(c.getVersion());
}

@Override
public String getName() {
return this.cookie.getName();
}

@Override
public String getValue() {
return this.cookie.getValue();
}

@Override
public void setValue(String value) {
this.cookie.setValue(value);
}

@Override
public void setComment(final String purpose) {
this.cookie.setComment(purpose);
super.setComment(purpose);
}

@Override
public String getComment() {
return this.cookie.getComment();
}

@Override
public void setDomain(final String domain) {
this.cookie.setDomain(domain);
super.setDomain(domain);
}

@Override
public String getDomain() {
return this.cookie.getDomain();
}

@Override
public void setMaxAge(final int expiry) {
this.cookie.setMaxAge(expiry);
super.setMaxAge(expiry);
}

@Override
public int getMaxAge() {
return this.cookie.getMaxAge();
}

@Override
public void setPath(final String uri) {
this.cookie.setPath(uri);
super.setPath(uri);
}

@Override
public String getPath() {
return this.cookie.getPath();
}

@Override
public void setSecure(final boolean flag) {
this.cookie.setSecure(flag);
super.setSecure(flag);
}

@Override
public void setValue(final String newValue) {
this.cookie.setValue(newValue);
super.setValue(newValue);
public boolean getSecure() {
return this.cookie.getSecure();
}

@Override
public void setVersion(final int v) {
this.cookie.setVersion(v);
super.setVersion(v);
}

@Override
public int getVersion() {
return this.cookie.getVersion();
}

@Override
public void setHttpOnly(final boolean isHttpOnly) {
this.cookie.setHttpOnly(isHttpOnly);
super.setHttpOnly(isHttpOnly);
}

@Override
public boolean isHttpOnly() {
return this.cookie.isHttpOnly();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,67 +47,97 @@ public static javax.servlet.http.Cookie[] wrap(final Cookie[] array) {

/**
* Create new cookie
*
* @param c Wrapped cookie
*/
public CookieWrapper(@NotNull final Cookie c) {
super(c.getName(), c.getValue());
super("dummy", "dummy");
this.cookie = c;
super.setComment(c.getComment());
if ( c.getDomain() != null ) {
super.setDomain(c.getDomain());
}
super.setHttpOnly(c.isHttpOnly());
super.setMaxAge(c.getMaxAge());
super.setPath(c.getPath());
super.setSecure(c.getSecure());
super.setVersion(c.getVersion());
}

@Override
public String getName() {
return this.cookie.getName();
}

@Override
public String getValue() {
return this.cookie.getValue();
}

@Override
public void setValue(String value) {
this.cookie.setValue(value);
}

@Override
public void setComment(final String purpose) {
this.cookie.setComment(purpose);
super.setComment(purpose);
}

@Override
public String getComment() {
return this.cookie.getComment();
}

@Override
public void setDomain(final String domain) {
this.cookie.setDomain(domain);
super.setDomain(domain);
}

@Override
public String getDomain() {
return this.cookie.getDomain();
}

@Override
public void setMaxAge(final int expiry) {
this.cookie.setMaxAge(expiry);
super.setMaxAge(expiry);
}

@Override
public int getMaxAge() {
return this.cookie.getMaxAge();
}

@Override
public void setPath(final String uri) {
this.cookie.setPath(uri);
super.setPath(uri);
}

@Override
public String getPath() {
return this.cookie.getPath();
}

@Override
public void setSecure(final boolean flag) {
this.cookie.setSecure(flag);
super.setSecure(flag);
}

@Override
public void setValue(final String newValue) {
this.cookie.setValue(newValue);
super.setValue(newValue);
public boolean getSecure() {
return this.cookie.getSecure();
}

@Override
public void setVersion(final int v) {
this.cookie.setVersion(v);
super.setVersion(v);
}

@Override
public int getVersion() {
return this.cookie.getVersion();
}

@Override
public void setHttpOnly(final boolean isHttpOnly) {
this.cookie.setHttpOnly(isHttpOnly);
super.setHttpOnly(isHttpOnly);
}

@Override
public boolean isHttpOnly() {
return this.cookie.isHttpOnly();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.felix.http.jakartawrappers;

import javax.servlet.http.Cookie;

import org.junit.Test;
import org.mockito.Mockito;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

public class CookieWrapperTest {
@Test
public void testReservedCookieNames() {
testCookie("Path");
testCookie("MaxAge");
testCookie("Comment");
}

private static void testCookie(String cookieName) {
Cookie pathCookie = Mockito.mock(Cookie.class);
when(pathCookie.getName()).thenReturn(cookieName);

// Threw `java.lang.IllegalArgumentException: Cookie name "Path" is a reserved token` before
CookieWrapper cookieWrapper = new CookieWrapper(pathCookie);

assertEquals(cookieName, cookieWrapper.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.felix.http.javaxwrappers;

import jakarta.servlet.http.Cookie;

import org.junit.Test;
import org.mockito.Mockito;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

public class CookieWrapperTest {
@Test
public void testReservedCookieNames() {
testCookie("Path");
testCookie("MaxAge");
testCookie("Comment");
}

private static void testCookie(String cookieName) {
Cookie pathCookie = Mockito.mock(Cookie.class);
when(pathCookie.getName()).thenReturn(cookieName);

// Threw `java.lang.IllegalArgumentException: Cookie name "Path" is a reserved token` before
CookieWrapper cookieWrapper = new CookieWrapper(pathCookie);

assertEquals(cookieName, cookieWrapper.getName());
}
}

0 comments on commit a1d6c1f

Please sign in to comment.