diff options
author | Wouter Admiraal <45544358+wouter-admiraal-sonarsource@users.noreply.github.com> | 2021-04-20 16:39:10 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-04-20 20:03:47 +0000 |
commit | 9c6b2db886aaa6943618573c1290aa6e422a4120 (patch) | |
tree | 3d93b241f177278dc0f51bd5967a2e07e97e5ff0 /sonar-ws/src | |
parent | 43ab5b022f80236d76d05cba6e1c3d57f41daff3 (diff) | |
download | sonarqube-9c6b2db886aaa6943618573c1290aa6e422a4120.tar.gz sonarqube-9c6b2db886aaa6943618573c1290aa6e422a4120.zip |
SONAR-14213 Do not follow redirects when interacting with gitlab.com API
Diffstat (limited to 'sonar-ws/src')
-rw-r--r-- | sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpClientBuilder.java | 14 | ||||
-rw-r--r-- | sonar-ws/src/test/java/org/sonarqube/ws/client/OkHttpClientBuilderTest.java | 19 |
2 files changed, 33 insertions, 0 deletions
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpClientBuilder.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpClientBuilder.java index ede86381037..aa3a736eb4c 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpClientBuilder.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpClientBuilder.java @@ -67,6 +67,7 @@ public class OkHttpClientBuilder { private String credentials; private String proxyLogin; private String proxyPassword; + private Boolean followRedirects; private long connectTimeoutMs = -1; private long readTimeoutMs = -1; private SSLSocketFactory sslSocketFactory = null; @@ -159,6 +160,15 @@ public class OkHttpClientBuilder { return this; } + /** + * Set if redirects should be followed or not. + * Default is defined by OkHttp (true, follow redirects). + */ + public OkHttpClientBuilder setFollowRedirects(Boolean followRedirects) { + this.followRedirects = followRedirects; + return this; + } + public OkHttpClient build() { OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.proxy(proxy); @@ -182,6 +192,10 @@ public class OkHttpClientBuilder { return null; }); } + if (followRedirects != null) { + builder.followRedirects(followRedirects); + builder.followSslRedirects(followRedirects); + } ConnectionSpec tls = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .allEnabledTlsVersions() diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/OkHttpClientBuilderTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/OkHttpClientBuilderTest.java index 26c66de6af5..047cfbfc205 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/OkHttpClientBuilderTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/OkHttpClientBuilderTest.java @@ -42,6 +42,8 @@ public class OkHttpClientBuilderTest { assertThat(okHttpClient.proxy()).isNull(); assertThat(okHttpClient.networkInterceptors()).hasSize(1); assertThat(okHttpClient.sslSocketFactory()).isNotNull(); + assertThat(okHttpClient.followRedirects()).isTrue(); + assertThat(okHttpClient.followSslRedirects()).isTrue(); } @Test @@ -55,6 +57,23 @@ public class OkHttpClientBuilderTest { } @Test + public void build_follow_redirects() { + OkHttpClient okHttpClientWithRedirect = underTest + .setFollowRedirects(true) + .build(); + + assertThat(okHttpClientWithRedirect.followRedirects()).isTrue(); + assertThat(okHttpClientWithRedirect.followSslRedirects()).isTrue(); + + OkHttpClient okHttpClientWithoutRedirect = underTest + .setFollowRedirects(false) + .build(); + + assertThat(okHttpClientWithoutRedirect.followRedirects()).isFalse(); + assertThat(okHttpClientWithoutRedirect.followSslRedirects()).isFalse(); + } + + @Test public void build_throws_IAE_if_connect_timeout_is_negative() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Connect timeout must be positive. Got -10"); |