undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
56
test/jdk/java/net/HttpCookie/CookieNegativeMaxAge.java
Normal file
56
test/jdk/java/net/HttpCookie/CookieNegativeMaxAge.java
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8005068
|
||||
* @summary Check that any negative maxAge is treated as "unspecified" and
|
||||
* if header contains cookie with "expires" attribute in the past then cookie
|
||||
* is created with maxAge=0 meaning it is specified to be immediately expired.
|
||||
* @run main CookieNegativeMaxAge
|
||||
*/
|
||||
|
||||
|
||||
import java.net.HttpCookie;
|
||||
import java.util.List;
|
||||
|
||||
public class CookieNegativeMaxAge {
|
||||
|
||||
public static void main(String... args) {
|
||||
HttpCookie cookie = new HttpCookie("testCookie", "value");
|
||||
cookie.setMaxAge(Integer.MIN_VALUE);
|
||||
if (cookie.hasExpired()) {
|
||||
throw new RuntimeException("Cookie has unexpectedly expired");
|
||||
}
|
||||
|
||||
List<HttpCookie> cookies = HttpCookie.parse("Set-Cookie: " +
|
||||
"expiredCookie=value; expires=Thu, 01 Jan 1970 00:00:00 GMT");
|
||||
if (cookies.size() == 1) {
|
||||
if (cookies.get(0).getMaxAge() != 0) {
|
||||
throw new RuntimeException("Cookie maxAge expected to be 0");
|
||||
}
|
||||
} else {
|
||||
throw new RuntimeException("Header was incorrectly parsed");
|
||||
}
|
||||
}
|
||||
}
|
||||
106
test/jdk/java/net/HttpCookie/ExpiredCookieTest.java
Normal file
106
test/jdk/java/net/HttpCookie/ExpiredCookieTest.java
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
/*
|
||||
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8000525
|
||||
* @library /test/lib
|
||||
*/
|
||||
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
import java.text.*;
|
||||
import jdk.test.lib.net.URIBuilder;
|
||||
|
||||
public class ExpiredCookieTest {
|
||||
// lifted from HttpCookie.java
|
||||
private final static String[] COOKIE_DATE_FORMATS = {
|
||||
"EEE',' dd-MMM-yy HH:mm:ss 'GMT'",
|
||||
"EEE',' dd MMM yy HH:mm:ss 'GMT'",
|
||||
"EEE MMM dd yy HH:mm:ss 'GMT'Z",
|
||||
"EEE',' dd-MMM-yyyy HH:mm:ss 'GMT'",
|
||||
"EEE',' dd MMM yyyy HH:mm:ss 'GMT'",
|
||||
"EEE MMM dd yyyy HH:mm:ss 'GMT'Z"
|
||||
};
|
||||
static final TimeZone GMT = TimeZone.getTimeZone("GMT");
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Calendar cal = Calendar.getInstance(GMT);
|
||||
|
||||
for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
|
||||
SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
|
||||
Locale.US);
|
||||
cal.set(1970, 0, 1, 0, 0, 0);
|
||||
df.setTimeZone(GMT);
|
||||
df.setLenient(false);
|
||||
df.set2DigitYearStart(cal.getTime());
|
||||
CookieManager cm = new CookieManager(
|
||||
null, CookiePolicy.ACCEPT_ALL);
|
||||
CookieHandler.setDefault(cm);
|
||||
Map<String,List<String>> header = new HashMap<>();
|
||||
List<String> values = new ArrayList<>();
|
||||
|
||||
cal.set(1970, 6, 9, 10, 10, 1);
|
||||
StringBuilder datestring =
|
||||
new StringBuilder(df.format(cal.getTime()));
|
||||
values.add(
|
||||
"TEST1=TEST1; Path=/; Expires=" + datestring.toString());
|
||||
|
||||
cal.set(1969, 6, 9, 10, 10, 2);
|
||||
datestring = new StringBuilder(df.format(cal.getTime()));
|
||||
values.add(
|
||||
"TEST2=TEST2; Path=/; Expires=" + datestring.toString());
|
||||
|
||||
cal.set(2070, 6, 9, 10, 10, 3);
|
||||
datestring = new StringBuilder(df.format(cal.getTime()));
|
||||
values.add(
|
||||
"TEST3=TEST3; Path=/; Expires=" + datestring.toString());
|
||||
|
||||
cal.set(2069, 6, 9, 10, 10, 4);
|
||||
datestring = new StringBuilder(df.format(cal.getTime()));
|
||||
values.add(
|
||||
"TEST4=TEST4; Path=/; Expires=" + datestring.toString());
|
||||
|
||||
header.put("Set-Cookie", values);
|
||||
URI uri = URIBuilder.newBuilder()
|
||||
.scheme("http")
|
||||
.loopback()
|
||||
.path("/")
|
||||
.buildUnchecked();
|
||||
System.out.println("URI: " + uri);
|
||||
cm.put(uri, header);
|
||||
|
||||
CookieStore cookieJar = cm.getCookieStore();
|
||||
List <HttpCookie> cookies = cookieJar.getCookies();
|
||||
if (COOKIE_DATE_FORMATS[i].contains("yyyy")) {
|
||||
if (cookies.size() != 2)
|
||||
throw new RuntimeException(
|
||||
"Incorrectly parsing a bad date");
|
||||
} else if (cookies.size() != 1) {
|
||||
throw new RuntimeException(
|
||||
"Incorrectly parsing a bad date");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
85
test/jdk/java/net/HttpCookie/IllegalCookieNameTest.java
Normal file
85
test/jdk/java/net/HttpCookie/IllegalCookieNameTest.java
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 7183292
|
||||
* @library /test/lib
|
||||
* @modules jdk.httpserver
|
||||
* @run main IllegalCookieNameTest
|
||||
* @run main/othervm -Djava.net.preferIPv6Addresses=true IllegalCookieNameTest
|
||||
*/
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
import com.sun.net.httpserver.*;
|
||||
import jdk.test.lib.net.URIBuilder;
|
||||
|
||||
public class IllegalCookieNameTest {
|
||||
public static void main(String[] args) throws Exception {
|
||||
HttpServer s = null;
|
||||
try {
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
InetSocketAddress addr = new InetSocketAddress(loopback, 0);
|
||||
s = HttpServer.create(addr, 10);
|
||||
s.createContext("/", new HHandler());
|
||||
s.start();
|
||||
String u = URIBuilder.newBuilder()
|
||||
.scheme("http")
|
||||
.loopback()
|
||||
.port(s.getAddress().getPort())
|
||||
.path("/")
|
||||
.build().toString();
|
||||
CookieHandler.setDefault(new TestCookieHandler());
|
||||
URL url = new URL(u);
|
||||
HttpURLConnection c = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
|
||||
c.getHeaderFields();
|
||||
System.out.println ("OK");
|
||||
} finally {
|
||||
s.stop(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TestCookieHandler extends CookieHandler {
|
||||
@Override
|
||||
public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) {
|
||||
return new HashMap<String, List<String>>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(URI uri, Map<String, List<String>> responseHeaders) {
|
||||
}
|
||||
}
|
||||
|
||||
class HHandler implements HttpHandler {
|
||||
public void handle (HttpExchange e) {
|
||||
try {
|
||||
Headers h = e.getResponseHeaders();
|
||||
h.set ("Set-Cookie", "domain=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=.foo.com");
|
||||
e.sendResponseHeaders(200, -1);
|
||||
e.close();
|
||||
} catch (Exception ex) {
|
||||
System.out.println (ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8351983
|
||||
* @summary HttpCookie Parser Incorrectly Handles Cookies with Expires Attribute
|
||||
* @run testng java.base/java.net.MaxAgeExpires
|
||||
*/
|
||||
public class MaxAgeExpiresDriver {
|
||||
}
|
||||
|
|
@ -0,0 +1,137 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.net;
|
||||
|
||||
import java.time.*;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
import org.testng.annotations.DataProvider;
|
||||
|
||||
public class MaxAgeExpires {
|
||||
|
||||
static final DateTimeFormatter dtFormatter =
|
||||
DateTimeFormatter.RFC_1123_DATE_TIME;
|
||||
|
||||
static final String NOW_PLUS_500_SEC =
|
||||
dtFormatter.format(ZonedDateTime.ofInstant(Instant.now()
|
||||
.plusSeconds(500), ZoneId.of("UTC")));
|
||||
|
||||
// Test dates 1 minute apart
|
||||
static final String DT1 = "Mon, 01 Jan 2024 01:00:00 GMT";
|
||||
static final String DT2 = "Mon, 01 Jan 2024 01:01:00 GMT";
|
||||
static final String DT3 = "Mon, 01 Jan 2024 01:02:00 GMT";
|
||||
|
||||
static final String FAR_FUTURE = "Mon, 01 Jan 4024 01:02:00 GMT";
|
||||
|
||||
static final ZonedDateTime zdt1 = ZonedDateTime.parse(DT1, dtFormatter);
|
||||
static final ZonedDateTime zdt2 = ZonedDateTime.parse(DT2, dtFormatter);
|
||||
static final ZonedDateTime zdt3 = ZonedDateTime.parse(DT3, dtFormatter);
|
||||
|
||||
static long zdtToMillis(ZonedDateTime zdt) {
|
||||
return zdt.toInstant().getEpochSecond() * 1000; // always exact seconds
|
||||
}
|
||||
|
||||
@DataProvider(name = "testData")
|
||||
public Object[][] testData() {
|
||||
return new Object[][] {
|
||||
// Date string in past. But checking based on current time.
|
||||
{-1L, -1L, -1L, DT1, 0, true},
|
||||
{-1L, -1L, 1000, DT1, 1000, false},
|
||||
{-1L, -1L, 0, DT1, 0, true},
|
||||
{-1L, -1L, 1000, NOW_PLUS_500_SEC, 1000, false},
|
||||
|
||||
// Far in the future. Just check hasExpired() not the exact maxAge
|
||||
{-1L, -1L, -1L, FAR_FUTURE, -1L, false},
|
||||
|
||||
// Tests using fixed creation and verification dates
|
||||
// (independent of current time)
|
||||
// expires=
|
||||
// maxAge= |
|
||||
// create time expiry check time | |expected maxAge
|
||||
// | | | | | hasExpired()
|
||||
// | | | | | | expected
|
||||
{zdtToMillis(zdt1), zdtToMillis(zdt3), -1L, DT2, 60, true},
|
||||
{zdtToMillis(zdt1), zdtToMillis(zdt3), 20, DT2, 20, true},
|
||||
{zdtToMillis(zdt1), zdtToMillis(zdt2), 40, DT3, 40, true},
|
||||
{zdtToMillis(zdt1), zdtToMillis(zdt2), -1L, DT3,120, false}
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@Test(dataProvider = "testData")
|
||||
public void test1(long creationInstant, // if -1, then current time is used
|
||||
long expiryCheckInstant, // if -1 then current time is used
|
||||
long maxAge, // if -1, then not included in String
|
||||
String expires, // if null, then not included in String
|
||||
long expectedAge, // expected return value from getMaxAge()
|
||||
boolean hasExpired) // expected return value from hasExpired()
|
||||
throws Exception
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Set-Cookie: name=value");
|
||||
if (expires != null)
|
||||
sb.append("; expires=" + expires);
|
||||
if (maxAge != -1)
|
||||
sb.append("; max-age=" + Long.toString(maxAge));
|
||||
|
||||
String s = sb.toString();
|
||||
System.out.println(s);
|
||||
HttpCookie cookie;
|
||||
if (creationInstant == -1)
|
||||
cookie = HttpCookie.parse(s).get(0);
|
||||
else
|
||||
cookie = HttpCookie.parse(s, false, creationInstant).get(0);
|
||||
|
||||
if (expectedAge != -1 && cookie.getMaxAge() != expectedAge) {
|
||||
System.out.println("getMaxAge returned " + cookie.getMaxAge());
|
||||
System.out.println("expectedAge was " + expectedAge);
|
||||
throw new RuntimeException("Test failed: wrong age");
|
||||
}
|
||||
|
||||
boolean expired = expiryCheckInstant == -1
|
||||
? cookie.hasExpired()
|
||||
: cookie.hasExpired(expiryCheckInstant);
|
||||
|
||||
if (expired != hasExpired) {
|
||||
System.out.println("cookie.hasExpired() returned " + expired);
|
||||
System.out.println("hasExpired was " + hasExpired);
|
||||
System.out.println("getMaxAge() returned " + cookie.getMaxAge());
|
||||
throw new RuntimeException("Test failed: wrong hasExpired");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() {
|
||||
// Miscellaneous tests that setMaxAge() overrides whatever was set already
|
||||
HttpCookie cookie = HttpCookie.parse("Set-Cookie: name=value; max-age=100").get(0);
|
||||
Assert.assertEquals(cookie.getMaxAge(), 100);
|
||||
cookie.setMaxAge(200);
|
||||
Assert.assertEquals(cookie.getMaxAge(), 200);
|
||||
cookie.setMaxAge(-2);
|
||||
Assert.assertEquals(cookie.getMaxAge(), -2);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue