Skip to content
ScanAnt

Stripe secret key exposed: what to do right now

Short answer

Roll the key in the Stripe Dashboard immediately: a secret key (sk_live_ or sk_test_) lets its holder create charges, issue refunds and read customer data. Then review recent activity, move the new key to server-side code only and switch to a restricted key with the minimum permissions. The publishable key (pk_) is meant to be public and is not the problem.

Why you got this

Stripe has two kinds of keys. The publishable key starts with pk_ and belongs in your browser code. The secret key starts with sk_ and must stay on your server. A secret key ends up in public when it is placed in a front-end variable, committed to a repository, pasted into a client component by an AI assistant or left inside a mobile app.

What an attacker can do depends on the key's permissions. A full secret key can read customer and payment details, create and refund payments and change settings. Card numbers themselves are not exposed, because Stripe does not give them out, but the rest is serious.

What to do now

  1. In the Stripe Dashboard open Developers, then API keys, and roll the exposed secret key. You can set an expiry for the old key so you have time to deploy the new one, but keep that window short.

  2. Put the new key in your server's environment variables and redeploy. Check that nothing in the browser bundle or repository still contains the old one.

  3. Review Payments, Customers, Refunds and the Events log for the period the key was public. Look for charges, refunds, payouts or customers you did not create, and for changed webhook endpoints.

  4. If you see anything suspicious, contact Stripe support and tell them the key was exposed. Do not wait for it to resolve itself.

How to fix it properly

Create the Stripe client on the server only, and read the key from the environment.

ts
// server only, for example an API route
import Stripe from 'stripe';

export const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);

Replace the full key with a restricted key that has write access only to what that code needs, for example creating Checkout Sessions. A leak of a restricted key does less damage. Create it under Developers, API keys.

Verify webhook signatures, so nobody can forge a payment confirmation by calling your endpoint. Read the raw body, not parsed JSON.

ts
const sig = req.headers.get('stripe-signature')!;
const body = await req.text();
const event = stripe.webhooks.constructEvent(body, sig, process.env.STRIPE_WEBHOOK_SECRET!);

Never decide the price or the plan in the browser. Send a product ID and let the server look up the price. Turn on secret scanning in your repository so the next slip is blocked.

How to check it is fixed

Search your repository history and your deployed JavaScript for the string sk_live and sk_test. Open your site's sources in the browser developer tools and confirm nothing starting with sk_ is present.

The free ScanAnt scan checks the JavaScript your site ships for Stripe secret keys among other provider keys, reports them masked and never uses or stores them whole. It also looks for prices and paid access decided in the browser. It cannot see your server code or private repository.

Questions

Is the pk_ publishable key a leak?

No. It is designed to appear in front-end code and can only do limited things like creating tokens.

Does rolling the key break my site?

Payments fail until the new key is deployed, so update your environment right after rolling. A short expiry window on the old key can smooth the switch.

Could someone steal card numbers with the key?

Stripe does not return full card numbers through the API, but a leaked key can still expose customer data and move money, so treat it as serious.

Should I use test mode keys while developing?

Yes. A leaked sk_test_ key cannot move real money, though you should still replace it.

Check your site after the fix

The quick scan is free for any public site and needs no signup. Checks that touch your database or files run only after you prove you own the site.

Choose a scan level →