Skip to content

x-forwarded-proto broken in Spring Boot 2.1.1 #753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
CalamarBicefalo opened this issue Dec 5, 2018 · 34 comments
Closed

x-forwarded-proto broken in Spring Boot 2.1.1 #753

CalamarBicefalo opened this issue Dec 5, 2018 · 34 comments

Comments

@CalamarBicefalo
Copy link

CalamarBicefalo commented Dec 5, 2018

For the following test:

@Test
fun `GET links uses proto headers`() {
     mvc.perform(get("/v1/").header("x-forwarded-proto", "https"))
              .andExpect(status().isOk)
              .andExpect(jsonPath("$._links.activate.href", startsWith("https")))
}

And the following implementation:

private fun buildLinkForUser(currentUser: User) =  
entityLinks.linkToCollectionResource(UserResource::class.java).withRel("activate")

Or alternatively with controller links, e.g.:

fun buildLinkForUser(): Link = linkTo(methodOn(UserController::class.java).activate())
                .withRel("activate")

I get the test passing in Spring Boot 2.0.7 and failing in Spring Boot 2.1.1 - because the link gets http instead of https.

I checked Spring Hateoas in both boot releases and it remains 0.25.0. Obviously, this makes me think this issue doesn't belong here. However, I was hoping you could help me reassign it where it belongs?

@wilkinsona
Copy link
Member

Thanks for the report, unfortunately, there isn't enough information here to diagnose where the problem might have been introduced. Can you please provide a minimal sample (something that we can unzip or git clone and run) that reproduces the behaviour you have described?

@gregturn
Copy link
Contributor

gregturn commented Dec 5, 2018

Spring Framework 5.1 (from Boot 2.1) alters forwarded header handling and we haven’t merge our patch to compensate for this.

See #713.

@CalamarBicefalo
Copy link
Author

Do you then need this zip @gregturn ? Or do you have enough context?

@CalamarBicefalo
Copy link
Author

Either way, here it is @gregturn, @wilkinsona, just upgrade boot and you'll see that one test failing.
user-service.zip

@gregturn
Copy link
Contributor

gregturn commented Dec 6, 2018

#758 has been merged to master and backported so you can find it in Spring HATEOAS 1.0.0.BUILD-SNAPSHOT (and soon 0.25.1.RELEASE).

If you test your app against this most recent change (and also configure Forwarded headers properly with Spring MVC), your issue should clear up.

If not, please provide more details.

@ilya40umov
Copy link

ilya40umov commented Dec 14, 2018

I have the same problem while trying to migrate to 2.1.1.
https://github.com/ilya40umov/KotLink/blob/master/src/test/kotlin/org/kotlink/KotLinkSecurityAndSslTest.kt
Which works perfectly fine on 2.0.x, starts failing on 2.1.1 with
Caused by: java.lang.AssertionError: Response header 'Location' expected:<[https://localhost/]> but was:<[http://localhost:37359/]>
I'm not using hateoas, so my question is if I should fine another bug against Spring MVC?

P.S. Forwarding headers are configured here:
https://github.com/ilya40umov/KotLink/blob/master/src/main/resources/application-local.yaml
P.S.S. If anybody will want to run the test, you can find more instructions on setting-up the environment here:
https://github.com/ilya40umov/KotLink/blob/master/docs/engineering-guide.md

Edit: I have moved to a new desktop and for whatever reason I can't reproduce the issue anymore.

@gregturn
Copy link
Contributor

If this is purely Spring Boot and not Spring HATEOAS then you need to open a ticket with Spring Boot.

@Bert-R
Copy link

Bert-R commented Jan 30, 2019

@gregturn You said

(and soon 0.25.1.RELEASE)

Is this still coming? It would be tremendously helpful for us if a fix could be released soon.

@gregturn
Copy link
Contributor

Test it out against 0.25.1.BUILD-SNAPSHOT and tell me if it works.

@jenny1976
Copy link

we currently use this SNAPSHOT and it works.

Bert-R added a commit to yonadev/yona-server that referenced this issue Jan 30, 2019
Bert-R added a commit to yonadev/yona-server that referenced this issue Jan 30, 2019
@Bert-R
Copy link

Bert-R commented Jan 30, 2019

It unfortunately does not work for us. With Spring Boot 2.0, the protocol worked, based on the X-Forwarded-Proto header, but now it doesn't work anymore.

@gregturn
Copy link
Contributor

Have you created the right filter? If you check this ticket there’s a link about how to properly activate forwarded headers.

Bert-R added a commit to yonadev/yona-server that referenced this issue Jan 31, 2019
* YD-261 Testing Spring HATEOAS 0.25.1.BUILD-SNAPSHOT

To see whether spring-projects/spring-hateoas#753 is fixed for us

* YD-621 set server.use-forward-headers=true

To enable interpretation of headers like X-Forwarded-For and X-Forwarded-Proto. This wasn't necessary before, but seems to be a new requirement for us.
@Bert-R
Copy link

Bert-R commented Jan 31, 2019

After adding server.use-forward-headers=true, it works. Since when is this setting necessary? We never used it and it always worked.

With my confirmation (and also from @jenny1976), would you be willing and able to release 0.25.1?

@jenny1976
Copy link

@gregturn
We would also appreciate a 0.25.1 Release because using a SNAPSHOT in production of course is not a Problem here but nevertheless makes us developers feel a bit nervous :)

@gregturn
Copy link
Contributor

Spring Framework now defaults with Forwarded header support disabled. So you have to use that setting for any apps you need from here on.

I’ll consult with @odrotbohm about seeing if we can get a patch release out the door.

@Bert-R
Copy link

Bert-R commented Feb 7, 2019

@gregturn Any news on this?

@odrotbohm
Copy link
Member

We can ship an 0.25.1 next week for inclusion in Spring Boot 2.1.3.

@Bert-R
Copy link

Bert-R commented Feb 7, 2019

That would be great. Thanks in advance!

@vjnaidu
Copy link

vjnaidu commented Feb 8, 2019

Once the patch is release would the "spring-boot-starter-hateoas" automatically pull the new patch 0.25.1.RELEASE as its dependency?

@gregturn
Copy link
Contributor

gregturn commented Feb 9, 2019

That will require a separate patch.

However, at any point, you can put this into your build file:

<spring-hateoas.version>0.25.1.BUILD-SNAPSHOT</spring-hateoas.version>

...and adjust it based on the release. (Comparable mod available if you are using Gradle).

@dinkarchaturvedi
Copy link

dinkarchaturvedi commented Feb 12, 2019

Saw a comment from @Bert-R mentioning that server.use-forward-headers=true works to bypass this issue in case of Spring Boot. Is there any Spring 5 XML equivalent for this?
We recently upgraded to Spring 5.1.3 and Hateoas to 0.25.0.RELEASE and started facing this issue where in case of a request coming from within the system (as opposed to from the client via the LB), the X-Forwarded-Host is not used while generating the links.
Also, when is 0.25.1 expected to be release independently?

@agebhar1
Copy link

agebhar1 commented Feb 12, 2019

If anybody struggles with this issue there is a fine workaround (?):

@Bean                                                                                                                                                                                                                                                                                                                                                                                 
public FilterRegistrationBean<ForwardedHeaderFilter> forwardedHeaderFilter() {                                                                                                                                                                                                                                                                                                        
    final FilterRegistrationBean<ForwardedHeaderFilter> filter = new FilterRegistrationBean<>();                                                                                                                                                                                                                                                                                  
    filter.setFilter(new ForwardedHeaderFilter());                                                                                                                                                                                                                                                                                                                                
    return filter;                                                                                                                                                                                                                                                                                                                                                                
} 

taken from https://stackoverflow.com/a/53269319

@odrotbohm
Copy link
Member

Spring HATEOAS 0.25.1 was just released and is ready for pickup in Spring Data Lovelace SR coming tomorrow and Spring Boot 2.1 maintenance release.

@Bert-R
Copy link

Bert-R commented Feb 13, 2019

@dinkarchaturvedi Besides setting server.use-forward-headers=true, you also need to use release 0.25.1.

Bert-R added a commit to yonadev/yona-server that referenced this issue Feb 16, 2019
* YD-261 Testing Spring HATEOAS 0.25.1.BUILD-SNAPSHOT

To see whether spring-projects/spring-hateoas#753 is fixed for us

* YD-621 set server.use-forward-headers=true

To enable interpretation of headers like X-Forwarded-For and X-Forwarded-Proto. This wasn't necessary before, but seems to be a new requirement for us.

* YD-621 Added extra assert to verify proxy settings
@dinkarchaturvedi
Copy link

Thanks @Bert-R . The Spring setting server.use-forward-headers appears to be specific to Spring Boot (I could be wrong) and meant to be used within application.properties file.
However, since we are using Spring 5.1, configured using XML, within a conventional web application running a standalone tomcat, I couldn't find an equivalent of this setting that can be used within XML. Could you share some documentation or links where I can find XML equivalent of server.use-forward-headers setting?

@gregturn
Copy link
Contributor

There is a bean definition listed above with a link to SO showing how to define the necessary bean.

@dinkarchaturvedi
Copy link

Thanks @gregturn . Apologies for posting on a closed issue but none of the solutions mentioned in this thread have helped my case. The SO link suggests to use FilterRegistrationBean which is available in SpringBoot but not as part of Spring Core 5.1 so I can't use that.
Also, I couldn't find XML equivalent for server.use-forward-headers setting that is used in application.properties in SpringBoot but our Spring configuration comes from XML.
I have upgraded the Hateoas to 0.25.1.Release version and hoping to find a way to fix the problem.

@raphaelLacerda
Copy link

i´ve recently upgraded from 1.5.4 to 2.1.4 and i´ve had the same issue.

With server.use-forward-headers=true on application.properties it´s fixed

@taleodor
Copy link

Thank you very much @raphaelLacerda - adding server.use-forward-headers=true fixed it for me as well.

@redent
Copy link

redent commented Feb 22, 2020

For future readers, use-forward-headers has been deprecated in favour of forward-headers-strategy:

server.forward-headers-strategy=native

forward-headers-strategy defaults to none.

@arung0wda
Copy link

For future readers, use-forward-headers has been deprecated in favour of forward-headers-strategy:

server.forward-headers-strategy=native

forward-headers-strategy defaults to none.

Can somebody explain how NATIVE and FRAMEWORK are different for this property? When should I use one over the other?

@odrotbohm
Copy link
Member

See the Javadoc on the corresponding enum.

@arung0wda
Copy link

See the Javadoc on the corresponding enum.

I did. But it doesn't give enough information for me to choose between Native/Framework. All it says is who handles forwarded headers for corresponding values. servlet container? or spring framework? but it brings back to square 1. Should I let container handle it? or the framework? when Should I prefer one over the other?

I've posted a question on stackoverflow regarding the same
https://stackoverflow.com/questions/68318269/

@odrotbohm
Copy link
Member

There already is an answer and discussion on the Stack overflow post. This ticket is the least appropriate place to discuss this.

@spring-projects spring-projects locked as resolved and limited conversation to collaborators Jul 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests