Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/HttpOnly.java
38867 views
1
/*
2
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
/**
24
* @test
25
* @bug 7095980 8007315
26
* @summary Ensure HttpURLConnection (and supporting APIs) don't expose
27
* HttpOnly cookies
28
*/
29
30
import java.io.IOException;
31
import java.net.CookieHandler;
32
import java.net.CookieManager;
33
import java.net.CookiePolicy;
34
import java.net.InetAddress;
35
import java.net.InetSocketAddress;
36
import java.net.URI;
37
import java.net.HttpURLConnection;
38
import java.util.ArrayList;
39
import java.util.HashMap;
40
import java.util.List;
41
import java.util.Map;
42
import java.util.Set;
43
import com.sun.net.httpserver.Headers;
44
import com.sun.net.httpserver.HttpExchange;
45
import com.sun.net.httpserver.HttpHandler;
46
import com.sun.net.httpserver.HttpServer;
47
48
/*
49
* 1) start the HTTP server
50
* 2) populate cookie store with HttpOnly cookies
51
* 3) make HTTP request that should contain HttpOnly cookies
52
* 4) check HttpOnly cookies received by server
53
* 5) server reply with Set-Cookie containing HttpOnly cookie
54
* 6) check HttpOnly cookies are not accessible from Http client
55
* 7) check that non-null (empty string) values are returned for
56
scenario where all values are stripped from original key values
57
*/
58
59
public class HttpOnly {
60
61
static final String URI_PATH = "/xxyyzz/";
62
static final int SESSION_ID = 12345;
63
64
void test(String[] args) throws Exception {
65
HttpServer server = startHttpServer();
66
CookieHandler previousHandler = CookieHandler.getDefault();
67
try {
68
InetSocketAddress address = server.getAddress();
69
URI uri = new URI("http://" + InetAddress.getLocalHost().getHostAddress()
70
+ ":" + address.getPort() + URI_PATH);
71
populateCookieStore(uri);
72
doClient(uri);
73
} finally {
74
CookieHandler.setDefault(previousHandler);
75
server.stop(0);
76
}
77
}
78
79
void populateCookieStore(URI uri)
80
throws IOException {
81
82
CookieManager cm = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
83
CookieHandler.setDefault(cm);
84
Map<String,List<String>> header = new HashMap<>();
85
List<String> values = new ArrayList<>();
86
values.add("JSESSIONID=" + SESSION_ID + "; version=1; Path="
87
+ URI_PATH +"; HttpOnly");
88
values.add("CUSTOMER=WILE_E_COYOTE; version=1; Path=" + URI_PATH);
89
header.put("Set-Cookie", values);
90
cm.put(uri, header);
91
}
92
93
void doClient(URI uri) throws Exception {
94
HttpURLConnection uc = (HttpURLConnection) uri.toURL().openConnection();
95
int resp = uc.getResponseCode();
96
check(resp == 200,
97
"Unexpected response code. Expected 200, got " + resp);
98
99
// TEST 1: check getRequestProperty doesn't return the HttpOnly cookie
100
// In fact, that it doesn't return any automatically set cookies.
101
String cookie = uc.getRequestProperty("Cookie");
102
check(cookie == null,
103
"Cookie header returned from getRequestProperty, value " + cookie);
104
105
// TEST 2: check getRequestProperties doesn't return the HttpOnly cookie.
106
// In fact, that it doesn't return any automatically set cookies.
107
Map<String,List<String>> reqHeaders = uc.getRequestProperties();
108
Set<Map.Entry<String,List<String>>> entries = reqHeaders.entrySet();
109
for (Map.Entry<String,List<String>> entry : entries) {
110
String header = entry.getKey();
111
check(!"Cookie".equalsIgnoreCase(header),
112
"Cookie header returned from getRequestProperties, value " +
113
entry.getValue());
114
}
115
116
// TEST 3: check getHeaderField doesn't return Set-Cookie with HttpOnly
117
String setCookie = uc.getHeaderField("Set-Cookie");
118
if (setCookie != null) {
119
debug("Set-Cookie:" + setCookie);
120
check(!setCookie.toLowerCase().contains("httponly"),
121
"getHeaderField returned Set-Cookie header with HttpOnly, " +
122
"value = " + setCookie);
123
}
124
125
// TEST 3.5: check getHeaderField doesn't return Set-Cookie2 with HttpOnly
126
String setCookie2 = uc.getHeaderField("Set-Cookie2");
127
if (setCookie2 != null) {
128
debug("Set-Cookie2:" + setCookie2);
129
check(!setCookie2.toLowerCase().contains("httponly"),
130
"getHeaderField returned Set-Cookie2 header with HttpOnly, " +
131
"value = " + setCookie2);
132
}
133
134
// TEST 4: check getHeaderFields doesn't return Set-Cookie
135
// or Set-Cookie2 headers with HttpOnly
136
Map<String,List<String>> respHeaders = uc.getHeaderFields();
137
Set<Map.Entry<String,List<String>>> respEntries = respHeaders.entrySet();
138
for (Map.Entry<String,List<String>> entry : respEntries) {
139
String header = entry.getKey();
140
if ("Set-Cookie".equalsIgnoreCase(header)) {
141
List<String> setCookieValues = entry.getValue();
142
debug("Set-Cookie:" + setCookieValues);
143
for (String value : setCookieValues)
144
check(!value.toLowerCase().contains("httponly"),
145
"getHeaderFields returned Set-Cookie header with HttpOnly, "
146
+ "value = " + value);
147
}
148
if ("Set-Cookie2".equalsIgnoreCase(header)) {
149
List<String> setCookieValues = entry.getValue();
150
debug("Set-Cookie2:" + setCookieValues);
151
for (String value : setCookieValues)
152
check(!value.toLowerCase().contains("httponly"),
153
"getHeaderFields returned Set-Cookie2 header with HttpOnly, "
154
+ "value = " + value);
155
}
156
}
157
158
// Now add some user set cookies into the mix.
159
uc = (HttpURLConnection) uri.toURL().openConnection();
160
uc.addRequestProperty("Cookie", "CUSTOMER_ID=CHEGAR;");
161
resp = uc.getResponseCode();
162
check(resp == 200,
163
"Unexpected response code. Expected 200, got " + resp);
164
165
// TEST 5: check getRequestProperty doesn't return the HttpOnly cookie
166
cookie = uc.getRequestProperty("Cookie");
167
check(!cookie.toLowerCase().contains("httponly"),
168
"HttpOnly cookie returned from getRequestProperty, value " + cookie);
169
170
// TEST 6: check getRequestProperties doesn't return the HttpOnly cookie.
171
reqHeaders = uc.getRequestProperties();
172
entries = reqHeaders.entrySet();
173
for (Map.Entry<String,List<String>> entry : entries) {
174
String header = entry.getKey();
175
if ("Cookie".equalsIgnoreCase(header)) {
176
for (String val : entry.getValue())
177
check(!val.toLowerCase().contains("httponly"),
178
"HttpOnly cookie returned from getRequestProperties," +
179
" value " + val);
180
}
181
}
182
183
// TEST 7 : check that header keys containing empty key values don't return null
184
int i = 1;
185
String key = "";
186
String value = "";
187
188
while (true) {
189
key = uc.getHeaderFieldKey(i);
190
value = uc.getHeaderField(i++);
191
if (key == null && value == null)
192
break;
193
194
if (key != null)
195
check(value != null,
196
"Encountered a null value for key value : " + key);
197
}
198
199
// TEST 7.5 similar test but use getHeaderFields
200
respHeaders = uc.getHeaderFields();
201
respEntries = respHeaders.entrySet();
202
for (Map.Entry<String,List<String>> entry : respEntries) {
203
String header = entry.getKey();
204
if (header != null) {
205
List<String> listValues = entry.getValue();
206
for (String value1 : listValues)
207
check(value1 != null,
208
"getHeaderFields returned null values for header:, "
209
+ header);
210
}
211
}
212
}
213
214
// HTTP Server
215
HttpServer startHttpServer() throws IOException {
216
HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
217
httpServer.createContext(URI_PATH, new SimpleHandler());
218
httpServer.start();
219
return httpServer;
220
}
221
222
class SimpleHandler implements HttpHandler {
223
@Override
224
public void handle(HttpExchange t) throws IOException {
225
Headers reqHeaders = t.getRequestHeaders();
226
227
// some small sanity check
228
List<String> cookies = reqHeaders.get("Cookie");
229
for (String cookie : cookies) {
230
if (!cookie.contains("JSESSIONID")
231
|| !cookie.contains("WILE_E_COYOTE"))
232
t.sendResponseHeaders(400, -1);
233
}
234
235
// return some cookies so we can check getHeaderField(s)
236
Headers respHeaders = t.getResponseHeaders();
237
List<String> values = new ArrayList<>();
238
values.add("ID=JOEBLOGGS; version=1; Path=" + URI_PATH);
239
values.add("NEW_JSESSIONID=" + (SESSION_ID+1) + "; version=1; Path="
240
+ URI_PATH +"; HttpOnly");
241
values.add("NEW_CUSTOMER=WILE_E_COYOTE2; version=1; Path=" + URI_PATH);
242
respHeaders.put("Set-Cookie", values);
243
values = new ArrayList<>();
244
values.add("COOKIE2_CUSTOMER=WILE_E_COYOTE2; version=1; Path="
245
+ URI_PATH);
246
respHeaders.put("Set-Cookie2", values);
247
values.add("COOKIE2_JSESSIONID=" + (SESSION_ID+100)
248
+ "; version=1; Path=" + URI_PATH +"; HttpOnly");
249
respHeaders.put("Set-Cookie2", values);
250
251
t.sendResponseHeaders(200, -1);
252
t.close();
253
}
254
}
255
256
volatile int passed = 0, failed = 0;
257
boolean debug = false;
258
void pass() {passed++;}
259
void fail() {failed++;}
260
void fail(String msg) {System.err.println(msg); fail();}
261
void unexpected(Throwable t) {failed++; t.printStackTrace();}
262
void debug(String message) { if (debug) System.out.println(message); }
263
void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
264
public static void main(String[] args) throws Throwable {
265
Class<?> k = new Object(){}.getClass().getEnclosingClass();
266
try {k.getMethod("instanceMain",String[].class)
267
.invoke( k.newInstance(), (Object) args);}
268
catch (Throwable e) {throw e.getCause();}}
269
public void instanceMain(String[] args) throws Throwable {
270
try {test(args);} catch (Throwable t) {unexpected(t);}
271
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
272
if (failed > 0) throw new AssertionError("Some tests failed");}
273
}
274
275
276