Skip to content

MvcUriComponentsBuilder javadocs inaccurately reflects usage of forwarded headers #34615

Closed
@dforrest-es

Description

@dforrest-es

In version 5.1.x, the ServletUriComponentsBuilder methods changed to remove the usage of forwarded headers:

However, the method and class documentation in MvcUriComponentsBuilder was not updated and still indicates the forwarded headers are used. (see instances of Note: in the javadocs) Yet MvcUriComponentsBuilder internally uses ServletUriComponentsBuilder for URI components resolution:

private static UriComponentsBuilder getBaseUrlToUse(@Nullable UriComponentsBuilder baseUrl) {
return baseUrl == null ?
ServletUriComponentsBuilder.fromCurrentServletMapping() :
baseUrl.cloneBuilder();
}

This affects 5.1.x -> 7.0.x.


Simple test:

@Test
void testMvcUriComponentsBuilder() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName("localhost");
    request.setServerPort(8080);
    request.setScheme("http");

    request.addHeader("X-Forwarded-Proto", "https");
    request.addHeader("X-Forwarded-Host", "test.example.com");
    request.addHeader("X-Forwarded-Port", "443");

    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, new MockHttpServletResponse()));

    @Controller
    @RequestMapping("/test")
    class InnerController {}

    String expected = MvcUriComponentsBuilder.fromController(InnerController.class).toUriString();
    assertEquals(expected, "https://test.example.com");
}

Error:

expected: <http://localhost:8080/test> but was: <https://test.example.com>

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: documentationA documentation task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions