Vercel Sandbox firewall 现在支持请求代理和过滤
Source: Vercel Blog
Vercel Sandbox 防火墙现在支持将特定的 HTTP 请求转发到您自行控制的代理。您还可以使用匹配器对转发进行过滤,并仅对需要的请求进行凭证中介。
请求代理
您可以将出站 sandbox 流量通过自己的代理进行日志记录、调试或转换请求和响应。只需在任意允许的域上设置 forwardURL,防火墙就会将匹配的 HTTPS 请求转发到您的服务器。
代理会收到原始请求以及用于标识来源的额外请求头:
vercel-forwarded-host:原始请求的 SNIvercel-forwarded-scheme:原始请求的协议vercel-forwarded-port:原始请求的端口vercel-sandbox-oidc-token:Vercel 颁发的 OIDC 令牌,代理可使用该令牌对请求进行身份验证并识别来源团队 / 项目 / sandbox。更多信息请参阅文档。
import { Sandbox } from '@vercel/sandbox';
// Sandbox has access to everything, with a proxy for requests towards github.com
const sandbox = await Sandbox.create({
networkPolicy: {
allow: {
"github.com": [
{
forwardURL: "https://my-custom-proxy.vercel.app/api/proxy"
}
],
// Allow traffic to all other domains. If unset only defined ones are reachable.
"*": []
}
}
});
过滤
您现在可以使用匹配器对请求转发或凭证中介进行过滤,仅针对匹配特定路径、方法、查询字符串或请求头的请求生效。这使您能够细粒度地控制哪些请求会被转换;例如,仅转发 POST 请求到特定的 API 路径,而让其他所有流量保持不变。
import { Sandbox } from '@vercel/sandbox';
// Sandbox has access to everything, with a proxy for POST requests to api.github.com/v1/*
const sandbox = await Sandbox.create({
networkPolicy: {
allow: {
"api.github.com": [
{
match: {
path: { startsWith: "/v1" },
method: ["POST"]
},
forwardURL: "https://my-custom-proxy.vercel.app/api/proxy"
}
],
// Allow traffic to all other domains. If unset only defined ones are reachable.
"*": []
}
}
});
这些功能已在 Pro 和 Enterprise 计划的 beta 版中提供。通过安装 @vercel/sandbox@beta SDK 开始使用,并在文档中了解更多关于请求代理和匹配器的内容。