Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/HttpsProxyStackOverflow.java
41161 views
/*1* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 667086826* @summary StackOverFlow with bad authenticated Proxy tunnels27* @run main/othervm HttpsProxyStackOverflow28*29* No way to reserve default Authenticator, need to run in othervm mode.30*/3132import java.io.IOException;33import java.io.InputStream;34import java.net.Authenticator;35import java.net.Proxy;36import java.net.InetAddress;37import java.net.InetSocketAddress;38import java.net.PasswordAuthentication;39import java.net.ServerSocket;40import java.net.Socket;41import java.net.URL;42import javax.net.ssl.HttpsURLConnection;4344public class HttpsProxyStackOverflow {4546public static void main(String[] args) throws IOException {47BadAuthProxyServer server = startServer();48doClient(server);49}5051static void doClient(BadAuthProxyServer server) throws IOException {52// url doesn't matter since we will never make the connection53URL url = new URL("https://anythingwilldo/");54InetAddress loopback = InetAddress.getLoopbackAddress();55String loopbackAddress = loopback.getHostAddress();56HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(57new Proxy(Proxy.Type.HTTP,58InetSocketAddress.createUnresolved(loopbackAddress, server.getPort())));59try (InputStream is = conn.getInputStream()) {60} catch(IOException unused) {61// no real server, IOException is expected.62// failure if StackOverflowError63} finally {64server.done();65}66}6768static BadAuthProxyServer startServer() throws IOException {69Authenticator.setDefault(new Authenticator() {70@Override71protected PasswordAuthentication getPasswordAuthentication() {72return new PasswordAuthentication("xyz", "xyz".toCharArray());73}74});75InetAddress loopback = InetAddress.getLoopbackAddress();76InetSocketAddress address = new InetSocketAddress(loopback, 0);77ServerSocket ss = new ServerSocket();78ss.bind(address);79BadAuthProxyServer server = new BadAuthProxyServer(ss);80Thread serverThread = new Thread(server);81serverThread.start();82return server;83}8485static class BadAuthProxyServer implements Runnable {86private ServerSocket ss;87private boolean done;8889BadAuthProxyServer(ServerSocket ss) { this.ss = ss; }9091public void run() {92try {93while (!done) {94Socket s = ss.accept();95s.getOutputStream().write(96("HTTP/1.1 407\nProxy-Authenticate:Basic " +97"realm=\"WallyWorld\"\n\n").getBytes("US-ASCII"));9899s.close();100101s = ss.accept();102s.close();103}104} catch (IOException e) {105// Ignore IOException when the main thread calls done106} finally {107try { ss.close(); } catch (IOException e) {}108}109}110111int getPort() {112return ss.getLocalPort();113}114115void done() {116try { ss.close(); } catch (IOException e) {}117done = true;118}119}120}121122123