]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9336 Fix infinite proxy authentication loop
authorJulien HENRY <julien.henry@sonarsource.com>
Fri, 30 Jun 2017 13:39:20 +0000 (15:39 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 3 Jul 2017 06:53:38 +0000 (08:53 +0200)
sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpClientBuilder.java
sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java

index 77ad1552be4b2a593271dbe1b0deaf11ab3270f7..a9d4985fff5b7ed2ad311838d29b1f90659a5c40 100644 (file)
@@ -59,6 +59,7 @@ public class OkHttpClientBuilder {
 
   private static final String NONE = "NONE";
   private static final String P11KEYSTORE = "PKCS11";
+  private static final String PROXY_AUTHORIZATION = "Proxy-Authorization";
 
   private String userAgent;
   private Proxy proxy;
@@ -160,9 +161,13 @@ public class OkHttpClientBuilder {
     builder.addNetworkInterceptor(this::addUserAgent);
     if (proxyLogin != null) {
       builder.proxyAuthenticator((route, response) -> {
+        if (response.request().header(PROXY_AUTHORIZATION) != null) {
+          // Give up, we've already attempted to authenticate.
+          return null;
+        }
         if (HttpURLConnection.HTTP_PROXY_AUTH == response.code()) {
           String credential = Credentials.basic(proxyLogin, nullToEmpty(proxyPassword));
-          return response.request().newBuilder().header("Proxy-Authorization", credential).build();
+          return response.request().newBuilder().header(PROXY_AUTHORIZATION, credential).build();
         }
         return null;
       });
index ba6e9d4dc43d70a0aed967d0ea21ad366f6ca9b2..449b4fcebe8766c1fac1747420f8396611d80fa4 100644 (file)
@@ -31,6 +31,7 @@ import okhttp3.mockwebserver.RecordedRequest;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -62,6 +63,11 @@ public class HttpConnectorTest {
     serverUrl = server.url("").url().toString();
   }
 
+  @After
+  public void stop() throws Exception {
+    server.close();
+  }
+
   @Test
   public void test_default_settings() throws Exception {
     answerHelloWorld();
@@ -140,23 +146,50 @@ public class HttpConnectorTest {
 
   @Test
   public void use_proxy_authentication() throws Exception {
-    MockWebServer proxy = new MockWebServer();
-    proxy.start();
-    underTest = HttpConnector.newBuilder()
-      .url(serverUrl)
-      .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.getHostName(), proxy.getPort())))
-      .proxyCredentials("theProxyLogin", "theProxyPassword")
-      .build();
-
-    GetRequest request = new GetRequest("api/issues/search");
-    proxy.enqueue(new MockResponse().setResponseCode(407));
-    proxy.enqueue(new MockResponse().setBody("OK!"));
-    underTest.call(request);
+    try (MockWebServer proxy = new MockWebServer()) {
+      proxy.start();
+
+      underTest = HttpConnector.newBuilder()
+        .url(serverUrl)
+        .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.getHostName(), proxy.getPort())))
+        .proxyCredentials("theProxyLogin", "theProxyPassword")
+        .build();
+
+      GetRequest request = new GetRequest("api/issues/search");
+      proxy.enqueue(new MockResponse().setResponseCode(407));
+      proxy.enqueue(new MockResponse().setBody("OK!"));
+      underTest.call(request);
+
+      RecordedRequest recordedRequest = proxy.takeRequest();
+      assertThat(recordedRequest.getHeader("Proxy-Authorization")).isNull();
+      recordedRequest = proxy.takeRequest();
+      assertThat(recordedRequest.getHeader("Proxy-Authorization")).isEqualTo(basic("theProxyLogin", "theProxyPassword"));
+    }
+  }
 
-    RecordedRequest recordedRequest = proxy.takeRequest();
-    assertThat(recordedRequest.getHeader("Proxy-Authorization")).isNull();
-    recordedRequest = proxy.takeRequest();
-    assertThat(recordedRequest.getHeader("Proxy-Authorization")).isEqualTo(basic("theProxyLogin", "theProxyPassword"));
+  @Test
+  public void use_proxy_authentication_wrong_crendentials() throws Exception {
+    try (MockWebServer proxy = new MockWebServer()) {
+      proxy.start();
+
+      underTest = HttpConnector.newBuilder()
+        .url(serverUrl)
+        .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.getHostName(), proxy.getPort())))
+        .proxyCredentials("theProxyLogin", "wrongPassword")
+        .build();
+
+      GetRequest request = new GetRequest("api/issues/search");
+      proxy.enqueue(new MockResponse().setResponseCode(407));
+      proxy.enqueue(new MockResponse().setResponseCode(407));
+      proxy.enqueue(new MockResponse().setResponseCode(407));
+      underTest.call(request);
+
+      RecordedRequest recordedRequest = proxy.takeRequest();
+      assertThat(recordedRequest.getHeader("Proxy-Authorization")).isNull();
+      recordedRequest = proxy.takeRequest();
+      assertThat(recordedRequest.getHeader("Proxy-Authorization")).isEqualTo(basic("theProxyLogin", "wrongPassword"));
+      assertThat(proxy.getRequestCount()).isEqualTo(2);
+    }
   }
 
   @Test