Merge "Honor X-FORWARDED-HOST when negociating server name"
diff --git a/src/main/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostFilter.java b/src/main/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostFilter.java
index 99eaa19..edf3491 100644
--- a/src/main/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostFilter.java
+++ b/src/main/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostFilter.java
@@ -15,6 +15,8 @@
package com.gerritforge.gerrit.modules.virtualhost;
import java.io.IOException;
+import java.util.Optional;
+
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
@@ -23,7 +25,10 @@
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
+import com.google.common.annotations.VisibleForTesting;
+
public class VirtualHostFilter implements Filter {
+ static final String FORWARDED_HOST_HEADER = "X-FORWARDED-HOST";
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@@ -31,10 +36,17 @@
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
- CurrentServerName.set(((HttpServletRequest) request).getServerName());
+ CurrentServerName.set(getServerNameFromRequest((HttpServletRequest) request));
+
chain.doFilter(request, response);
}
@Override
public void destroy() {}
+
+ @VisibleForTesting
+ static String getServerNameFromRequest(HttpServletRequest httpRequest) {
+ return Optional.ofNullable(httpRequest.getHeader(FORWARDED_HOST_HEADER))
+ .orElseGet(httpRequest::getServerName);
+ }
}
diff --git a/src/test/java/com/gerritforge/gerrit/modules/virtualhost/VirutalHostFilterTest.java b/src/test/java/com/gerritforge/gerrit/modules/virtualhost/VirutalHostFilterTest.java
new file mode 100644
index 0000000..7e6eeaf
--- /dev/null
+++ b/src/test/java/com/gerritforge/gerrit/modules/virtualhost/VirutalHostFilterTest.java
@@ -0,0 +1,64 @@
+// Copyright (C) 2023 GerritForge Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.gerritforge.gerrit.modules.virtualhost;
+
+import static com.gerritforge.gerrit.modules.virtualhost.VirtualHostFilter.FORWARDED_HOST_HEADER;
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.junit.Test;
+
+import com.google.gerrit.common.Nullable;
+
+public class VirutalHostFilterTest {
+ private static final String DOM0_HOST = "review.gerrithub.io";
+ private static final String FORWARDED_HOST = "eclipse.gerrithub.io";
+ private static final String CLOUD_HOST = "cloud-99.provider.com";
+
+ @Test
+ public void getServerNameFromForwardedHost() {
+ HttpServletRequest requestMock = mockHttpServletRequest(CLOUD_HOST, FORWARDED_HOST);
+
+ String actual = VirtualHostFilter.getServerNameFromRequest(requestMock);
+
+ assertThat(actual).isEqualTo(FORWARDED_HOST);
+ }
+
+ @Test
+ public void getServerNameFromRequest() {
+ HttpServletRequest requestMock = mockHttpServletRequest(DOM0_HOST);
+
+ String actual = VirtualHostFilter.getServerNameFromRequest(requestMock);
+
+ assertThat(actual).isEqualTo(DOM0_HOST);
+ }
+
+ private HttpServletRequest mockHttpServletRequest(String serverName) {
+ return mockHttpServletRequest(serverName, null);
+ }
+
+ private HttpServletRequest mockHttpServletRequest(
+ String serverName, @Nullable String forwardedHost) {
+ HttpServletRequest mock = mock(HttpServletRequest.class);
+
+ when(mock.getHeader(FORWARDED_HOST_HEADER)).thenReturn(forwardedHost);
+ when(mock.getServerName()).thenReturn(serverName);
+
+ return mock;
+ }
+}