diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java index 95e10a2e17e..dc2808e0b1f 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java @@ -230,26 +230,30 @@ public class Response implements HttpServletResponse @Override public void addCookie(Cookie cookie) { - if (StringUtil.isBlank(cookie.getName())) - throw new IllegalArgumentException("Cookie.name cannot be blank/null"); + //Servlet Spec 9.3 Include method: cannot set a cookie if handling an include + if (isMutable()) + { + if (StringUtil.isBlank(cookie.getName())) + throw new IllegalArgumentException("Cookie.name cannot be blank/null"); - String comment = cookie.getComment(); - // HttpOnly was supported as a comment in cookie flags before the java.net.HttpCookie implementation so need to check that - boolean httpOnly = cookie.isHttpOnly() || HttpCookie.isHttpOnlyInComment(comment); - SameSite sameSite = HttpCookie.getSameSiteFromComment(comment); - comment = HttpCookie.getCommentWithoutAttributes(comment); + String comment = cookie.getComment(); + // HttpOnly was supported as a comment in cookie flags before the java.net.HttpCookie implementation so need to check that + boolean httpOnly = cookie.isHttpOnly() || HttpCookie.isHttpOnlyInComment(comment); + SameSite sameSite = HttpCookie.getSameSiteFromComment(comment); + comment = HttpCookie.getCommentWithoutAttributes(comment); - addCookie(new HttpCookie( - cookie.getName(), - cookie.getValue(), - cookie.getDomain(), - cookie.getPath(), - cookie.getMaxAge(), - httpOnly, - cookie.getSecure(), - comment, - cookie.getVersion(), - sameSite)); + addCookie(new HttpCookie( + cookie.getName(), + cookie.getValue(), + cookie.getDomain(), + cookie.getPath(), + cookie.getMaxAge(), + httpOnly, + cookie.getSecure(), + comment, + cookie.getVersion(), + sameSite)); + } } /** @@ -302,7 +306,6 @@ public class Response implements HttpServletResponse addCookie(cookie); } - @Override public boolean containsHeader(String name) { return _fields.contains(name); diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java index a567c681492..3f3b3220061 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java @@ -1094,6 +1094,23 @@ public class ResponseTest assertEquals("name=value; Path=/path; Domain=domain; Secure; HttpOnly", set); } + @Test + public void testAddCookieInInclude() throws Exception + { + Response response = getResponse(); + response.include(); + + Cookie cookie = new Cookie("naughty", "value"); + cookie.setDomain("domain"); + cookie.setPath("/path"); + cookie.setSecure(true); + cookie.setComment("comment__HTTP_ONLY__"); + + response.addCookie(cookie); + + assertNull(response.getHttpFields().get("Set-Cookie")); + } + @Test public void testAddCookieSameSiteDefault() throws Exception {