使用 Firebase 托管,如何将 PURGE 请求限制为特定 IP

问题描述

我在 Firebase 上托管了一个网站,使用静态 html,没有使用服务器端功能来提供结果。

运行 curl -X PURGE https://mywebsite.com -v -L 时的结果是:

{ "status": "ok","id": "20755-1619059392-3560756" }

我需要一种方法来将此操作限制在特定 IP 上,这样任何人都无法重置我的缓存,这可能会导致额外费用。

此外,Firebase 似乎使用 Varnish 来管理缓存(这是空的)。

我客户的安全顾问向我们发送了有关如何处理此问题的建议,我不确定这是 .htaccess 语法还是什么:

# Varnish recommends to using PURGE method only by valid user,# for example by ip limiting and for other return 405 Not allowed:


acl purge { 
 "localhost";
 "192.168.55.0"/24;
}

sub vcl_recv {
 # allow PURGE from localhost and 192.168.55...
 if (req.method == "PURGE") { 
  if (!client.ip ~ purge) {
   return(synth(405,"Not allowed."));
  }
  return (purge);
 }
}

我不知道如何在 Firebase 托管中应用它,我也没有使用服务器功能,只是使用带有以下标头的常规 firebase.json

      "headers": [
        {
          "source": "*.[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f].+(css|js)","headers": [
            {
              "key": "Cache-Control","value": "public,max-age=31536000,immutable"
            }
          ]
        },{
          "source": "**/*.@(json|eot|otf|ttf|ttc|woff|font.css)","headers": [
            {
              "key": "Access-Control-Allow-Origin","value": "*"
            }
          ]
        }
      ]

解决方法

以下代码是VCL代码

acl purge { 
 "localhost";
 "192.168.55.0"/24;
}

sub vcl_recv {
 # allow PURGE from localhost and 192.168.55...
 if (req.method == "PURGE") { 
  if (!client.ip ~ purge) {
   return(synth(405,"Not allowed."));
  }
  return (purge);
 }
}

此代码允许您扩展 Varnish 的行为。必须将此代码添加到您的 /etc/varnish/default.vcl 文件中。

将此代码添加到您的 VCL 文件后,您必须重新加载 varnishd 进程以激活此 VCL 配置。

如果重新加载 varnishd 不是一个选项,您还可以使用以下命令激活新的 VCL 文件:

sudo varnishadm vcl.load purge_acl /etc/varnish/default.vcl
sudo varnishadm vcl.use purge_acl

有关VarnishVCL 编程语言的更多信息,请查看http://varnish-cache.org/docs/6.0/reference/index.html