Too many transfer-encoding issue

本文详细分析了在使用 Go 1.15+ 版本时遇到的由于重复或不规范的 Transfer-Encoding 头部导致的错误。问题源于 Go 1.15 对 Transfer-Encoding 的更严格处理,旨在防止请求走私漏洞和其他安全问题。当服务端应用通过代理转发请求时,不正确地复制 hop-by-hop 头部,特别是 transfer-encoding,会导致此错误。解决方案建议在返回 ResponseEntity 之前移除响应中的 transfer-encoding 头部,可以使用 RestTemplate 的拦截器来实现这一操作。

Problem

we experiences error as pic shown when receiving response from backend service rm-client-backend, which serves as a proxy routing http call.

 

Reason

The reason the error above is happening is our service has been built with Golang 1.15+. Before Golang 1.15, duplicated transfer-encoding headers or headers with two values were permitted by Golang's proxy roundTrip. However, starting in Golang 1.15+, the implementation is more strict. Specifically, the router is now rejecting responses from applications with either multiple transfer encodings or out-of-spec "identity" encoding values. Golang 1.15 is more strict about the Transfer Encoding header to prevent a well-known request smuggling vulnerability, as well as mitigate a broad class of security issues around this header over time.

Why backend application have multiple transfer encodings?

An application proxies requests (like a gateway) and the proxy/gateway app is naïvely copying all headers between the client and backend application. This can cause issues because you may copy a transfer encoding header added by the server to which you're communicating and also have the proxy server add in a second transfer-encoding header, which can trigger the error. It is incorrect for a proxy/gateway application to copy the transfer-encoding header, which is a hop-by-hop header

More specifically, In backend proxy service, when using RestTemplate from a Controller annotated class and returning the result of RestTemplate.exchange in a method that returns a ResponseEntity. Similar to situation #1, this will cause a problem if the microservice to which you are talking returns a transfer-encoding header because RestTemplate.exchange will copy all of the response headers from the client response into the ResponseEntity it creates, including the transfer-encoding header.

Solution

Issue can be mitigated by not directly returning ResponseEntity objects from RestTemplate.exchange. You need to first remove the transfer-encoding header, if it's present. You could directly modify that object in your Controller before returning it, but that could get repetitive across many methods and many controllers. A less invasive way of doing this would be with a RestTemplate interceptor.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值