Why Supabase Edge Functions cannot provide static egress IPs for allow listing
在尝试从 Supabase Edge Functions 建立到外部服务的安全连接时,你可能会遇到传统 IP 白名单的问题。本指南解释了为什么会出现这个问题,并提供了各种解决方案来应对它。
🌐 When trying to establish secure connections from Supabase Edge Functions to external services, you might encounter difficulties with traditional IP allow listing. This guide explains why this problem occurs and provides various solutions to address it.
什么是出口 IP 地址? #
🌐 What are egress IP addresses?
“出口 IP 地址”是指网络流量离开网络或服务时的公共 IP 地址。当一个应用或无服务器函数连接到外部第三方服务时,第三方服务看到的连接来源就是这个出口 IP。
🌐 An Egress IP address is the public IP address from which network traffic originates when leaving a network or service. When an application or a serverless function connects to an external third-party service, the third-party service sees the connection coming from this Egress IP.
CIDR 是什么? #
🌐 What is CIDR?
CIDR(无类域间路由) 是一种高效分配 IP 地址和路由 IP 数据包的方法。它允许使用类似 [IP]/prefix_length(例如 192.168.1.0/24)的表示法将 IP 地址分组为块。基于 CIDR 范围的允许列表通常会授予对指定块内所有 IP 地址的访问权限。
理解 Supabase Edge 函数及其问题 #
🌐 Understanding Supabase Edge Functions and the problem
Supabase Edge Functions 是在全球范围内运行的无服务器函数,靠近你的用户,由 Deno Deploy 提供支持。它们设计用于低延迟执行,并遵循“无服务器”和“边缘优先”的原则:
- 无服务器: 底层基础设施由云服务提供商动态管理。开发者不需要直接管理服务器;他们只需部署代码,平台会处理执行。这意味着 Edge Functions 不会运行在单一的专用服务器上。
- 边缘优先: 它们部署在全球分布的网络上。这确保它们在接近你用户的地理位置执行,从而最小化延迟并提高性能。
由于 Supabase Edge Functions 是无服务器的并且在全球分布,它们不会来自单一的静态 IP 地址或一个小而稳定的 IP 范围。这就无法分配传统网络级允许列表所需的固定出口 IP 地址。
🌐 Due to their serverless and globally distributed nature, Supabase Edge Functions do not originate from a single static IP address or a small, stable range of IPs. This prevents the assignment of fixed egress IP addresses necessary for traditional network-level allow listing.
问题总结 #
🌐 Problem summary
因为 Supabase Edge Functions 没有静态或稳定的出站 IP 地址,所以 Edge Function 发出的标准外部调用到需要 IP 白名单的服务(例如,指定像 [IP]/24 这样的 IPv4 CIDR 范围)很可能会被阻止。
🌐 Because Supabase Edge Functions lack static or stable egress IP addresses, standard outbound calls from an Edge Function to a service that requires IP allow listing (e.g., specifying an IPv4 CIDR range like [IP]/24) will likely be blocked.
允许列入白名单的外部服务的解决方案 #
🌐 Solutions for allow listing external services
本节介绍了如何让你的 Edge Functions 建立安全连接的方法。
🌐 This section details the approaches to enable secure connections from your Edge Functions.
解决方案1:使用外发代理(推荐) #
🌐 Solution 1: Use an outbound proxy (recommended)
这是连接你无法控制但需要静态 IP 地址以进行白名单的第三方服务的标准且推荐的解决方案。
🌐 This is the standard and recommended solution for connecting to third-party services that you do not control but which require a static IP address for allow listing.
- 工作原理: 与其让你的 Edge Function 直接连接到第三方服务,不如将它的所有请求通过一个中间的“出站代理”服务器转发。这个代理服务器配置了一个固定的公网 IP 地址。
- 实现:
- 部署一个小型的专用实例(例如,AWS EC2 实例或类似的云虚拟机),并设置固定的出口 IP。这个实例将作为你的网关或代理。
- 配置你的 Supabase Edge 函数,让它的外发请求通过这个代理服务器。
- 在外部第三方服务上允许列出你的代理服务器的静态 IP 地址。
- 优点: 所有来自边缘功能的流量都将显示来自代理的静态IP,满足外部服务的允许列表要求。
方案 2:基于请求头的认证(最适合你自己控制的服务器) #
🌐 Solution 2: Header-based authentication (best for servers you control)
如果你拥有或控制目标服务器,你可以把安全策略从网络层转到应用层。不依赖 IP 地址进行认证,而是使用共享密钥。
🌐 If you own or control the destination server, you can shift your security strategy from the network layer to the application layer. Instead of relying on IP addresses for authentication, you use a shared secret.
- 应用层和网络层安全是什么?
- **网络层安全:**根据来源IP地址过滤流量,运行在网络协议栈的较低层。IP允许列表就是一个例子。
- 应用层安全: 根据嵌入在应用通信中的凭证或秘密(比如 HTTP 头)对请求进行身份验证,工作在网络堆栈的更高层。
- 它是如何运作的:
-
生成一个秘密: 创建一个独特的随机秘密令牌。
-
存储密钥: 安全地在你的 Supabase 项目中存储这个令牌(例如,使用 Supabase Secrets,如
supabase secrets set CUSTOM_AUTH_TOKEN=your_random_string)。 -
边缘函数配置: 在你的边缘函数中,获取这个秘密令牌(例如,
const token = Deno.env.get("CUSTOM_AUTH_TOKEN");)。 -
发送请求: 在发送到目标服务器的每个请求中,都在自定义 HTTP 头中包含秘密令牌。
1await fetch('https://api.example.com/data', {2method: 'POST',3headers: {4'X-Custom-Auth': token, // Example custom header carrying the secret5'Content-Type': 'application/json',6},7body: JSON.stringify({8data: 'example',9}),10}) -
目标服务器配置: 在你的目标服务器上,配置你的中间件或应用逻辑以:
- 检查传入请求中是否存在这个自定义头和它的秘密令牌,并确认其是否正确。
- 拒绝任何没有携带正确秘密令牌的请求。
-
- 好处: 这在应用层面上创建了一个“虚拟允许列表”,有效地控制访问,而无需依赖静态 IP 地址。
解决方案 3:自托管边缘运行时 #
🌐 Solution 3: Self-hosting the edge runtime
这个解决方案适用于安全策略非常严格的场景(比如政府或健康数据法规),这些场景可能会因为合规原因禁止使用代理或基于秘钥的白名单。
🌐 This solution is for scenarios with extremely strict security policies (e.g., government or health data regulations) that might forbid the use of proxies or secret-based allow listing for compliance reasons.
- 工作原理: Supabase Edge Functions 构建于开源的 Edge Runtime 之上。你可以选择自己部署这个运行环境。
- 实现:
- 把开源的 Edge Runtime 部署到你自己的虚拟私有服务器(VPS)或类似的基础设施上。
- 确保你的VPS有一个你可以控制并加入白名单的静态IP地址。
- 好处: 这种方法让你完全掌控网络环境,包括分配和管理静态 IP 地址的能力,同时还能利用 Deno 的开发体验来编写你的函数。
- 进一步阅读: 更多详情通常可以在关于自托管 Deno 函数的文档或博客文章中找到。