critical · M1 · MASVS-STORAGE-1 · PTKD-LEFTOVER-PUBLIC-ENV-SECRET

    Secret key in a variable that gets built into the app

    A variable with a secret-looking name is exposed to the app bundle (EXPO_PUBLIC_*, NEXT_PUBLIC_*, VITE_*, or a library that inlines every .env value). Its value is compiled into the app as plain text, readable by anyone who downloads it.

    How it is exploited

    The app calls OpenAI directly with EXPO_PUBLIC_OPENAI_API_KEY. Expo inlines every EXPO_PUBLIC_ variable into the JavaScript bundle, so the key sits in index.android.bundle. Someone unzips the app, copies the key and runs their own workload on your account until the card limit is hit.

    Why it matters

    Direct financial loss (AI, SMS, email and cloud bills), data theft with service-role or admin keys, and account bans from the provider.

    How to fix it

    1. Rotate the key now: it is already public in every installed copy of the app.
    2. Create a server endpoint (Supabase Edge Function, Firebase Function, Vercel/Cloudflare route) that holds the key and makes the call.
    3. Have the app call your endpoint with the user's session, and rate-limit it per user.
    4. Keep only public keys (Supabase anon key with RLS, Stripe publishable key) in EXPO_PUBLIC_ variables.

    Supabase Edge Function holding the key

    Deno.serve(async (req) => {
      const { prompt } = await req.json();
      const r = await fetch("https://api.openai.com/v1/responses", {
        method: "POST",
        headers: { Authorization: `Bearer ${Deno.env.get("OPENAI_API_KEY")}`, "Content-Type": "application/json" },
        body: JSON.stringify({ model: "gpt-4.1-mini", input: prompt }),
      });
      return new Response(r.body, { headers: { "Content-Type": "application/json" } });
    });

    Does your app have this?

    PTKD checks your build or your source project for PTKD-LEFTOVER-PUBLIC-ENV-SECRET and every other check in this knowledge base.

    Scan your app

    References

    Related checks (M1)