In short
- Only the publishable key (
pk_) may be in your frontend. Secret (sk_) and restricted (rk_) keys stay on the server, and the webhook signing secret too. - Stripe recommends restricted keys instead of unrestricted secret keys, including for any key you hand to an AI agent.
- The amount to charge must be decided on your server, from a Price ID or your own price list. A price sent by the browser can be edited by the buyer.
- If a secret or restricted key is exposed, rotate it immediately, even with no sign of use. Stripe's advice is to treat any exposure as a potential compromise.
- A redirect to your success page is not proof of payment. Grant access from a verified webhook or a server-side check of the session.
On this page
- What Stripe secures, and what stays with you
- Which Stripe keys can be public, and which never
- How to check your own app for a leaked Stripe key
- Use restricted keys, one per job
- Never trust the price from the client
- Know that someone paid: webhooks, signed
- What to do when a Stripe secret key leaks
- What Keybrum tests around Stripe
- Questions
- Sources
What Stripe secures, and what stays with you
When you use Stripe Checkout or Elements, the card number goes from the buyer's browser to Stripe. Your code never sees it. That removes the hardest part of taking payments from your plate.
What remains is the code around the payment, and that is where products built fast get hurt. There are five places to look: where the keys live, what each key can do, who decides the price, how your app learns that someone paid, and what you do on the day a key leaks. None of them needs a security background. Each one is a setting or a few lines.
Which Stripe keys can be public, and which never
| Key | Prefix | May be in the browser | What it can do |
|---|---|---|---|
| Publishable | pk_live_, pk_test_ | Yes | Identifies your account to Stripe.js, Elements and the mobile SDKs, and creates tokens from payment details. It cannot create charges or read account data. |
| Restricted | rk_live_, rk_test_ | No | Only what you allowed: each resource is set to None, Read or Write. |
| Secret | sk_live_, sk_test_ | No | Everything on the account. Stripe advises against it for new uses and recommends moving to restricted keys. |
| Webhook signing secret | whsec_ | No | Lets your server confirm that an event really comes from Stripe. It is a per-endpoint secret, separate from API keys. |
Stripe's wording is direct: only publishable keys can be safely exposed outside your application's backend. Finding pk_live_ in your JavaScript is normal. Finding sk_live_, rk_live_ or whsec_ there is a leak, whatever the reason the coding agent had for putting it in a NEXT_PUBLIC_ or VITE_ variable. The guide on API keys exposed in the frontend explains how those variables end up in the bundle.
How to check your own app for a leaked Stripe key
Build your app the way production does, then search the output. Anything the build wrote into the client folder is downloaded by every visitor.
# From your project folder, after a production build.
# Adjust the folders to your framework: dist, build, .next/static, out.
grep -rEo "(sk|rk)_(live|test)_[A-Za-z0-9]{8}|whsec_[A-Za-z0-9]{8}" dist .next/static 2>/dev/null | sort -u- No
sk_,rk_orwhsec_value in the built client files - No Stripe secret in a variable whose name starts with
NEXT_PUBLIC_,VITE_orPUBLIC_ - No Stripe key in the repository, including old commits and
.envfiles that were committed once - No API route or error page that prints environment variables
- The Stripe SDK is initialised only in server files
Use restricted keys, one per job
A restricted key works like a secret key in your server code, with one difference: it carries only the permissions you gave it. If it leaks, the holder can do what the key allows and nothing more. Stripe now presents restricted keys as the preferred type and says to use them whenever possible.
- 1
Create the key
On the API keys page of the Dashboard, click Create restricted key. Every permission starts at None.
- 2
Grant what the code uses
Search your code for Stripe calls and map each to a permission. A route that creates Checkout Sessions needs write access to Checkout Sessions. A page that lists invoices needs read access to invoices. Write includes read.
- 3
Try it in a sandbox first
Run the app with a test restricted key. A missing permission shows as a 403 in the key's request logs, and the error names the permission to add.
- 4
Swap the live key, then retire the old one
Replace the secret key in your server environment with the restricted key, confirm payments work, then rotate or expire the old secret key.
Two more settings are worth ten minutes. Access policies let a key work only from given IP addresses, or only from a cloud provider and country, and Stripe recommends them on all live keys. And if you give a key to a coding agent or an automation, Stripe lets you mark a restricted key as an agent key when you create it. Sensitive actions such as refunds and payouts then wait for a reviewer's approval.
Never trust the price from the client
This is the bug that costs money without any key leaking. The checkout button sends your server a request, and the request body contains the amount. Anyone can edit a request body with the browser's developer tools. If the server passes that amount to Stripe, the buyer chooses the price.
Stripe's Checkout guide says to always keep sensitive information about your product inventory, such as price and availability, on your server to prevent customer manipulation from the client. In practice the browser sends what the buyer wants, and the server decides what it costs.
// Leaks: the browser sets the amount
const { amount } = await req.json();
await stripe.checkout.sessions.create({
mode: "payment",
line_items: [{ price_data: { currency: "eur", unit_amount: amount,
product_data: { name: "Pro plan" } }, quantity: 1 }],
success_url: `${origin}/success`,
});
// Holds: the browser names a plan, the server owns the price
const PRICES = { pro: process.env.STRIPE_PRICE_PRO, team: process.env.STRIPE_PRICE_TEAM };
const { plan } = await req.json();
const price = PRICES[plan as keyof typeof PRICES];
if (!price) return new Response("Unknown plan", { status: 400 });
await stripe.checkout.sessions.create({
mode: "subscription",
line_items: [{ price, quantity: 1 }],
success_url: `${origin}/success?session_id={CHECKOUT_SESSION_ID}`,
});To test it on your own app, use a sandbox. Open the developer tools, start a checkout, and resend the request to your checkout route with a different amount, plan or quantity. If the Stripe page shows the number you typed, the server trusts the client. The same goes for a userId in the body: the server should take the user from the session and ignore the field. See IDOR and broken access control.
Know that someone paid: webhooks, signed
The success page is a URL. Anyone who knows it can open it, so it cannot be what unlocks the paid plan. Stripe's guide tells you to wait until payment has completed before fulfilling the order. There are two sound ways to know: a webhook event whose signature you verified, or a server-side retrieval of the Checkout Session where you check its status.
A webhook route that skips the signature check accepts a fake "payment completed" event from anyone who finds the URL. The fix is one function call and the raw request body. The Stripe webhook security guide has the code for Next.js and Express, the replay and idempotency traps, and a test you can run on your own route.
What to do when a Stripe secret key leaks
Stripe's guidance is to rotate immediately, even if you are not sure the key was seen. It separates exposure, where the key was visible somewhere it should not be, from compromise, where there are signs of use. Both get the same first step.
- 1
Rotate the key
On the API keys page, open the menu next to the key and choose Rotate key. Rotation revokes the key and creates a replacement. For a key that leaked, set the expiration to Now: the grace period of up to 7 days is meant for planned rotations.
- 2
Put the new key where the old one should have been
A server environment variable or your host's secret store. If the old key leaked through the bundle, fix the variable name before you deploy, or the new key leaks the same way.
- 3
Read the key's request logs
From the same menu, View request logs shows every call made with the key. Look for requests you cannot match to your servers.
- 4
Undo what was done
Stripe's support page lists the checks: connected accounts, payouts or bank accounts you do not recognise, fraudulent charges to refund, payouts to reverse. Contact Stripe support if money moved.
- 5
Rotate the other keys
Stripe advises rotating all other secret and restricted keys as well, and the webhook signing secret if it sat in the same file. Then confirm that the old key fails.
The leaked API key guide gives the same order for other providers, and explains why deleting the key from your code does not un-leak it.
What Keybrum tests around Stripe
Keybrum is the automated pentest we build for apps made with AI. It tests your live app from the outside, as a stranger and as two signed-in users. On Stripe that means three things:
- Stripe keys your app exposes. It looks for
sk_andrk_keys, live or test, in what the app serves. When it finds one, it checks whether the key works with one free read of the balance. The finding says whether the key is live, valid but restricted, or already rejected. It also flags awhsec_signing secret found in the same places. - Unsigned webhooks. It sends one forged event with no signature to routes that look like Stripe webhooks. The event's identifiers match nothing, so it cannot confirm a real payment. A route that answers with a success status gets a finding. A route that refuses it is recorded as blocked.
- Fields the client should not set. On write routes it sends values such as
price=0andplan=enterprisewith a marked test object, then reads the object back. A finding requires the privileged value to be stored. The test object is deleted afterwards.
What it does not test: anything inside your Stripe account. It does not log in to your Dashboard, review a restricted key's permissions or check your access policies. It never makes a payment, so it does not go through a real checkout to prove that your checkout route ignores an amount sent by the browser. Run the sandbox test above for that. It only attacks apps whose owner proved ownership.
A one-time report costs €49. The Maker plan is €29 a month excluding VAT for 5 apps, and attacks again on every deploy.
Questions
Is the Stripe publishable key safe to expose?
Yes. Stripe's documentation says publishable keys can be included in frontend code and in apps you distribute. The key identifies your account and creates tokens from payment details. It cannot create charges or read account data.
What happens if my Stripe secret key is leaked?
An unrestricted secret key has full access to the Stripe API for your account. Stripe says someone holding it could make fraudulent charges, access customer data or disrupt your integration. Rotate it at once and review the key's request logs.
How do I rotate a Stripe API key?
On the API keys page of the Dashboard, open the menu next to the key, choose Rotate key and pick an expiration. Now deletes the old key immediately. A later time, up to 7 days, keeps both keys working while you deploy the new one.
What is a Stripe restricted API key?
A key that starts with rk_ and carries only the permissions you assign: None, Read or Write for each resource. It replaces a secret key in server code. Stripe recommends restricted keys over unrestricted secret keys because a leak is limited to those permissions.
Can a customer change the price in Stripe Checkout?
Not on Stripe's page. The risk is before it: if your server creates the Checkout Session with an amount taken from the browser's request, the buyer can edit that request. Decide the price on the server from a Price ID or your own list.
Is a test mode Stripe key a problem if it leaks?
It cannot move real money, but it should still be rotated and it tells you the live key would leak the same way. Stripe's best practices ask you to scan for test and sandbox keys as well as live ones.
Does Stripe tell me if my key is exposed?
Sometimes. Stripe says it notifies you when it detects an exposed secret or restricted key, and may disable the key in some cases. It also states that it does not guarantee detecting every exposed or compromised key.
Sources
- 1.API keys, Stripe Docs
- 2.Best practices for managing secret API keys, Stripe Docs
- 3.Restricted API keys, Stripe Docs
- 4.Protecting against compromised API keys, Stripe Support
- 5.Stripe-hosted page: Checkout quickstart, Stripe Docs