Repository navigation
nav 链接图标自动抓取:worker 端图标解析器(复杂档) - #26
Merged
todosdev[bot] merged 3 commits intoNov 5, 2026
Merged
Conversation
added 3 commits
November 5, 2026 16:00
服务端读目标站 HTML 的 link rel=icon 并探测根文件,抓到后写入既有 R2 桶缓存(_cache/favicons/{host},不建表、不加绑定、不加定时任务),按字节嗅探类型后直接回图;请求 host 必须在站点注册表内,内网/回环/保留地址与非默认端口一律拒绝,不跟随重定向。私有链接域名需会话。保留前缀不经 /assets/* 公开。
Checkpoint: iYqIt-dY49HRMA7WT4W6_
302 过去的候选直接作废会让「icon 由同域跳转给出」的站(含 http→https 升级)退首字圆并写 7 天负缓存。改为自己管重定向:同一 host、默认端口、无账号密码、仅允许 http→https 升级,最多 2 跳;跨域/超跳数仍按候选失败处理,出网边界不变。缓存 sourceUrl 记最终地址,契约文档同步。
- 注册表按「更严一方」过滤:私有分类下的 public 链接对匿名不再暴露(与 /api/public 同口径),同一 host 优先 https。 - 后台刷新失败不再把在用的好图改写成负缓存,只顺延 fetchedAt。 - 图标声明的相对路径按跟随重定向后的 HTML 真实地址解析。 - 主机名末尾根点先规范化,`localhost.` 之类不再绕过内网黑名单。 - 图标响应加 `content-security-policy: default-src 'none'; sandbox`(第三方 SVG 不做本站脚本)与 `vary: Cookie`(会话相关结果不被共享缓存复用)。 - 收紧跳数上限用例断言;契约文档同步 400/404 语义与上述头部。
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.
Summary
GET /api/icon?url=…由服务端读目标站 HTML 的<link rel="icon">、再依次探测/favicon.ico、/favicon.svg、/favicon.png、/apple-touch-icon.png,抓到后直接回图字节。_cache/favicons/{host},时间戳放在对象customMetadata里 —— 无 D1 迁移、无 KV namespace、无新密钥、无定时任务。Changes
worker/src/services/favicon.ts(新增):解析器与缓存的全部逻辑 —— 目标校验与 SSRF 清单(含 IPv6 私网段、尾点规范化)、<link>声明的纯函数解析与偏好排序(跨 host 声明忽略)、带 5s 超时/字节上限/redirect: manual的抓取、按魔数嗅探真实类型(复用services/assets.ts的探测函数)、R2 读写与新鲜度(有图 30 天 / 无图 7 天,过期先回旧图后台重抓)、并发解析合并、站点注册表(可见性取更严一方、同 host 优先 https)。worker/src/routes/icon.ts(新增):400 / 404 / 200三条分支与响应头(cache-control、nosniff、content-security-policy: default-src 'none'; sandbox、vary: Cookie);waitUntil在无 ExecutionContext 环境下安全降级。worker/src/index.ts:挂载/api/icon,位于 SPA 兜底之前。worker/src/middleware/auth.ts:把会话校验抽成可复用的getSessionUserId(),authMiddleware行为不变;图标端点用它做「私有 host 需要会话」的可选判断。worker/src/routes/assets.ts+worker/src/services/assets.ts:GET /assets/*拒绝保留前缀_cache/,避免内部缓存对象被按 key 猜读。docs/api-contract.md:新增GET /api/icon契约小节(入参、注册表与可见性口径、解析顺序、出网约束、成功/失败语义、缓存与刷新策略、SVG 与 Vary 说明),并把原「favicon 抓取不在后端做」一句改为指向新端点。worker/test/helpers.ts:MemoryR2补上customMetadata/uploaded/bytes(),让缓存逻辑可在内存替身上被真实验证。worker/test/favicon.test.ts(新增,36 条):SSRF 拒绝清单、声明解析与排序、相对路径解析、注册表可见性与 https 优先、冷启动抓取与落缓存、命中不回源、负缓存、过期后台刷新、刷新失败保留好图、并发合并、跨域 3xx 不跟随与同域 2 跳上限、超大/非图片/HTML 冒充、响应头白名单、保留前缀不可公开读取。Testing
pnpm test:worker 151 条、web 78 条全过;pnpm typecheck、pnpm lint、pnpm format:check、pnpm build均通过(与 CI 同一套命令)。42009439840,head73535c2,completed / success(Lint / Format check / Type check / Build / Test 全绿)。wrangler dev端到端:已登记https://github.com→200 image/x-icon、6518 字节(file认出 ICO),响应头符合契约,冷启动约 4s、二次 7.7ms;未登记 host →404且无出网;http://127.0.0.1/、ftp://…、缺参 →400;私有链接 host 匿名404、带会话200;/assets/_cache/...→404。y18.svg)、B站、掘金、V2EX(用其声明的/static/icon-192.png)、MDN、Google、Bing、百度、DuckDuckGo 均取到图;知乎仍取不到 —— 其图标跨 301 到static.zhihu.com,按契约不跨域跟随,回退首字圆。bmp/avif等奇异格式回退首字圆;被 WAF 挡掉非浏览器 UA 的站回退首字圆;前端SiteIcon接入由另一单负责,本 PR 只交付后端与契约 —— 因此合并部署后页面图标不会立刻变化。