Why I stopped putting API keys in .env for coding agents (Kiroween Hackathon 2026)

2026-12-03来源 dev.toEnglish
Why I stopped putting API keys in .env for coding agents (Kiroween Hackathon 2026)

Written as part of the Kiroween Hackathon 2026. Live demo: cryptgate.keyrook-labs.workers.dev | Source: github.com/keyrook/cryptgate

Every time I wire a coding agent up to Stripe, GitHub, or OpenAI, the quickest shortcut is pasting an API key into .env. And every time, the same nagging problem comes back: the agent now holds a long-lived, full-scope credential on disk. One poisoned GitHub issue comment, one 302 redirect to an external host, or one verbose debug endpoint that reflects headers back, and that key lands straight in tool output and logs.

For the Kiroween Hackathon 2026, I built CryptGate around a simpler setup. Keep the real key encrypted on a server (or on 127.0.0.1 locally) and give the agent a 15-minute proxy URL (/g/<id>/*) plus a matching Streamable HTTP MCP endpoint (/g/<id>/mcp). Requests are locked to specific paths and HTTP methods, can pause live until you approve them, and get scrubbed before anything comes back to the agent.

What actually changed in my workflow with Kiro

Security proxy code is annoying to iterate on with an AI assistant because tiny regressions quietly break the whole model. Usually I spend half a session re-explaining the same rules in chat: do not follow 3xx redirects, decode %252e%252e before checking paths, strip Accept-Encoding so upstream cannot send gzipped reflections, and keep zero third-party runtime dependencies in the core engine.

The most useful habit Kiro gave me was moving those rules out of chat and into versioned files inside .kiro/:

I started with .kiro/specs/cryptgate/ (requirements.md, design.md, tasks.md) to lock down the boundary first: HTTPS-only remote upstreams, SSRF checks blocking private/loopback/cloud-metadata IPs, and a pure Web API core (URL, TextEncoder, crypto.subtle) so @cryptgate/engine runs identically on Cloudflare Workers, Node 22, and the browser.

Then I pinned the non-negotiables in .kiro/steering/security-invariants.md and wired two hooks in .kiro/hooks/: one runs npm run scan:self on file save so synthetic test tokens never accidentally turn into real leaks, and another runs npm test whenever engine files change (54 Vitest tests finish in under a second). Having constraints enforced on save changed how I build. Instead of fixing security holes after writing features, I now write the invariants and tests before touching UI code.

I also packaged the repo as a Kiro Crew app (app.json, agents/, skills/, cron/, and a React UI bundle at ui/dist/index.mjs mounted at /apps/cryptgate), alongside .kiro/agents/ checked with kiro-cli agent validate. On the live site, the Kiro app tab actually renders that same ui/dist/index.mjs panel connected to the Worker.

Four real snags we hit while building it

Pausing a live HTTP or MCP request on serverless

You cannot hold a pending HTTP or MCP connection open in a normal stateless Worker and expect a click on Approve from another tab to unblock it, because the two requests can hit different edge isolates.

Routing proxy and control-plane calls through one SQLite-backed Cloudflare Durable Object (GateCoordinator) fixed that. Incoming /g/:id/* and /g/:id/mcp calls await a Promise in the Durable Object until you click Approve once, Approve 15m, or Deny, or until the 45-second timeout fails closed. Proxy configs, AES-256-GCM encrypted secrets and webhook URLs, 15-minute session approvals, traffic logs, and the ECDSA P-256 signing keypair survive hibernation in SQLite. A Durable Object alarm() fires right after TTL expiry to wipe expired ciphertext even if no new traffic ever arrives.

CryptGate live approval gate holding an MCP request on Cloudflare Durable Objects

Stopping a prompt-injected agent from approving itself

Every proxy URL reveals its gateId. If the approval API only required gateId, any agent holding /g/<id>/* could just POST to /api/gates/<id>/approvals/<ticket> and approve its own call.

When you create a proxy, CryptGate mints a one-time 256-bit operatorToken and stores it only in your browser sessionStorage (or an optional #gate=...&op=... link if you want to approve from another tab). Polling, approving, pausing, live policy or key-rotation PATCH, revoking, and receipt exports all verify X-CryptGate-Operator-Token in constant time. Callers without it get 401.

Encoded echoes and offline audit receipts

Simple string replacement misses encoded reflections, and compressed responses slip right past text inspection. CryptGate strips Accept-Encoding before dispatching upstream, then scrubs 8 encodings longest-first across both headers and body: raw, Base64, Base64URL, lowercase and uppercase hex, URL-encoded, JSON-escaped (\/, \u002b/\u002f/\u003d), and HTTP Basic (user:secret).

Every request and policy change goes into an ECDSA P-256 hash chain that you can export as JSON and verify completely offline:

node bin/cryptgate.mjs verify-audit cryptgate-live-receipt.json # [cryptgate] VERIFIED (ecdsa-p256:5a547ecf4287) - All 3 entry hashes and ECDSA P-256 signatures verified intact. 

The free-tier Durable Object bug we caught right before wrapping up

The funniest (and luckiest) catch happened while taking final screenshots on the live Cloudflare deployment. POST /api/gates suddenly started returning HTTP 500 (Error: Exceeded allowed volume of requests in Durable Objects free tier).

The culprit was boring and obvious once wrangler tail printed the error: leaving 4 or 5 browser tabs open with a 750ms status poll running even when tabs were backgrounded or proxies had expired burned straight through Cloudflare's 100,000 requests/day Durable Objects free limit.

Fixing it took two changes. First, both the web UI and Kiro Crew panel now use visibility-aware adaptive polling (0 requests when document.hidden or after expiry/revoke, local client-side 1s timers for countdowns, 4s idle polling, and 1s only while an approval is in flight), which cuts daily DO calls by over 99%. Second, worker/src/index.ts catches Durable Object free-tier quota errors and falls back to an isolate coordinator instead of throwing Cloudflare 1101.

Try it yourself

You can test the hosted version with zero signup at cryptgate.keyrook-labs.workers.dev by clicking Run live walkthrough (it fires a forged-token 401, a %252e%252e traversal 403, a read-only DELETE 403, and pauses an MCP call for your approval before scrubbing 6 echoed copies of the test key).

If you want keys to stay strictly on your own machine, run npx cryptgate serve (127.0.0.1:4173), or check the MIT source at github.com/keyrook/cryptgate. It also plugs into our open-source @keyrook/passhold-cli for local vault grants.

原文:https://www.a8q9.cn/en/article/why-i-stopped-putting-api-keys-in-env-for-coding-agents-kiroween-hackathon-2026-c0087e.html

相关文章