/* * 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 7095980 8007315 * @modules jdk.httpserver * @library /test/lib * @summary Ensure HttpURLConnection (and supporting APIs) don't expose * HttpOnly cookies * @run main HttpOnly * @run main/othervm -Djava.net.preferIPv6Addresses=true HttpOnly
*/
/* * 1) start the HTTP server * 2) populate cookie store with HttpOnly cookies * 3) make HTTP request that should contain HttpOnly cookies * 4) check HttpOnly cookies received by server * 5) server reply with Set-Cookie containing HttpOnly cookie * 6) check HttpOnly cookies are not accessible from Http client * 7) check that non-null (empty string) values are returned for scenario where all values are stripped from original key values
*/
// TEST 1: check getRequestProperty doesn't return the HttpOnly cookie // In fact, that it doesn't return any automatically set cookies.
String cookie = uc.getRequestProperty("Cookie");
check(cookie == null, "Cookie header returned from getRequestProperty, value " + cookie);
// TEST 2: check getRequestProperties doesn't return the HttpOnly cookie. // In fact, that it doesn't return any automatically set cookies.
Map<String,List<String>> reqHeaders = uc.getRequestProperties();
Set<Map.Entry<String,List<String>>> entries = reqHeaders.entrySet(); for (Map.Entry<String,List<String>> entry : entries) {
String header = entry.getKey();
check(!"Cookie".equalsIgnoreCase(header), "Cookie header returned from getRequestProperties, value " +
entry.getValue());
}
// TEST 3: check getHeaderField doesn't return Set-Cookie with HttpOnly
String setCookie = uc.getHeaderField("Set-Cookie"); if (setCookie != null) {
debug("Set-Cookie:" + setCookie);
check(!setCookie.toLowerCase().contains("httponly"), "getHeaderField returned Set-Cookie header with HttpOnly, " + "value = " + setCookie);
}
// TEST 3.5: check getHeaderField doesn't return Set-Cookie2 with HttpOnly
String setCookie2 = uc.getHeaderField("Set-Cookie2"); if (setCookie2 != null) {
debug("Set-Cookie2:" + setCookie2);
check(!setCookie2.toLowerCase().contains("httponly"), "getHeaderField returned Set-Cookie2 header with HttpOnly, " + "value = " + setCookie2);
}
// TEST 4: check getHeaderFields doesn't return Set-Cookie // or Set-Cookie2 headers with HttpOnly
Map<String,List<String>> respHeaders = uc.getHeaderFields();
Set<Map.Entry<String,List<String>>> respEntries = respHeaders.entrySet(); for (Map.Entry<String,List<String>> entry : respEntries) {
String header = entry.getKey(); if ("Set-Cookie".equalsIgnoreCase(header)) {
List<String> setCookieValues = entry.getValue();
debug("Set-Cookie:" + setCookieValues); for (String value : setCookieValues)
check(!value.toLowerCase().contains("httponly"), "getHeaderFields returned Set-Cookie header with HttpOnly, "
+ "value = " + value);
} if ("Set-Cookie2".equalsIgnoreCase(header)) {
List<String> setCookieValues = entry.getValue();
debug("Set-Cookie2:" + setCookieValues); for (String value : setCookieValues)
check(!value.toLowerCase().contains("httponly"), "getHeaderFields returned Set-Cookie2 header with HttpOnly, "
+ "value = " + value);
}
}
// Now add some user set cookies into the mix.
uc = (HttpURLConnection) uri.toURL().openConnection(Proxy.NO_PROXY);
uc.addRequestProperty("Cookie", "CUSTOMER_ID=CHEGAR;");
resp = uc.getResponseCode();
check(resp == 200, "Unexpected response code. Expected 200, got " + resp);
// TEST 5: check getRequestProperty doesn't return the HttpOnly cookie
cookie = uc.getRequestProperty("Cookie");
check(!cookie.toLowerCase().contains("httponly"), "HttpOnly cookie returned from getRequestProperty, value " + cookie);
// TEST 6: check getRequestProperties doesn't return the HttpOnly cookie.
reqHeaders = uc.getRequestProperties();
entries = reqHeaders.entrySet(); for (Map.Entry<String,List<String>> entry : entries) {
String header = entry.getKey(); if ("Cookie".equalsIgnoreCase(header)) { for (String val : entry.getValue())
check(!val.toLowerCase().contains("httponly"), "HttpOnly cookie returned from getRequestProperties," + " value " + val);
}
}
// TEST 7 : check that header keys containing empty key values don't return null int i = 1;
String key = "";
String value = "";
while (true) {
key = uc.getHeaderFieldKey(i);
value = uc.getHeaderField(i++); if (key == null && value == null) break;
if (key != null)
check(value != null, "Encountered a null value for key value : " + key);
}
// TEST 7.5 similar test but use getHeaderFields
respHeaders = uc.getHeaderFields();
respEntries = respHeaders.entrySet(); for (Map.Entry<String,List<String>> entry : respEntries) {
String header = entry.getKey(); if (header != null) {
List<String> listValues = entry.getValue(); for (String value1 : listValues)
check(value1 != null, "getHeaderFields returned null values for header:, "
+ header);
}
}
}
// some small sanity check
List<String> cookies = reqHeaders.get("Cookie"); for (String cookie : cookies) { if (!cookie.contains("JSESSIONID")
|| !cookie.contains("WILE_E_COYOTE"))
t.sendResponseHeaders(400, -1);
}
// return some cookies so we can check getHeaderField(s)
Headers respHeaders = t.getResponseHeaders();
List<String> values = new ArrayList<>();
values.add("ID=JOEBLOGGS; version=1; Path=" + URI_PATH);
values.add("NEW_JSESSIONID=" + (SESSION_ID+1) + "; version=1; Path="
+ URI_PATH +"; HttpOnly");
values.add("NEW_CUSTOMER=WILE_E_COYOTE2; version=1; Path=" + URI_PATH);
respHeaders.put("Set-Cookie", values);
values = new ArrayList<>();
values.add("COOKIE2_CUSTOMER=WILE_E_COYOTE2; version=1; Path="
+ URI_PATH);
respHeaders.put("Set-Cookie2", values);
values.add("COOKIE2_JSESSIONID=" + (SESSION_ID+100)
+ "; version=1; Path=" + URI_PATH +"; HttpOnly");
respHeaders.put("Set-Cookie2", values);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.