LLM Security interview question

AI Safety & SecurityLLM SecurityLLM System Designeasy

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?

javascript
// 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();
}
  1. ASafe, because the key comes from an environment variable at build time, so it never appears in the source repository and cannot be read from the app bundle that users download.
  2. BSafe once the provider's CORS policy allows only the company's own domain, because browsers and mobile clients then refuse to send the key from any other origin or device.
  3. CUnsafe: 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.
  4. DRisky only if the key appears in server logs; minifying and obfuscating the JavaScript bundle hides the key well enough, and rotating it every quarter limits the damage from any leak.
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:

  1. The app calls your backend, authenticated as the end user (session or OAuth token).
  2. 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.
  3. 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, curl or 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.

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More AI Safety & Security questions

  1. A consultancy's responsible-AI board reviews the five incidents below. Which mapping to harm types (allocation, quality of service, stereotyping, denigration,…
  2. In this enterprise RAG pipeline, users must never receive content from documents they are not permitted to read. Where should that rule primarily be enforced?
  3. Five banks plan to train a shared fraud model with federated learning: each bank trains on its own transactions and sends only model updates to a coordinating…
  4. An e-commerce company has trained a new jailbreak classifier for its shopping assistant. It scored well on an offline labelled set, but the team worries it…
  5. A bank's research agent answers each analyst question from k = 20 retrieved web chunks and produces 500,000 answers a month. Assume 1% of retrieved chunks…