Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Add a function that checks a Cloudflare Turnstile token on the server

Đã đóng Phù hợp với người mới
#228 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@bhushan4work đang làm issue này rồi.

Từ ngày 21/10/2026.

  • #282 của @bhushan4work — đã merge

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
88/100
Loại issue
Tính năng
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
typescript
Lĩnh vực
backend, security

Hướng nghiên cứu

Đọc packages/server/src/webhook.ts để xem mẫu timeout fetch hiện có và packages/server/tests/webhook-delivery.test.ts để xem cách giả lập fetch. Triển khai packages/server/src/security/turnstile.ts và thêm các bài kiểm thử được chỉ định vào packages/server/tests/security/turnstile.test.ts. Công việc hoàn tất khi đáp ứng các tiêu chí chấp nhận, bao gồm các trường của yêu cầu, các trường hợp không khớp và các lỗi không được ném ngoại lệ hoặc làm lộ secret.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

enhancement hacktoberfest help wanted server

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/siteverify as described in Cloudflare's server-side validation docs, with secret, response, remoteip (when given) and idempotency_key (when given).
  • A token can only be redeemed once. The idempotency_key lets the server retry the same check after a network error without it failing as already used, so pass it through.
  • Return missing_token without calling the API when the token is empty, and unavailable when the request fails, times out (AbortSignal.timeout) or doesn't return JSON. Never throw.
  • Check hostname and action from 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.ts has a similar fetch with a timeout
  • packages/server/tests/webhook-delivery.test.ts shows how the tests fake fetch

Acceptance criteria

  • New packages/server/src/security/turnstile.ts and packages/server/tests/security/turnstile.test.ts
  • Tests cover success, success: false with 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_key and remoteip
  • The secret never appears in a returned value or in a log line
  • Not exported from @chatcops/server yet, and no changeset needed (pnpm changeset --empty)
  • pnpm test:coverage and pnpm lint pass

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.

Ngôn ngữ chính
TypeScript
Star
13
Fork
7
Merge trung bình
3 giờ 33 phút
Pull request đã merge (30 ngày)
176

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của codercops/chatcops

Tất cả issue của codercops/chatcops

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.