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

Rib*_*bal 5 caching varnish purge firebase firebase-hosting

我有一个托管在 Firebase 上的网站,使用静态 html,没有使用服务器端函数来传递结果。

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

{ "status": "ok", "id": "20755-1619059392-3560756" }
Run Code Online (Sandbox Code Playgroud)

我需要一种方法来将此操作限制为特定 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);
 }
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何在 Firebase Hosting 中应用此功能,同样我不使用服务器功能,只是firebase.json使用以下标头的常规功能:

{ "status": "ok", "id": "20755-1619059392-3560756" }
Run Code Online (Sandbox Code Playgroud)

Thi*_*ryn 0

以下代码是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);
 }
}
Run Code Online (Sandbox Code Playgroud)

此代码允许您扩展 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
Run Code Online (Sandbox Code Playgroud)

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