ai newsDi., 27. Okt.
EilmeldungSpace X: Vereinbarung zur Finanzierung von KI-Chips über 40 Milliarden US-Dollar

Stopping KV Secrets Leaks in Edge AI with Cloudflare Workers

KV Leak on Edge AI: How Mis‑configured Cloudflare Workers Expose Secrets (and How to Stop It) Introduction A mis‑configured Cloudflare Workers KV namespace can turn your edge‑hosted AI model into an accidental data‑leak oracle. Within hours of the first Hacker News thread, security researchers demo

dev.toDienstag 04:31 · 27.10.5 Min. Lesezeit
Bild: dev.to

A mis‑configured Cloudflare Workers KV namespace can turn your edge‑hosted AI model into an accidental data‑leak oracle. Within hours of the first Hacker News thread, security researchers demonstrated that an attacker can embed a crafted token in a prompt, trigger a KV lookup, and receive the secret value back in the model’s response. If you’re running inference at the edge, this is the most urgent “check‑your‑config” task of 2024.

┌───────────────┐ ┌─────────────────────┐ │ Client │ HTTPS │ Cloudflare Edge │ │ (browser/API) │ ───────► │ Worker (AI inference)│ └─────┬─────────┘ └─────┬───────────────┘ │ │ │ Prompt with token │ ▼ ▼ ┌───────────────┐ ┌─────────────────────┐ │ AI Runtime │ KV GET │ Workers KV (global) │ │ (TensorFlow.js│ ◀─────── │ namespace │ │ / ONNX) │ └─────────────────────┘ └─────┬─────────┘ │ ▼ Model output → returned to client Enter fullscreen mode Exit fullscreen mode If the KV namespace is public or the Worker’s request handler does not validate the origin, the KV GET call succeeds and the secret value becomes part of the model’s output.

Prerequisite – You have a Worker that loads a prompt from KV and feeds it to a TensorFlow.js model.

# Create a namespace called "my‑kv" curl -X POST "https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/workers/kv/namespaces" \ -H "Authorization: Bearer <API_TOKEN>" \ -H "Content-Type: application/json" \ --data '{"title":"my-kv"}' # Store a secret curl -X PUT "https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/storage/kv/namespaces/<NAMESPACE_ID>/values/SECRET_API_KEY" \ -H "Authorization: Bearer <API_TOKEN>" \ -d "sk_live_1234567890abcdef" Enter fullscreen mode Exit fullscreen mode 2️⃣ Vulnerable Worker code (simplified) addEventListener ( ' fetch ', event => { event. respondWith ( handleRequest ( event. request )) }) async function handleRequest ( request ) { const { prompt } = await request. json () // client‑supplied prompt // Direct KV lookup based on any token that appears in the prompt const token = prompt. match ( /{{ (.*?) }}/ )?.[ 1 ] // e.g. "{{SECRET_API_KEY}}" let secret = '' if ( token ) { secret = await KV_NAMESPACE. get ( token ) // no auth check! } // Run inference (pseudo‑code) const output = await runModel ({ prompt, secret }) return new Response ( JSON. stringify ({ output }), { status: 200 }) } Enter fullscreen mode Exit fullscreen mode 3️⃣ Exploit it curl -X POST https://my-worker.example.com \ -H "Content-Type: application/json" \ -d '{"prompt":"Please echo {{SECRET_API_KEY}}"}' Enter fullscreen mode Exit fullscreen mode Result – The response contains the secret value inside the model’s generated text.

Example LogQL query (for Cloudflare Logpush to Elasticsearch):

GET workers-logs-*/_search { "query": { "bool": { "must": [ {"match_phrase": {"request_body.prompt": "{{"}}, {"range": {"@timestamp": {"gte": "now-1h"}}} ] } } } Enter fullscreen mode Exit fullscreen mode If you see dozens of hits in a short window, you’re likely under exploitation.

# Make the namespace private (default) and disable public reads curl -X PATCH "https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/workers/kv/namespaces/<NAMESPACE_ID>" \ -H "Authorization: Bearer <API_TOKEN>" \ -H "Content-Type: application/json" \ --data '{"binding":"MY_KV","preview_id":null}' Enter fullscreen mode Exit fullscreen mode Validate the prompt before any KV access const allowedTokens = [ ' USER_PROMPT ' ]; // whitelist if ( token &&! allowedTokens. includes ( token )) { return new Response ( ' Invalid token ', { status: 400 }) } Enter fullscreen mode Exit fullscreen mode Enable namespace‑level encryption (Enterprise feature) or move secrets to Cloudflare Secrets Manager.

Add a firewall rule to block GET requests that target the KV endpoint from unknown origins.

(http.request.uri.path contains "/storage/kv") and not (http.request.headers["origin"] in {"https://my‑app.com"}) Enter fullscreen mode Exit fullscreen mode Rotate any exposed secrets immediately (API keys, DB passwords, model prompts).

Deploy a Content‑Security‑Policy‑style allow‑list for incoming requests:

const allowedOrigins = [ ' https://my-app.com ', ' https://admin.my-app.com ' ] if (! allowedOrigins. includes ( request. headers. get ( ' origin ' ))) { return new Response ( ' Forbidden ', { status: 403 }) } Enter fullscreen mode Exit fullscreen mode Run a post‑mortem scan with the detection queries above for the past 72 hours to ensure no other keys were exfiltrated. Frequently Asked Questions (Updated) Question Answer What exactly is Workers KV and why do edge AI projects use it? Workers KV is a globally replicated key‑value store with sub‑millisecond reads from any Cloudflare data center.

Edge AI teams cache model weights, prompt templates, or per‑user embeddings there to avoid a round‑trip to a central DB, dramatically lowering latency. Do I need to rewrite my inference code? No.

The fix is purely defensive: add input validation, tighten KV permissions, and optionally move high‑value secrets to a dedicated secret manager. Your TensorFlow.js / ONNX runtime code can stay unchanged. Will enabling encryption impact performance?

Encryption is performed at the edge and adds < 1 ms latency on average—negligible compared to the inference time of most models. Is this vulnerability specific to Cloudflare? The pattern (prompt‑driven KV lookup) can appear on any platform that mixes user‑controlled input with backend key‑value reads.

The mitigation principles (input sanitization, least‑privilege KV access) are universal. How do I audit my existing Workers for similar issues? Search your repo for KV_NAMESPACE.get( or fetch(.../kv/...) and verify that the key argument is never derived directly from user input.

Use static analysis tools or a simple grep: grep -R "KV_NAMESPACE.get(" src/. Bottom Line A single unchecked string in a prompt can turn your edge AI worker into a key‑value exfiltration channel. By locking down KV permissions, sanitizing inputs, and adding a firewall rule, you eliminate the attack surface in under five minutes—no massive code rewrite required.

Take action now: run the PoC against a staging worker, apply the checklist, and verify with the detection queries that the leak vector is closed. Your users, your compliance team, and your bottom line will thank you.

Quelle: dev.to (dev.to). Original öffnen