展示 HN:适用于 Cloudflare Workers 的极简 NIST/OWASP 合规认证实现
Show HN: Minimal NIST/OWASP-compliant auth implementation for Cloudflare Workers

原始链接: https://github.com/vhscom/private-landing

这个项目是一个使用 Hono、Turso 和 TypeScript 为 Cloudflare Workers 构建的、从头开始的教育性身份验证参考实现。它优先考虑安全最佳实践,遵循 NIST 和 OWASP 标准进行密码处理(PBKDF2)、JWT 会话(滑动过期机制的双令牌模式)和整体验证。 该实现包括安全的 cookie 处理、安全标头以及通过 Zod 模式进行的强大的输入验证。它经过了大量测试——超过 250 个测试涵盖了标准功能*以及*令牌篡改等潜在攻击向量。 **但是,它不适用于生产环境。** 该项目故意省略了实时应用程序所必需的功能,例如速率限制、帐户锁定和泄露密码检查。作者建议使用他们的生产就绪库 **Better Auth** 来满足这些需求。 该代码的设计目的是为了学习——强调做出安全选择的*原因*,并提供详细的文档,包括架构决策记录和安全审计。它结构清晰且易于 AI 读取,使其成为理解身份验证机制的宝贵资源。

一位开发者发布了一个针对 Cloudflare Workers 的精简、教育性的身份验证实现,旨在清晰易懂并符合 NIST/OWASP 安全标准。该项目在 GitHub 上可用 ([https://github.com/vhscom/private-landing](https://github.com/vhscom/private-landing)),使用了 Hono、Turso (libSQL) 以及 PBKDF2-SHA384 与 JWT 和刷新令牌等强大的加密实践。 重要的是,开发者明确指出该项目*不*适用于生产环境,而是推荐使用 Better Auth ([https://www.better-auth.com](https://www.better-auth.com))。其目标是理解边缘运行时身份验证的限制,并提供一个清晰、可审计的示例,采用 Apache-2.0 许可协议。 一个在线演示可在 [https://private-landing.vhsdev.workers.dev/](https://private-landing.vhsdev.workers.dev/) 访问。开发者欢迎大家就实现中做出的技术选择提出问题。一位评论者赞扬了明确的禁止用于生产环境的声明。
相关文章

原文

A from-scratch authentication reference implementation for Cloudflare Workers — PBKDF2 password hashing, JWT dual-token sessions, constant-time comparison, and sliding expiration — all wired together with Hono, Turso, and strict TypeScript.

Every design choice traces back to a standard: NIST SP 800-63B for credentials, NIST SP 800-132 for key derivation, OWASP ASVS for verification, and RFC 8725 for JWT best practices.

Shipping a product? Use Better Auth instead — it covers OAuth, passkeys, MFA, rate limiting, and more out of the box with an active plugin ecosystem. This repo exists to teach you how auth works, not to replace a production library.

  • Read the code, not just the docs — every security property (timing-safe rejection, session-linked revocation, algorithm pinning) is implemented and tested, not just described
  • NIST + OWASP + RFC references throughout — learn the why behind each decision
  • 250+ tests including attack-vector suites (token tampering, algorithm confusion, unicode edge cases)
  • Built for the edge — runs on Cloudflare Workers with Web Crypto API, no Node.js dependencies
  • Apache-2.0 — fork it, teach with it, learn from it
Layer What it does
Password storage PBKDF2-SHA384 with 128-bit salts, integrity digest, version tracking (password-service.ts)
Session management Server-side sessions with device tracking, sliding expiration, max-3-per-user enforcement (session-service.ts)
JWT dual-token pattern 15-min access + 7-day refresh tokens, session-linked for revocation (token-service.ts)
Auth middleware Automatic refresh flow, explicit HS256 pinning, typ claim validation (require-auth.ts)
Secure cookies HttpOnly, Secure, SameSite=Strict, Path=/ (cookie.ts)
Security headers HSTS, CSP, CORP/COEP/COOP, Permissions-Policy, fingerprint removal (security.ts)
Input validation Zod schemas with NIST-compliant password policy (length only, no complexity rules)
Attack-vector tests JWT tampering, algorithm confusion, type confusion, unicode edge cases, info-disclosure checks

This project intentionally omits features that are outside its educational scope. If you're extending this code toward production (or evaluating what a production auth system requires), the tables below organize the gaps by priority tier.

For most real-world projects, use Better Auth instead of building these yourself.

Critical — Add Before Real Users

Feature Why It Matters Standard / Reference
Rate limiting Prevents brute-force login and credential-stuffing attacks OWASP ASVS V2.2.1
Account lockout / throttling Slows automated attacks without full rate-limiting infra NIST SP 800-63B §5.2.2
Password change endpoint Users cannot recover from compromised credentials without it OWASP ASVS V2.1.6
Breached-password checking Prevents use of passwords known to be in public breach dumps NIST SP 800-63B §5.1.1.2, HIBP API

High Priority — Production Confidence

Feature Why It Matters Standard / Reference
CSRF protection (if SameSite relaxed) SameSite=Strict currently prevents CSRF; if changed to Lax for UX, an explicit token is needed OWASP CSRF Cheat Sheet
Refresh token rotation Detects token theft — if a rotated-out refresh token is replayed, revoke the entire session family RFC 6819 §5.2.2.3
aud claim in JWTs Prevents token from one service being accepted by another sharing the same secret RFC 7519 §4.1.3, RFC 8725 §3.9
Audit logging Enables incident response, anomaly detection, and compliance OWASP Logging Cheat Sheet
CSP nonces for inline scripts Current CSP uses 'unsafe-inline'; nonces eliminate inline-script XSS vectors MDN CSP script-src

Medium Priority — As Product Scales

Advanced — Enterprise / High-Security

Feature Why It Matters Standard / Reference
DPoP / token binding Binds tokens to the client's TLS connection, preventing exfiltration replay RFC 9449 (DPoP)
Multi-tenancy Isolates user pools, secrets, and policies per tenant Application-specific
Geo-fencing / IP reputation Blocks logins from unexpected regions or known-bad IPs OWASP ASVS V2.2.3
Adaptive authentication Steps up auth requirements based on risk signals (device, location, behavior) NIST SP 800-63B §6
PBKDF2 iteration upgrade or Argon2id OWASP recommends 210,000 PBKDF2-SHA512 iterations (Cloudflare limits to 100k); Argon2id is memory-hard OWASP Password Storage Cheat Sheet

All of these are excellent reasons to reach for Better Auth instead.

.
├── apps/
│   └── cloudflare-workers/    # Example Worker + Hono routes
├── packages/
│   ├── core/                  # Auth services, middleware, crypto utilities
│   ├── infrastructure/        # DB client + utilities
│   ├── schemas/               # Zod schemas
│   └── types/                 # Shared TypeScript types
└── docs/
    ├── adr/                   # Architecture Decision Records
    └── audits/                # Security audits
# Clone and install
git clone https://github.com/vhscom/private-landing.git
cd private-landing
bun install

# Build packages
bun run build

# Start dev server
bun run dev

See CONTRIBUTING.md for detailed setup and testing instructions.

This repository includes a CLAUDE.md file that provides context for AI assistants. When using Claude Code, Cursor, or similar AI-powered development tools:

  1. The AI will automatically read CLAUDE.md for project context
  2. Architecture Decision Records in docs/adr/ explain design choices
  3. Security audits in docs/audits/ document the security posture
  4. Tests demonstrate expected behavior and edge cases

The codebase is designed to be AI-readable with clear module boundaries, comprehensive types, and descriptive naming.

Apache-2.0

联系我们 contact @ memedata.com