繁体   English   中英

是否有任何替代方案可以以编程方式覆盖“主机”header

[英]Are there any alternatives to overriding the 'host' header programatically

我在 docker 图像上本地运行了两个服务。 其中一个是 nginx 服务器,其配置为将请求代理到各种其他服务,另一个是简单的 React GraphiQL UI。

The nginx server is not explicitly set up to run on localhost, but when making requests with curl/postman I can explicitly set the host header to be that of the actual url (rather than localhost ) and it will then find the correct config and the请求会成功。

问题是我想从我的 UI 的本地实例调用服务器,但它失败了,因为我无法覆盖host header。 我尝试将其手动添加到我的反应fetch请求中,但是当我在浏览器中检查请求时,header 不存在。 经过一番搜索,我发现一些松弛的帖子说这是不可能的,尽管没有提到原因。

return fetch(
        edgeUrl(environment) + "/some/endpoint",
        {
            method: "POST",
            headers: {
                'Authorization': 'Bearer ' + getApiKey(partner, environment),
                'host': 'actual.host.com',
                'origin': 'http://localhost/'
            },
            body: JSON.stringify({ query })
        }
    )

有没有其他方法可以覆盖请求中使用的主机? 可能我可以使用另一个 http 库? 我不想为 localhost 配置 nginx 服务器,因为它由另一个团队拥有。

您不应该尝试更改主机 header。 浏览器不允许您这样做,这不是正确的方法。

在我看来,您有两个选择:

  1. 配置 NGINX 以接受对 localhost 的请求,如果那是它的实际主机名。

  2. 更改 hosts 文件,以包含指向 127.0.0.1 的域,这相当于将其添加到 DNS。

Windows 主机文件位于此处: C:\Windows\System32\drivers\etc\hosts

您应该在注释#之后将以下内容添加到您的主机文件中。

actual.host.com 127.0.0.1

对于任何感兴趣的人,这里有一些关于使用主机 header 的可能攻击的信息,以及为什么验证它很有用,这就是该服务正在做的事情。

https://portswigger.net/web-security/host-header

https://infosecwriteups.com/identifying-escalating-http-host-header-injection-attacks-7586d0ff2c67

我将询问其他团队是否可以将 localhost 配置添加到他们的 nginx 配置中,以便我可以在本地发出请求,看起来我的同事在建议我覆盖主机 header 时被误导了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM