VibeSecCheck
Home/Security Guides/cloudflare-workers-security
High RiskHeaders, Auth & APIsCloudflare WorkersD1OpenNextTypeScript

Securing Cloudflare Workers, D1 & Pages Apps

Cloudflare Workers give indie SaaS apps blazing speed and generous free tiers, but common AI mistakes — string-interpolated D1 SQL queries, committing secrets into wrangler.jsonc vars, and open fetch proxies — can compromise your edge stack.

Why AI Builders Introduce This

LLMs regularly put API keys inside `wrangler.toml` `[vars]` (which gets committed to git) instead of `wrangler secret put`, and sometimes build D1 queries with template strings instead of `.bind()`.

Real-World Exploit Impact
  • SQL injection in D1 via template literals allows dumping or wiping SQLite tables
  • Secrets pasted into wrangler.toml/wrangler.jsonc leak immediately into git repositories
  • Unvalidated `fetch(userUrl)` inside a Worker creates an open proxy / SSRF vector

Vulnerable Pattern vs. Production Fix

Vulnerable Cloudflare D1 Query (SQL Injection)typescript
// VULNERABLE: String interpolation in D1 query!
const email = reqUrl.searchParams.get("email");
const user = await env.DB.prepare(
  `SELECT * FROM users WHERE email = '${email}'`
).first();
Parameterized D1 Query + Edge Security Headers
// SECURE: Always use prepared .bind() parameters in Cloudflare D1
const email = (reqUrl.searchParams.get("email") || "").trim().toLowerCase();
const user = await env.DB.prepare(
  "SELECT id, email, plan FROM users WHERE email = ?"
)
  .bind(email)
  .first();

Copy-Paste AI Fix Prompt (Cursor · Lovable · Bolt · Claude Code)

Audit this Cloudflare Workers / OpenNext project for edge security:
1. Verify every Cloudflare D1 .prepare() query uses positional ? placeholders with .bind() — zero string interpolation.
2. Audit wrangler.toml / wrangler.jsonc to confirm ZERO private secrets exist under [vars]; document them for wrangler secret put instead.
3. Validate any outbound fetch() URLs against an SSRF allowlist/private-IP guard.
4. Ensure all HTML/API responses include HSTS, X-Frame-Options, nosniff, Referrer-Policy, and explicit CORS allowlists.

How to Verify Your Fix Worked

  1. 1Inspect `wrangler.jsonc` / `wrangler.toml` and verify no API tokens or passwords appear in plaintext.
  2. 2Grep for `.prepare(`` across your Worker code to ensure no `${...}` interpolation exists.
  3. 3Scan your live Workers domain with VibeSecCheck to validate headers and bundle posture.

Frequently Asked Questions

How should I store API keys in Cloudflare Workers?

Use `npx wrangler secret put SECRET_NAME` (or Cloudflare Dashboard -> Worker Settings -> Variables & Secrets with Encrypt enabled). Never place secrets in wrangler.toml vars.

Can Cloudflare Workers run Node's node:dns for SSRF protection?

Workers don't support native OS DNS sockets by default, so always implement strict hostname/IP/protocol validation and explicit allowlists before calling fetch().

Does Cloudflare automatically add CSP or X-Frame-Options headers?

No. You must emit them from your Worker/Next.js config or Cloudflare Transform Rules.

Related Security Playbooks