Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.5-flash-lite-preview-06-17-thinking· GoogleHugging Face LMArena Human Preference 140K

What are the best practice approaches to have an web service that uses some end user…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with chatgpt-4o-latest-20250326’s answer.
  1. User

    What are the best practice approaches to have an web service that uses some end user secret api keys in its logic? E.g. llm api keys provided by user? Im thinking about storing the keys only on the client side (e.g. local storage) and just send them to the backend actions that require it, so not storing anything on server side

  2. Gemini Assistant

    Okay, let's break down the approach of storing user API keys client-side (like in Local Storage) and sending them to the backend only when needed.

    Analysis of the Client-Side Storage Approach

    • Mechanism: User enters API key -> Stored in browser's Local Storage -> JavaScript on your site retrieves it -> JavaScript sends the key along with the request to your backend -> Your backend uses the key to call the third-party API (e.g., LLM API).

    • Security Risks: This approach is highly discouraged and generally considered insecure for sensitive credentials like API keys. Here's why:

      1. Cross-Site Scripting (XSS) Vulnerability: Local Storage is accessible by any JavaScript running on the same origin (your website's domain). If your website has even a minor XSS vulnerability, an attacker can inject malicious JavaScript to steal the API keys stored in Local Storage. Once stolen, the attacker can use them with the third-party service, potentially incurring costs or accessing data under the user's account.
      2. Exposure During Transit (Client-to-Backend): While the key needs to be sent to your backend anyway, storing it client-side means it resides in the user's browser environment, which is less secure than a well-managed server environment. The transmission itself must be over HTTPS, but the fundamental problem is its residence in the browser.
      3. User Environment Compromise: If the user's computer itself is compromised (malware, keyloggers), the keys stored locally can be accessed directly, bypassing your website's security entirely.
      4. Limited Utility: You still need your backend to orchestrate the call to the third-party API. The key has to be sent to your backend at some point to be used. If you're sending it to your backend, the question becomes: why not manage it more securely there?
    • Conclusion on Client-Side Storage: Relying on client-side storage (Local Storage, Session Storage, IndexedDB, or even just in-memory variables) for sensitive API keys that your backend needs to use is not a best practice. It shifts the security burden entirely onto the client, which is a very difficult environment to secure reliably.

    Best Practice Approaches

    The core challenge is that your web service needs to act on behalf of the user using their credentials for a third-party service, without compromising those credentials.

    1. Secure Server-Side Storage (Most Common & Recommended):

      • How it works:
        • User submits their API key through a secure form on your website (HTTPS POST).
        • Your backend receives the key.
        • Your backend validates the key immediately (e.g., by making a test call to the third-party API).
        • If valid, your backend securely stores the key in your database, encrypted at rest (e.g., using AES-256 encryption). The encryption key itself should be managed securely (e.g., via environment variables or a dedicated secrets management service).
        • When the user performs an action requiring the key, your backend retrieves the decrypted key from the database, uses it to call the third-party API, and then potentially discards the decrypted key from memory immediately after use.
      • Pros: Centralized security management, keys are protected server-side, user doesn't need to re-enter the key constantly.
      • Cons: Your service provider does hold the keys (requires trust), necessitates robust server-side security measures (encryption, access control).
      • Security: Use strong encryption, secure key management for your encryption keys, implement strict database access controls, and ensure your servers are hardened.
    2. OAuth 2.0 / Token-Based Delegation (Ideal if Supported):

      • How it works:
        • Instead of asking for an API key, your service facilitates an OAuth flow with the third-party service.
        • The user authorizes your service to access the third-party service on their behalf, usually via a redirect to the third-party provider.
        • The third-party provider issues an access token (and possibly a refresh token) to your service.
        • Your backend securely stores these tokens (often encrypted), not the user's original API key.
        • Your backend uses the access token to call the third-party API.
      • Pros: Most secure method, user never exposes their raw API key to your service, user can revoke access granularly without affecting other services, leverages standardized protocols.
      • Cons: Requires the third-party service to support OAuth, can be more complex to implement. Not suitable if the user must use their specific key for billing or quota reasons tied directly to their account.
    3. Using API Keys Only for Initial Validation/Setup:

      • How it works:
        • User provides their key client-side.
        • Key is sent to backend via HTTPS.
        • Backend uses the key only to call a specific, limited endpoint on the third-party service to generate a short-lived, scoped token or profile identifier.
        • Backend stores this derived token/identifier (securely, maybe encrypted), discarding the original user key.
        • Backend uses the derived token for subsequent operations.
      • Pros: Reduces the time the actual user API key is handled and stored.
      • Cons: Still requires initial secure handling of the user key, relies on third-party support for generating such tokens/identifiers, adds complexity.

    Recommendations

    • Avoid Client-Side Storage: Do not rely on Local Storage or similar client-side mechanisms for storing sensitive API keys. The risk of theft via XSS is too high.
    • Prefer Server-Side Storage: If OAuth isn't an option, the standard and most practical approach is secure, encrypted server-side storage. This is the industry norm for managing user credentials or secrets securely.
    • Use HTTPS: Absolutely mandatory for any transmission of sensitive data, including API keys, between client and server.
    • Input Validation & Sanitization: Always validate and sanitize any input, including API keys, on the server-side.
    • Rate Limiting: Implement rate limiting on your API endpoints to prevent abuse, regardless of how keys are handled.
    • Consider Third-Party Key Management: Services like HashiCorp Vault can help manage secrets securely, both for your service and potentially for storing user secrets (though integrating user secrets directly might still require careful design).

    In summary, while your idea avoids persistent server-side storage, it introduces significant client-side security vulnerabilities. The best practice involves securely managing the keys on the server or, ideally, using delegation mechanisms like OAuth.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.