Repository navigation
fix(worker): 限制 SSE 存活时间并关闭 workers_dev 防范账单失控 - #150
Open
sunyuan686 wants to merge 2 commits into
Open
sunyuan686 wants to merge 2 commits into
sunyuan686 wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概述
对照 Serverless Horrors 中披露的典型 Serverless 计费陷阱(Denial of Wallet),以**最小改动(3 个文件,8 行代码)**修复当前项目中两个高危的账单失控漏洞。
问题背景与严重性
/api/v1/sse实时流无限轮询打爆 D1 读配额 (🚨 致命隐患)默认⚠️ 高危隐患)
workers.dev域名绕过安全与缓存防线 (workers_dev,Cloudflare 默认会对外暴露*.workers.dev二级域名。workers.dev,完全绕过自定义域名上配置的 WAF 防火墙、Rate Limiting 以及 CDN 缓存策略,直接穿透刷爆 Worker 资源。解决方案
apps/worker/src/routes/memos-sse.ts:限制单次 SSE 连接最长存活 60 秒(
maxStreamDurationMs = 60_000)。到期后优雅断开,依赖浏览器原生EventSource携带Last-Event-ID透明自动重连。用户体验完全无感、不漏任何事件,同时彻底杜绝了后台僵尸连接持续空转。wrangler.json&wrangler.jsonc.example:显式配置
"workers_dev": false与"preview_urls": false,收紧公网入口,强制所有访问经过自定义域名的统一防护与缓存。验证命令
pnpm format:checkpnpm --filter @flaremo/worker checkpnpm exec vitest run apps/worker/src/transport/sse-stream.test.tspnpm deploy:dry-run