jdk/test/sun/net/www/protocol/http/B5017051.java
author chegar
Fri, 16 Dec 2011 16:09:41 +0000
changeset 11284 2750cfd2352c
parent 5506 202f599c92aa
child 38557 5c485e1ea6fa
permissions -rw-r--r--
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies Reviewed-by: michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 5017051 6360774
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @run main/othervm B5017051
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Tests CR 5017051 & 6360774
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.concurrent.Executors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.concurrent.ExecutorService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Part 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *  First request sent to the http server will not have an "Authorization" header set and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *  the server will respond with a 401, but not until it has set a cookie in the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *  headers. The subsequent request ( comes from HttpURLConnection's authentication retry )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *  will have the appropriate Authorization header and the servers context handler will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *  invoked. The test passes only if the client (HttpURLConnection) has sent the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *  in its second request that had been set via the first response from the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Part 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *  Preload the CookieManager with a cookie. Make a http request that requires authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *  The cookie will be sent in the first request (without the Authorization header), the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *  server will respond with a 401 (from MyBasicAuthFilter) and the client will add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *  appropriate Authorization header. This tests ensures that there is only one Cookie header
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *  in the request that actually makes it to the Http servers context handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class B5017051
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    com.sun.net.httpserver.HttpServer httpServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    ExecutorService executorService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static void main(String[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        new B5017051();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public B5017051()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            startHttpServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            doClient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            System.err.println(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    void doClient() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        java.net.Authenticator.setDefault(new MyAuthenticator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            InetSocketAddress address = httpServer.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            // Part 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            HttpURLConnection uc = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            int resp = uc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (resp != 200)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                throw new RuntimeException("Failed: Part 1, Response code is not 200");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            System.out.println("Response code from Part 1 = 200 OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            // Part 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            URL url2 = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test2/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            // can use the global CookieHandler used for the first test as the URL's are different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            CookieHandler ch = CookieHandler.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            Map<String,List<String>> header = new HashMap<String,List<String>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            List<String> values = new LinkedList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            values.add("Test2Cookie=\"TEST2\"; path=\"/test2/\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            header.put("Set-Cookie2", values);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            // preload the CookieHandler with a cookie for our URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            // so that it will be sent during the first request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            ch.put(url2.toURI(), header);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            uc = (HttpURLConnection)url2.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            resp = uc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (resp != 200)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                throw new RuntimeException("Failed: Part 2, Response code is not 200");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            System.out.println("Response code from Part 2 = 200 OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        } catch (URISyntaxException ue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            ue.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            httpServer.stop(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            executorService.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Http Server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public void startHttpServer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // create HttpServer context for Part 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        ctx.setAuthenticator( new MyBasicAuthenticator("foo"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // CookieFilter needs to be executed before Authenticator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        ctx.getFilters().add(0, new CookieFilter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // create HttpServer context for Part 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        HttpContext ctx2 = httpServer.createContext("/test2/", new MyHandler2());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        ctx2.setAuthenticator( new MyBasicAuthenticator("foobar"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        executorService = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        httpServer.setExecutor(executorService);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        httpServer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    class MyHandler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        public void handle(HttpExchange t) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            InputStream is = t.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            Headers reqHeaders = t.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            Headers resHeaders = t.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            while (is.read () != -1) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (!reqHeaders.containsKey("Authorization"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                t.sendResponseHeaders(400, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            List<String> cookies = reqHeaders.get("Cookie");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (cookies != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                for (String str : cookies) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    if (str.equals("Customer=WILE_E_COYOTE"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        t.sendResponseHeaders(200, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            t.sendResponseHeaders(400, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    class MyHandler2 implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        public void handle(HttpExchange t) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            InputStream is = t.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            Headers reqHeaders = t.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            Headers resHeaders = t.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            while (is.read () != -1) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (!reqHeaders.containsKey("Authorization"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                t.sendResponseHeaders(400, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            List<String> cookies = reqHeaders.get("Cookie");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // there should only be one Cookie header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (cookies != null && (cookies.size() == 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                t.sendResponseHeaders(200, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            t.sendResponseHeaders(400, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    class MyAuthenticator extends java.net.Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        public PasswordAuthentication getPasswordAuthentication () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return new PasswordAuthentication("tester", "passwd".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    class MyBasicAuthenticator extends BasicAuthenticator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        public MyBasicAuthenticator(String realm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            super(realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        public boolean checkCredentials (String username, String password) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return username.equals("tester") && password.equals("passwd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    class CookieFilter extends Filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        public void doFilter(HttpExchange t, Chain chain) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            Headers resHeaders = t.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            Headers reqHeaders = t.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (!reqHeaders.containsKey("Authorization"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                resHeaders.set("Set-Cookie2", "Customer=\"WILE_E_COYOTE\"; path=\"/test/\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            chain.doFilter(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        public void destroy(HttpContext c) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        public void init(HttpContext c) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        public String description() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return new String("Filter for setting a cookie for requests without an \"Authorization\" header.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
}