Skip to content

Commit

Permalink
feat: Add support for X-Forwarded-Prefix
Browse files Browse the repository at this point in the history
Change-Id: I200d5e1567118a026ac951389e616358cf81bba9
Signed-off-by: Florent Benoit <[email protected]>
  • Loading branch information
benoitf authored and spoenemann committed Feb 17, 2022
1 parent b8b65e4 commit ea2cde9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
12 changes: 11 additions & 1 deletion server/src/main/java/org/eclipse/openvsx/util/UrlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static String getBaseUrl() {
return getBaseUrl(requestAttrs.getRequest());
}

private static String getBaseUrl(HttpServletRequest request) {
protected static String getBaseUrl(HttpServletRequest request) {
var url = new StringBuilder();

// Use the scheme from the X-Forwarded-Proto header if present
Expand Down Expand Up @@ -132,6 +132,16 @@ private static String getBaseUrl(HttpServletRequest request) {
break;
}

// Use the prefix from the X-Forwarded-Prefix header if present
String prefix;
var forwardedPrefix = request.getHeader("X-Forwarded-Prefix");
if (forwardedPrefix == null) {
prefix = "";
} else {
prefix = forwardedPrefix;
}
url.append(prefix);

url.append(request.getContextPath());
return url.toString();
}
Expand Down
50 changes: 49 additions & 1 deletion server/src/test/java/org/eclipse/openvsx/util/UriUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,33 @@
package org.eclipse.openvsx.util;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;

import javax.servlet.http.HttpServletRequest;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

public class UriUtilTest {

@Mock
private HttpServletRequest request;

private AutoCloseable closeable;

@BeforeEach
public void openMocks() {
closeable = MockitoAnnotations.openMocks(this);
}

@AfterEach
public void releaseMocks() throws Exception {
closeable.close();
}

@Test
public void testApiUrl() throws Exception {
var baseUrl = "http://localhost/";
Expand All @@ -29,4 +51,30 @@ public void testQuery() throws Exception {
.isEqualTo("http://localhost/api/foo?a=1&c=b%09a%2Fr");
}

}
// Check base URL is localhost:8080 if there is no XForwarded headers
@Test
public void testWithoutXForwarded() throws Exception {
doReturn("http").when(request).getScheme();
doReturn("localhost").when(request).getServerName();
doReturn(8080).when(request).getServerPort();
doReturn("/").when(request).getContextPath();
assertThat(UrlUtil.getBaseUrl(request)).isEqualTo("http://localhost:8080/");
}

// Check base URL is using XForwarded headers
@Test
public void testWithXForwarded() throws Exception {
// basic request
doReturn("http").when(request).getScheme();
doReturn("localhost").when(request).getServerName();
doReturn(8080).when(request).getServerPort();
doReturn("/").when(request).getContextPath();

// XForwarded content
doReturn("https").when(request).getHeader("X-Forwarded-Proto");
doReturn("open-vsx.org").when(request).getHeader("X-Forwarded-Host");
doReturn("/openvsx").when(request).getHeader("X-Forwarded-Prefix");
assertThat(UrlUtil.getBaseUrl(request)).isEqualTo("https://open-vsx.org/openvsx/");
}

}

0 comments on commit ea2cde9

Please sign in to comment.