Put a chatbot on a public page and you have published an endpoint that spends your money. Anyone can send it a message, every message is billed to your OpenAI project, and a script can send thousands of them.

askdare's threat model starts from one admission: an anonymous public bot can't be made unforgeable. A determined attacker can always use it as a rate-limited LLM. So the goal is narrower and reachable. Bound the damage in requests, tokens and money per day, and stop the attacker from escalating to data, scripts or other people's conversations.

Below are the threats I'd check in any public chatbot, whatever it's built with, and then what askdare does about each one by default.

The client chooses what it shouldn't

The cheapest attack is a field in the request body. If the browser sends the model name, an attacker sends the most expensive one. If it sends instructions, a vector store ID or a conversation ID, they send their own or try someone else's.

This isn't hypothetical for us. askdare's predecessor, @boldare/openai-assistant, let the client override assistantId, reflected any origin in CORS and had no authentication or rate limits. ADR-0004 calls it "effectively an open, paid proxy".

In askdare the client never chooses the model, instructions, tools, vector stores, raw file_id values or OpenAI conversation IDs. All of them live in defineBot on the server. The browser gets only signed handles. The resume token that carries a conversation ID is encrypted, bound to the bot and the visitor, and a mismatch answers 404. Uploaded files come back as encrypted handles that expire after an hour, and raw file IDs are refused.

Scripts that drain tokens

Once the request can't pick the price, the attacker goes for volume. Rate limits slow one visitor down. Budgets decide what a bad day costs.

askdare has both. By default each bot allows 10 messages per visitor per minute, 30 per IP, 50 turns per conversation and 1,024 output tokens per model call. On top of that sit daily token budgets: 2,000,000 tokens per bot and 200,000 per client IP, counted per UTC day. With several bots, globalDailyTokens caps them together:

defineBot({ // … limits: { dailyTokens: 1_000_000, dailyTokensPerIp: 50_000 }, }); export const { GET, POST, DELETE } = createChatHandler({ bots, limits: { globalDailyTokens: 3_000_000 }, }); 

Two details matter more than the numbers. First, each turn reserves an estimate against every budget before the model is called, and the real usage replaces it afterwards. A turn that fails or times out keeps the estimate charged. Second, a turn whose model call has started runs to the end even if the visitor disconnects, because OpenAI keeps no record of an aborted streamed response, usage included. Disconnecting mid-answer hides nothing from the counter.

Counters live in memory by default, per instance. On Vercel or with several Heroku dynos, every instance would allow the full budget, and a deployment would reset them. Use a shared store, and make production refuse to start without one:

import { upstashStore } from '@boldare/askdare-upstash'; export const { GET, POST, DELETE } = createChatHandler({ bots, limits: { store: upstashStore(), requireSharedStore: process.env.NODE_ENV === 'production' }, }); 

Per-IP limits have a hole: they bound what one address can spend, not what many can. Ten addresses use up a bot's default daily budget. For a public chat that attracts scripted traffic, askdare can ask for a Cloudflare Turnstile check when a session starts:

import { defineBot, turnstile } from '@boldare/askdare'; defineBot({ // … security: { allowedOrigins, // The site key is public; the secret key comes from TURNSTILE_SECRET_KEY. humanVerifier: turnstile({ siteKey: '0x4AAAAAAA…' }), }, }); 

The token is verified on the server, and if Cloudflare can't be reached the session is refused, so the check fails closed. A passed check covers session renewals for 24 hours, and renewing can't stretch that. The trade-off is trust. Cloudflare's script runs inside the chat with your app's access, without Subresource Integrity, because Cloudflare updates it in place. That's why it's off by default.

Other sites embedding your widget

If your widget loads on any page, anyone can put your bot on their site and let you pay for their visitors.

security.allowedOrigins lists the exact origins allowed to embed the chat, such as https://www.example.com. No paths, no wildcards. Your app's own origin is always allowed. The browser enforces it through the chat page's frame-ancestors policy.

frame-ancestors stops browsers. It doesn't stop a script calling the API directly, which is why the limits and budgets above still matter. askdare also uses no cookies: requests carry Bearer tokens and JSON bodies that need a CORS preflight it never grants.

Forged or replayed sessions

If session tokens are guessable, unsigned or long-lived, the attacker mints their own or reuses one forever.

askdare issues two tokens, both encrypted JWE with keys derived from CHAT_SECRET. The access token lives 15 minutes and stays in memory. The resume token lives 30 days and carries the conversation, opaque to the client. Each token names its bot as audience, the key is looked up by its ID with no fallback, and the visitor ID is generated by the server. CHAT_SECRET must be at least 32 characters, and a short or low-entropy secret is refused. You rotate it by moving the old value to CHAT_SECRET_PREVIOUS.

The residual risk is written down too: anyone with physical access to a browser holding a resume token can continue that conversation for up to 30 days.

Spoofed client IPs

Per-IP limits are only as good as the IP. The leftmost X-Forwarded-For value is whatever the client typed.

askdare reads the client IP according to trustProxy. The default, 'auto', uses Vercel's headers on Vercel and otherwise trusts one proxy, which matches Heroku's router. Behind a CDN in front of Heroku, set it to the number of proxies. IPv6 addresses are grouped by /64. And never expose next start directly to the internet: without a proxy that sets X-Forwarded-For, clients choose their own IP and the per-IP limits stop working.

Prompt injection and leaked instructions

Assume visitors can make the model repeat its instructions, so put no secrets in them: no keys, no internal URLs, nothing you wouldn't put on the page.

askdare keeps instructions, model and tools on the server, and clients can't send system messages. Model output is rendered as Markdown without raw HTML, and images are never loaded, only their alt text, so an injected image URL can't leak the conversation to someone else's server. Three guardrails are opt-in per bot: OpenAI moderation (moderation), your own rules for messages and answers (onInput, onOutput), and toolResults: 'untrusted' for tools that return third-party text. That last one lowers the risk of planted instructions in a web page or a ticket. It doesn't remove it, so tools that act on the visitor's behalf should wait for the visitor's approval.

The OpenAI side

The last line of defence runs at OpenAI and needs no infrastructure:

  • a dedicated project for this deployment, not shared with other apps;
  • a restricted API key with only the permissions the chat needs (npx @boldare/askdare doctor names a missing one);
  • a model allowlist with only the model your bot uses;
  • a monthly budget with "Enforce a hard limit", plus email alerts at 50% and 80%.

Set the hard limit a little above what your askdare budgets allow in a month. The budgets then stop spending first, and the hard limit catches what they miss. OpenAI's docs say enforcement isn't instantaneous, so recorded spend can end slightly above the limit.

What this doesn't stop

  • An attacker rotating many IP addresses can still spend up to a bot's daily budget. Turnstile raises the cost of automation. It doesn't stop a person or a solving service.
  • The budgets count tokens only. File search fees and file storage aren't in them.
  • Guardrails fail open: a moderation outage or a hook that throws lets text through.
  • askdare works with OpenAI only for now, and runs inside a Next.js app.

The budgets are the main cost control. The OpenAI hard limit is the backstop. If you remember one thing, set both.

A checklist you can use on any chatbot

  • [ ] The client can't choose the model, instructions, tools, vector stores, file IDs or conversation IDs.
  • [ ] Rate limits per visitor and per IP, and a daily token budget per bot and per IP.
  • [ ] Counters shared across instances.
  • [ ] A human check, if the chat is public and anonymous and attracts scripts.
  • [ ] An exact list of sites allowed to embed the widget.
  • [ ] Short-lived, signed session tokens from a random secret that isn't in git.
  • [ ] Client IP read from your proxy's header, and the app never exposed without that proxy.
  • [ ] Instructions with no secrets, and output rendered without raw HTML or remote images.
  • [ ] A dedicated OpenAI project, a restricted key, a model allowlist and a hard spend limit with a tested alert.
  • [ ] A kill switch you know how to flip (in askdare, ASKDARE_DISABLED=true and a redeploy or restart).

Links

You pay OpenAI for tokens. askdare is MIT-licensed, made at Boldare, and adds no per-conversation or per-seat fee.

Sebastian Musiał (sebastianmusial.pl) builds askdare at Boldare.