File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed
main/java/org/opengrok/web/api/v1/filter
test/java/org/opengrok/web/api/v1/filter Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -110,6 +110,13 @@ public void filter(final ContainerRequestContext context) {
110
110
return ;
111
111
}
112
112
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
+
113
120
if (localAddresses .contains (request .getRemoteAddr ())) {
114
121
logger .log (Level .FINEST , "allowing request to {0} based on localhost IP address" , path );
115
122
return ;
Original file line number Diff line number Diff line change @@ -79,6 +79,22 @@ private void nonLocalhostTestWithToken(boolean allowed) throws Exception {
79
79
}
80
80
}
81
81
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
+
82
98
@ Test
83
99
public void nonLocalhostTestWithoutToken () throws Exception {
84
100
IncomingFilter filter = mockWithRemoteAddress ("192.168.1.1" );
@@ -166,5 +182,4 @@ public void searchTest() throws Exception {
166
182
167
183
verify (context , never ()).abortWith (captor .capture ());
168
184
}
169
-
170
185
}
You can’t perform that action at this time.
0 commit comments