httputil.ReverseProxy保留Host标头
本文讨论了如何使用 Go 语言的 httputil.ReverseProxy 创建一个反向代理服务器。作者遇到的问题是,尽管修改了请求标头,但 Host 标头仍会泄漏到后端服务器。经过调查,发现原因在于 url.host 的值仅在 request.host 为空的情况下使用。通过设置 request.host 为目标主机名,解决了 Host 标头泄漏问题。
我基本上是在尝试编写一个反向代理服务器,以便当我 curl localhost:8080/get 时,它会将请求代理到 https://nghttp2.org/httpbin/get。
注意:上面列出的 https://nghttp2.org/httpbin/get 服务是 http/2。但这种行为也会发生在 http/1 上,例如 https://httpbin.org/get。
我为此使用 httputil.reverseproxy,并且在自定义 host 标头时重写 url,以免将 localhost:8080 泄漏到实际后端。
但是,无论我在标头上设置多少次,请求仍然会通过 host: localhost:8080 到达后端。同样,我使用 mitmproxy 来监听请求,看起来 net/http.client 将 :authority 伪标头设置为 localhost:8080
这是我的源代码:
package main
import (
"log"
"net/http"
"net/http/httputil"
)
func main() {
proxy := &httputil.reverseproxy{
transport: roundtripper(rt),
director: func(req *http.request) {
req.url.scheme = "https"
req.url.host = "nghttp2.org"
req.url.path = "/httpbin" + req.url.path
req.header.set("host", "nghttp2.org") // <--- i set it here first
},
}
log.fatal(http.listenandserve(":8080", proxy))
}
func rt(req *http.request) (*http.response, error) {
log.printf("request received. url=%s", req.url)
req.header.set("host", "nghttp2.org") // <--- i set it here as well
defer log.printf("request complete. url=%s", req.url)
return http.defaulttransport.roundtrip(req)
}
// roundtripper makes func signature a http.roundtripper
type roundtripper func(*http.request) (*http.response, error)
func (f roundtripper) roundtrip(req *http.request) (*http.response, error) { return f(req) }
当我查询 curl localhost:8080/get 时,请求被代理到 https://nghttp2.org/httpbin/get。回显的响应显然表明我设置 host 标头的指令没有执行任何操作:
{
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip",
"Host": "localhost:8080",
"User-Agent": "curl/7.54.0"
},
"origin": "2601:602:9c02:16c2:fca3:aaab:3914:4a71",
"url": "https://localhost:8080/httpbin/get"
}
mitmproxy 监听还清楚地表明该请求是通过 :authority 伪标头设置为 localhost:8080 发出的:
解决方案
来自http.request docs:
// for server requests, host specifies the host on which the url
// is sought. per rfc 7230, section 5.4, this is either the value
// of the "host" header or the host name given in the url itself.
// it may be of the form "host:port". for international domain
// names, host may be in punycode or unicode form. use
// golang.org/x/net/idna to convert it to either format if
// needed.
// to prevent dns rebinding attacks, server handlers should
// validate that the host header has a value for which the
// handler considers itself authoritative. the included
// servemux supports patterns registered to particular host
// names and thus protects its registered handlers.
//
// for client requests, host optionally overrides the host
// header to send. if empty, the request.write method uses
// the value of url.host. host may contain an international
// domain name.
host string
因此,url.host 的值仅在 request.host 为空的情况下使用,但情况并非如此。设置 request.host 应该可以解决该问题:
req.Host = "nghttp2.org"
相关问题讨论here。
本篇关于《httputil.ReverseProxy保留Host标头》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!
禁用Win10系统的系统保护功能
- 上一篇
- 禁用Win10系统的系统保护功能
- 下一篇
- 深入了解PHP模运算符的功能和用法
-
- Golang · Go问答 | 9小时前 | 标准库 · Go问答 · Go RGBA image/draw image.Image color.Model Paletted draw.Draw Gray
- Go image/draw.Draw 怎样处理不同颜色模型的绘制
- 374浏览 收藏
-
- Golang · Go问答 | 9小时前 | 标准库 · 事务 · database/sql · Go问答 · Go 事务 isolation database/sql readonly TxOptions BeginTx
- Go database/sql TxOptions 的 ReadOnly 和 Isolation 怎么配合
- 443浏览 收藏
-
- Golang · Go问答 | 9小时前 | 标准库 · 连接池 · database/sql · Go问答 · Go 连接池 database/sql 连接生命周期 sql.Conn Conn.Close ErrConnDone
- Go database/sql.Conn.Close 后连接怎样回到连接池
- 426浏览 收藏
-
- Golang · Go问答 | 10小时前 | 标准库 · HTTP · Go问答 · Go 重复请求头 MIMEHeader.Values 首个值 textproto.MIMEHeader.Get 请求头大小写
- Go textproto.MIMEHeader.Get 为什么只返回一个请求头值
- 300浏览 收藏
-
- Golang · Go问答 | 10小时前 | 结构体 · 标准库 · 错误处理 · Go问答 · Go io.ErrUnexpectedEOF encoding/binary.Read 结构体空白字段 协议填充 binary.Size
- Go encoding/binary.Read 怎样处理结构体中的空白字段
- 330浏览 收藏
-
- Golang · Go问答 | 10小时前 | go · Go math/big big.Int Float.IsInt Float.Int64
- Go math/big.Float.IsInt 为什么不能用来判断能否转成 int64
- 358浏览 收藏
-
- Golang · Go问答 | 10小时前 | 标准库 · Go问答 · Go 位运算 位图 math/bits bits.Len64
- Go math/bits.Len64 为什么适合计算位图宽度
- 235浏览 收藏
-
- Golang · Go问答 | 10小时前 | go · math/big.Rat.Scan fmt.Scanner Go有理数
- Go math/big.Rat.Scan 的 %f 与 %v 为什么采用相同读取规则
- 270浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 1123次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 1110次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 1283次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 1224次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 1076次使用
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- Go index/suffixarray.Index.Bytes 怎样取得索引引用的原始数据
- 2026-11-18 501浏览
-
- Go gosym.Sym.BaseName 怎样去掉符号的包名与接收者
- 2026-11-17 501浏览
-
- 正则命名捕获组取不到值怎么办
- 2026-11-15 501浏览
