From b7d1eb4185aa35f037790580b60c1f2d76968fa8 Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Wed, 31 Jul 2019 19:40:40 +0200 Subject: [PATCH] HttpSupport.proxyFor(): use only scheme, host, and port Some URLs cannot be converted via URL.toURI(). So don't convert the full URL but only the bits that are needed to find a proxy via java.net.ProxySelector. Bug: 549690 Change-Id: I55b5ecee70c6b52f72f9bdba9ce552fde7f33976 Signed-off-by: Thomas Wolf --- .../eclipse/jgit/util/HttpSupportTest.java | 69 +++++++++++++++++++ .../org/eclipse/jgit/util/HttpSupport.java | 5 +- 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java new file mode 100644 index 0000000000..cbe4eb2eb0 --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2019, Thomas Wolf and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.net.ProxySelector; +import java.net.SocketAddress; +import java.net.URI; +import java.net.URL; +import java.util.Collections; +import java.util.List; + +import org.junit.Test; + +public class HttpSupportTest { + + private static class TestProxySelector extends ProxySelector { + + private static final Proxy DUMMY = new Proxy(Proxy.Type.HTTP, + InetSocketAddress.createUnresolved("localhost", 0)); + + @Override + public List select(URI uri) { + if ("http".equals(uri.getScheme()) + && "somehost".equals(uri.getHost())) { + return Collections.singletonList(DUMMY); + } + return Collections.singletonList(Proxy.NO_PROXY); + } + + @Override + public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { + // Empty + } + } + + @Test + public void testMalformedUri() throws Exception { + // Valid URL, but backslash is not allowed in a URI in the userinfo part + // per RFC 3986: https://tools.ietf.org/html/rfc3986#section-3.2.1 . + // Test that conversion to URI to call the ProxySelector does not throw + // an exception. + Proxy proxy = HttpSupport.proxyFor(new TestProxySelector(), new URL( + "http://infor\\c.jones@somehost/somewhere/someproject.git")); + assertNotNull(proxy); + assertEquals(Proxy.Type.HTTP, proxy.type()); + } + + @Test + public void testCorrectUri() throws Exception { + // Backslash escaped as %5C is correct. + Proxy proxy = HttpSupport.proxyFor(new TestProxySelector(), new URL( + "http://infor%5Cc.jones@somehost/somewhere/someproject.git")); + assertNotNull(proxy); + assertEquals(Proxy.Type.HTTP, proxy.type()); + } +} diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/HttpSupport.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/HttpSupport.java index 640670debc..d897255062 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/HttpSupport.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/HttpSupport.java @@ -51,6 +51,7 @@ import java.io.UnsupportedEncodingException; import java.net.ConnectException; import java.net.Proxy; import java.net.ProxySelector; +import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.net.URLEncoder; @@ -299,7 +300,9 @@ public class HttpSupport { public static Proxy proxyFor(ProxySelector proxySelector, URL u) throws ConnectException { try { - return proxySelector.select(u.toURI()).get(0); + URI uri = new URI(u.getProtocol(), null, u.getHost(), u.getPort(), + null, null, null); + return proxySelector.select(uri).get(0); } catch (URISyntaxException e) { final ConnectException err; err = new ConnectException(MessageFormat.format(JGitText.get().cannotDetermineProxyFor, u)); -- 2.39.5