|
17 | 17 |
|
18 | 18 | import java.io.IOException;
|
19 | 19 | import java.util.Enumeration;
|
| 20 | + |
| 21 | +import javax.servlet.Filter; |
| 22 | +import javax.servlet.FilterChain; |
20 | 23 | import javax.servlet.ServletException;
|
21 | 24 | import javax.servlet.http.HttpServlet;
|
22 | 25 | import javax.servlet.http.HttpServletRequest;
|
| 26 | +import javax.servlet.http.HttpServletResponse; |
23 | 27 |
|
24 | 28 | import org.junit.Before;
|
25 | 29 | import org.junit.Test;
|
|
37 | 41 | * Unit tests for {@link ForwardedHeaderFilter}.
|
38 | 42 | * @author Rossen Stoyanchev
|
39 | 43 | * @author Eddú Meléndez
|
| 44 | + * @author Rob Winch |
40 | 45 | */
|
41 | 46 | public class ForwardedHeaderFilterTests {
|
42 | 47 |
|
@@ -222,6 +227,157 @@ public void contextPathWithForwardedPrefixTrailingSlash() throws Exception {
|
222 | 227 | assertEquals("/prefix", actual);
|
223 | 228 | }
|
224 | 229 |
|
| 230 | + @Test |
| 231 | + public void sendRedirectWithAbsolutePath() throws Exception { |
| 232 | + this.request.addHeader(X_FORWARDED_PROTO, "https"); |
| 233 | + this.request.addHeader(X_FORWARDED_HOST, "example.com"); |
| 234 | + this.request.addHeader(X_FORWARDED_PORT, "443"); |
| 235 | + |
| 236 | + String redirectedUrl = sendRedirect("/foo/bar"); |
| 237 | + assertEquals("https://example.com/foo/bar", redirectedUrl); |
| 238 | + } |
| 239 | + |
| 240 | + @Test |
| 241 | + public void sendRedirectWithContextPath() throws Exception { |
| 242 | + this.request.addHeader(X_FORWARDED_PROTO, "https"); |
| 243 | + this.request.addHeader(X_FORWARDED_HOST, "example.com"); |
| 244 | + this.request.addHeader(X_FORWARDED_PORT, "443"); |
| 245 | + this.request.setContextPath("/context"); |
| 246 | + |
| 247 | + String redirectedUrl = sendRedirect("/foo/bar"); |
| 248 | + assertEquals("https://example.com/context/foo/bar", redirectedUrl); |
| 249 | + } |
| 250 | + |
| 251 | + @Test |
| 252 | + public void sendRedirectWithXForwardedPrefix() throws Exception { |
| 253 | + this.request.addHeader(X_FORWARDED_PROTO, "https"); |
| 254 | + this.request.addHeader(X_FORWARDED_HOST, "example.com"); |
| 255 | + this.request.addHeader(X_FORWARDED_PORT, "443"); |
| 256 | + this.request.addHeader(X_FORWARDED_PREFIX, "/prefix"); |
| 257 | + |
| 258 | + String redirectedUrl = sendRedirect("/foo/bar"); |
| 259 | + assertEquals("https://example.com/prefix/foo/bar", redirectedUrl); |
| 260 | + } |
| 261 | + |
| 262 | + @Test |
| 263 | + public void sendRedirectWithXForwardedPrefixAndContextPath() throws Exception { |
| 264 | + this.request.addHeader(X_FORWARDED_PROTO, "https"); |
| 265 | + this.request.addHeader(X_FORWARDED_HOST, "example.com"); |
| 266 | + this.request.addHeader(X_FORWARDED_PORT, "443"); |
| 267 | + this.request.addHeader(X_FORWARDED_PREFIX, "/prefix"); |
| 268 | + this.request.setContextPath("/context"); |
| 269 | + |
| 270 | + String redirectedUrl = sendRedirect("/foo/bar"); |
| 271 | + assertEquals("https://example.com/prefix/foo/bar", redirectedUrl); |
| 272 | + } |
| 273 | + |
| 274 | + @Test |
| 275 | + public void sendRedirectWithRelativePath() throws Exception { |
| 276 | + this.request.addHeader(X_FORWARDED_PROTO, "https"); |
| 277 | + this.request.addHeader(X_FORWARDED_HOST, "example.com"); |
| 278 | + this.request.addHeader(X_FORWARDED_PORT, "443"); |
| 279 | + this.request.setRequestURI("/parent/"); |
| 280 | + |
| 281 | + String redirectedUrl = sendRedirect("foo/bar"); |
| 282 | + assertEquals("https://example.com/parent/foo/bar", redirectedUrl); |
| 283 | + } |
| 284 | + |
| 285 | + @Test |
| 286 | + public void sendRedirectWithFileInPathAndRelativeRedirect() throws Exception { |
| 287 | + this.request.addHeader(X_FORWARDED_PROTO, "https"); |
| 288 | + this.request.addHeader(X_FORWARDED_HOST, "example.com"); |
| 289 | + this.request.addHeader(X_FORWARDED_PORT, "443"); |
| 290 | + this.request.setRequestURI("/context/a"); |
| 291 | + |
| 292 | + String redirectedUrl = sendRedirect("foo/bar"); |
| 293 | + assertEquals("https://example.com/context/foo/bar", redirectedUrl); |
| 294 | + } |
| 295 | + |
| 296 | + @Test |
| 297 | + public void sendRedirectWithRelativePathIgnoresFile() throws Exception { |
| 298 | + this.request.addHeader(X_FORWARDED_PROTO, "https"); |
| 299 | + this.request.addHeader(X_FORWARDED_HOST, "example.com"); |
| 300 | + this.request.addHeader(X_FORWARDED_PORT, "443"); |
| 301 | + this.request.setRequestURI("/parent"); |
| 302 | + |
| 303 | + String redirectedUrl = sendRedirect("foo/bar"); |
| 304 | + assertEquals("https://example.com/foo/bar", redirectedUrl); |
| 305 | + } |
| 306 | + |
| 307 | + @Test |
| 308 | + public void sendRedirectWithLocationDotDotPath() throws Exception { |
| 309 | + this.request.addHeader(X_FORWARDED_PROTO, "https"); |
| 310 | + this.request.addHeader(X_FORWARDED_HOST, "example.com"); |
| 311 | + this.request.addHeader(X_FORWARDED_PORT, "443"); |
| 312 | + |
| 313 | + String redirectedUrl = sendRedirect("parent/../foo/bar"); |
| 314 | + assertEquals("https://example.com/foo/bar", redirectedUrl); |
| 315 | + } |
| 316 | + |
| 317 | + @Test |
| 318 | + public void sendRedirectWithLocationHasScheme() throws Exception { |
| 319 | + this.request.addHeader(X_FORWARDED_PROTO, "https"); |
| 320 | + this.request.addHeader(X_FORWARDED_HOST, "example.com"); |
| 321 | + this.request.addHeader(X_FORWARDED_PORT, "443"); |
| 322 | + |
| 323 | + String location = "http://other.info/foo/bar"; |
| 324 | + String redirectedUrl = sendRedirect(location); |
| 325 | + assertEquals(location, redirectedUrl); |
| 326 | + } |
| 327 | + |
| 328 | + @Test |
| 329 | + public void sendRedirectWithLocationSlashSlash() throws Exception { |
| 330 | + this.request.addHeader(X_FORWARDED_PROTO, "https"); |
| 331 | + this.request.addHeader(X_FORWARDED_HOST, "example.com"); |
| 332 | + this.request.addHeader(X_FORWARDED_PORT, "443"); |
| 333 | + |
| 334 | + String location = "//other.info/foo/bar"; |
| 335 | + String redirectedUrl = sendRedirect(location); |
| 336 | + assertEquals("https:" + location, redirectedUrl); |
| 337 | + } |
| 338 | + |
| 339 | + @Test |
| 340 | + public void sendRedirectWithLocationSlashSlashParentDotDot() throws Exception { |
| 341 | + this.request.addHeader(X_FORWARDED_PROTO, "https"); |
| 342 | + this.request.addHeader(X_FORWARDED_HOST, "example.com"); |
| 343 | + this.request.addHeader(X_FORWARDED_PORT, "443"); |
| 344 | + |
| 345 | + String location = "//other.info/parent/../foo/bar"; |
| 346 | + String redirectedUrl = sendRedirect(location); |
| 347 | + assertEquals("https:" + location, redirectedUrl); |
| 348 | + } |
| 349 | + |
| 350 | + @Test |
| 351 | + public void sendRedirectWithNoXForwardedAndAbsolutePath() throws Exception { |
| 352 | + String redirectedUrl = sendRedirect("/foo/bar"); |
| 353 | + assertEquals("/foo/bar", redirectedUrl); |
| 354 | + } |
| 355 | + |
| 356 | + @Test |
| 357 | + public void sendRedirectWithNoXForwardedAndDotDotPath() throws Exception { |
| 358 | + String redirectedUrl = sendRedirect("../foo/bar"); |
| 359 | + assertEquals("../foo/bar", redirectedUrl); |
| 360 | + } |
| 361 | + |
| 362 | + private String sendRedirect(final String location) throws ServletException, IOException { |
| 363 | + MockHttpServletResponse response = doWithFiltersAndGetResponse(this.filter, new OncePerRequestFilter() { |
| 364 | + @Override |
| 365 | + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) |
| 366 | + throws ServletException, IOException { |
| 367 | + response.sendRedirect(location); |
| 368 | + } |
| 369 | + }); |
| 370 | + |
| 371 | + return response.getRedirectedUrl(); |
| 372 | + } |
| 373 | + |
| 374 | + private MockHttpServletResponse doWithFiltersAndGetResponse(Filter... filters) throws ServletException, IOException { |
| 375 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 376 | + FilterChain filterChain = new MockFilterChain(new HttpServlet() {}, filters); |
| 377 | + filterChain.doFilter(request, response); |
| 378 | + return response; |
| 379 | + } |
| 380 | + |
225 | 381 | private String filterAndGetContextPath() throws ServletException, IOException {
|
226 | 382 | return filterAndGetWrappedRequest().getContextPath();
|
227 | 383 | }
|
|
0 commit comments