spring-cloud / spring-cloud/spring-cloud-gateway

Expecting correct host request header when proxy uri has different host then the original call

Open
#2,939 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

waiting-for-triage
Dominant language
Java
Stars
4.9k
Forks
3.5k
Avg merge
20h 57m
Merged PRs (30d)
8

Description

Using spring-cloud-gateway-mvc-3.1.6.jar

I built an endpoint using (mvc) ProxyExchange like in the example of the documentation: https://cloud.spring.io/spring-cloud-gateway/reference/html/#building-a-simple-gateway-by-using-spring-mvc-or-webflux

@RestController
@SpringBootApplication
public class GatewaySampleApplication {

    @Value("${remote.home}")
    private URI home;

    @GetMapping("/test")
    public ResponseEntity<?> proxy(ProxyExchange<byte[]> proxy) throws Exception {
        return proxy.uri(home.toString() + "/image/png").get();
    }

}

However, the problem I face is that if the endpoint is called from a host e.g. abc.com and then the value of ${remote.home} is def.com, then the proxy or underlying restTemplate more or less invokes the request using abc.com both as host and forward request header, whereas I expected def.com as host request header value and abc.com as forward request header value.
This also then ends up in a 403 response.

As a workaround I used proxy.sensitive("host"). This forced restTemplate to set a host request header for me again and surprise, the host request header is then the host of the ${remote.home} URI.

Or what also worked is using proxy.header(HttpHeaders.HOST, home.getHost()) by setting the host name of the ${remote.home} URI directly.

Is this supposed to work like this? I just expected that the host request header is automatically set correct if host differs from origin host.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the MVC ProxyExchange entry point and the underlying RestTemplate request handling described in the report. Reproduce the /test endpoint with an incoming host different from remote.home, then trace the Host and forwarding headers. Done means the differing target and origin hosts are handled as expected, with regression coverage for the reported 403 case.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.