Skip to content

Regression in 4.1.5: UriComponentsBuilder.fromHttpRequest sets port to 80 i.s.o nothing/443 [SPR-12771] #17368

Closed
@spring-projects-issues

Description

@spring-projects-issues

Bart Van Bos opened SPR-12771 and commented

Hi,

I am using spring-hateoas to generate URL links between my REST managed resources. Since the upstep from Spring 4.1.4 -> 4.1.5, I'm having problems with the PORT/SCHEME combination generated.

Example link before the update:

https://example.com/rest/mobile/users/1

Example link after the update:

https://example.com:80/rest/mobile/users/1

As you can see, it automatically append port 80 to the URL. The problem is that my rest back-end is running on a cloud platform heroku, which is using a reverse proxy to terminate the TLS (HTTPS).

browser --- (HTTPS/TLS on :443) --- reverse proxy --- (HTTP on :80) --- Heroku web server

This worked well before the update since the port was not automatically appended and the browser could figure out the port by negotiation. By hard coding it (by taking it form the original request), this auto detecting is not possible anymore.

These are my headers:

Header Name: host, Header Value: example.com
Header Name: connection, Header Value: close
Header Name: user-agent, Header Value: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0
Header Name: accept, Header Value: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Header Name: accept-language, Header Value: en-US,en;q=0.5
Header Name: accept-encoding, Header Value: gzip, deflate
Header Name: x-request-id, Header Value: d97ed0c8-c91e-492a-b609-e05290f2fb2d
Header Name: x-forwarded-for, Header Value: 84.198.58.199
Header Name: x-forwarded-proto, Header Value: https
Header Name: x-forwarded-port, Header Value: 443
Header Name: via, Header Value: 1.1 vegur

I believe the problem lies in UriComponentsBuilder.fromHttpRequest:

String portHeader = request.getHeaders().getFirst("X-Forwarded-Port");
if (StringUtils.hasText(portHeader)) {
    port = Integer.parseInt(portHeader);
}

...

builder.scheme(scheme);
builder.host(host);
if (scheme.equals("http") && port != 80 || scheme.equals("https") && port != 443) {
    builder.port(port);
}
return builder;

In my case X-Forwarded-Port == 443, so the builder.port should be set to 443 or left blank (not filled with 80 as is now the case).


Affects: 4.1.5

Reference URL: spring-projects/spring-hateoas#301

Issue Links:

Referenced from: commits 410fc6d, d64c48f

2 votes, 7 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions