Skip to content

Commit 28fc16f

Browse files
author
Vladimir Kotal
committed
deny proxied connections
1 parent 3344366 commit 28fc16f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

opengrok-web/src/main/java/org/opengrok/web/api/v1/filter/IncomingFilter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ public void filter(final ContainerRequestContext context) {
110110
return;
111111
}
112112

113+
if (request.getHeader("X-Forwarded-For") != null || request.getHeader("Forwarded") != null) {
114+
logger.log(Level.FINEST, "denying request to {0} due to existence of forwarded header in the request",
115+
path);
116+
context.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
117+
return;
118+
}
119+
113120
if (localAddresses.contains(request.getRemoteAddr())) {
114121
logger.log(Level.FINEST, "allowing request to {0} based on localhost IP address", path);
115122
return;

opengrok-web/src/test/java/org/opengrok/web/api/v1/filter/IncomingFilterTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ private void nonLocalhostTestWithToken(boolean allowed) throws Exception {
7979
}
8080
}
8181

82+
@Test
83+
public void localhostTestWithForwardedHeader() throws Exception {
84+
Map<String, String> headers = new TreeMap<>();
85+
headers.put("X-Forwarded-For", "192.0.2.43, 2001:db8:cafe::17");
86+
IncomingFilter filter = mockWithRemoteAddress("127.0.0.1", headers, true);
87+
88+
ContainerRequestContext context = mockContainerRequestContext("test");
89+
90+
ArgumentCaptor<Response> captor = ArgumentCaptor.forClass(Response.class);
91+
92+
filter.filter(context);
93+
94+
verify(context).abortWith(captor.capture());
95+
assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), captor.getValue().getStatus());
96+
}
97+
8298
@Test
8399
public void nonLocalhostTestWithoutToken() throws Exception {
84100
IncomingFilter filter = mockWithRemoteAddress("192.168.1.1");
@@ -166,5 +182,4 @@ public void searchTest() throws Exception {
166182

167183
verify(context, never()).abortWith(captor.capture());
168184
}
169-
170185
}

0 commit comments

Comments
 (0)