Add a function that checks a Cloudflare Turnstile token on the server
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 88/100
- Issue 类型
- 功能
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- typescript
调研方向
阅读 packages/server/src/webhook.ts,了解现有的 fetch 超时模式;阅读 packages/server/tests/webhook-delivery.test.ts,了解 fetch 的模拟方式。实现 packages/server/src/security/turnstile.ts,并在 packages/server/tests/security/turnstile.test.ts 中添加指定的测试。满足验收标准后即为完成,包括请求字段、不匹配的情况,以及不会抛出异常或泄露 secret 的失败情况。
由索引模型根据 Issue 内容生成。
描述
ChatCops is getting optional Cloudflare Turnstile checks: the widget gets a token from Turnstile when a visitor first opens the chat, and the server checks it before it starts a session. This issue is the server half of that check, as one small function in a new file. Wiring it into a route comes later, so this PR doesn't touch anything else.
What to do
Create packages/server/src/security/turnstile.ts with:
export interface TurnstileConfig {
secretKey: string;
/** The action set in the widget, if any. A token for another action is rejected. */
action?: string;
/** Hostnames the token may come from. Unset means any. */
expectedHostnames?: string[];
/** Default 5000. */
timeoutMs?: number;
}
export type TurnstileResult =
| { ok: true; hostname?: string; action?: string }
| { ok: false; code: 'missing_token' | 'invalid_token' | 'hostname_mismatch' | 'action_mismatch' | 'unavailable'; errorCodes?: string[] };
export async function verifyTurnstile(
token: string | undefined,
config: TurnstileConfig,
opts?: { remoteIp?: string; idempotencyKey?: string; fetch?: typeof fetch }
): Promise<TurnstileResult>;
- POST to
https://challenges.cloudflare.com/turnstile/v0/siteverifyas described in Cloudflare's server-side validation docs, withsecret,response,remoteip(when given) andidempotency_key(when given). - A token can only be redeemed once. The
idempotency_keylets the server retry the same check after a network error without it failing as already used, so pass it through. - Return
missing_tokenwithout calling the API when the token is empty, andunavailablewhen the request fails, times out (AbortSignal.timeout) or doesn't return JSON. Never throw. - Check
hostnameandactionfrom the response against the config. - Use
opts.fetch ?? globalThis.fetch, so tests can pass a fake. Don't import anything from Node; this has to run on Workers too.
Where to look
packages/server/src/webhook.tshas a similar fetch with a timeoutpackages/server/tests/webhook-delivery.test.tsshows how the tests fakefetch
Acceptance criteria
- New
packages/server/src/security/turnstile.tsandpackages/server/tests/security/turnstile.test.ts - Tests cover success,
success: falsewith error codes, hostname and action mismatch, an empty token (no request sent), a network error, a timeout and a non-JSON reply - A test checks the request body, including
idempotency_keyandremoteip - The secret never appears in a returned value or in a log line
- Not exported from
@chatcops/serveryet, and no changeset needed (pnpm changeset --empty) -
pnpm test:coverageandpnpm lintpass
Want to work on this? Comment below and I'll assign it to you. Please take one issue at a time, branch off dev, and open your PR against dev with Closes #<this issue number>. Setup notes (pnpm, build before test) are in CONTRIBUTING.md.
- 主要语言
- TypeScript
- 星标
- 13
- 派生
- 7
- 平均合并
- 4 小时 11 分钟
- 30 天内合并 PR
- 122
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
codercops/chatcops 的其他 Issue
-
Document the pre-chat form可能已有人在做 @masyarakatai 于 4 天前认领。 未关闭documentation good first issue hacktoberfest widget
难度 2/5 1-3 小时 新手友好度 88/100
codercops/chatcops#70 · 3 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
enhancement hacktoberfest help wanted server
难度 5/5 一周以上 新手友好度 25/100
维护者通常 1 天内回复
-
Create @chatcops/store-redis for Upstash and other Redis clients可能已有人在做 @bhushan4work 今天认领。 未关闭enhancement hacktoberfest help wanted server
codercops/chatcops#248 · 2 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
enhancement hacktoberfest help wanted server
难度 5/5 一周以上 新手友好度 35/100
维护者通常 1 天内回复
-
core enhancement hacktoberfest help wanted
难度 3/5 1-2 天 新手友好度 78/100
维护者通常 1 天内回复
查看 codercops/chatcops 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 67/100
mobile-next/mobile-mcp#526 ·
维护者通常 1 天内回复
-
e2e-failure ready-to-code
难度 1/5 1 小时以内 新手友好度 73/100
redhat-developer/rhdh-plugin-export-overlays#4470 · 1 条评论 ·
维护者通常 1 天内回复