Skip to content

Bug: AI Assistant chatbot sends API key to client, exposes it to any user #772

Description

@anshul23102

Security Bug: AI Assistant API Key Accessible in Browser Network Requests

Description

The DevPath AI Learning Assistant feature makes calls to an AI provider
(OpenAI or Gemini) from the frontend. If the API key is stored in a
NEXT_PUBLIC_ environment variable or passed through a client-side SDK
call, it appears in the browser's network requests and is visible in
DevTools to any user of the platform.

Steps to Reproduce

  1. Open the DevPath website with the AI Assistant feature.
  2. Open DevTools > Network tab.
  3. Ask a question in the AI Assistant.
  4. Find the outgoing API request and inspect the request headers or body.
  5. Observe the API key in the Authorization header or request body.

Root Cause

Client-side AI SDK calls require the API key to be present in the browser.
Using NEXT_PUBLIC_AI_API_KEY or initialising the SDK with the key in a
client component exposes it.

Impact

Any user of the DevPath platform can extract the AI API key and use it
for their own purposes, generating API costs charged to the DevPath account
and potentially exhausting the API quota for legitimate users.

Proposed Fix

Route all AI calls through a Next.js API route (server-side):

// app/api/assistant/route.ts
import { GoogleGenerativeAI } from "@google/generative-ai";

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!); // NOT NEXT_PUBLIC_

export async function POST(req: Request) {
  const { message } = await req.json();
  const model = genAI.getGenerativeModel({ model: "gemini-pro" });
  const result = await model.generateContent(message);
  return Response.json({ response: result.response.text() });
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions