A retailer's shopping app calls the LLM provider directly from the client with the code below; the key is injected from an environment variable at build time. What is the correct assessment?
// src/api/chat.js: bundled into the web and mobile app
const LLM_API_KEY = process.env.REACT_APP_LLM_KEY; // inlined at build time
export async function ask(question) {
const res = await fetch("https://api.llm-provider.example/v1/chat", {
method: "POST",
headers: {
"Authorization": "Bearer " + LLM_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({ messages: [{ role: "user", content: question }] }),
});
return res.json();
}Show the answer and explanation
Correct answer: C, Unsafe: the key ships inside the bundle, so anyone can extract it and call the API on the company's bill; route calls through a backend that authenticates users and holds the key.
Explanation LLM Security
Anything shipped to a browser or a phone is readable by its user. A build-time variable is copied into the JavaScript bundle as a string, so opening the developer tools, the network tab or the unpacked app reveals it. With the key, anyone can call the provider directly: on the company's bill, with no per-user limits, and with whatever model access the key allows.
The standard pattern:
- The app calls your backend, authenticated as the end user (session or OAuth token).
- The backend holds the provider key in a secret manager, applies per-user rate limits and quotas, input and output checks, logging, and the system prompt.
- For real-time features that must connect from the client, the backend mints short-lived, narrowly scoped tokens, which some providers offer for this purpose, instead of handing out the long-lived key.
Why the others fail:
- Environment variables protect the repository, not the bundle; the value is inlined at build time.
- CORS is enforced by browsers for web pages. A script,
curlor a mobile app ignores it, so it cannot protect a leaked key. - Minification and obfuscation slow a reader down by minutes; rotation every quarter still leaves months of exposure, and the new key would ship the same way.
Interview tip: the one-liner is "secrets never go to the client". Add that the backend proxy is also where you enforce authentication, quotas and guardrails, so it pays for itself beyond key protection.