Your old secret may stop working the moment you click rotate
Stripe keeps old keys alive for up to seven days and OpenAI revokes them at once by default, so code must accept both secrets before anyone rotates.
In brief
- Stripe keeps the old and new keys running together for up to 7 days. When you rotate a webhook secret through OpenAI's API, the old secret stops working immediately unless you turn on the parameter that keeps it for 24 hours.
- Verification code must accept a request when at least one signature matches one of the currently valid secrets.
- If a secret has leaked, revoke it at once and skip the overlap, because during that window the old secret still lets someone else send forged requests.
According to Stripe’s documentation, when you rotate an API key in the Dashboard, the old and new keys both keep working for up to 7 more days. OpenAI’s API for rotating webhook secrets does the opposite by default: the old secret is disabled immediately. To keep it alive for another 24 hours, you have to turn on the keep_old_secret_active_for_24_hours parameter yourself.
Two providers, two opposite defaults. Click the button out of habit and you can break a customer’s payment flow in the middle of the working day. If you own an integration like this at a client site, this is a job to get right the first time.
Stripe gives one more reason to do it regularly: routine rotation tests whether you know where every key is used, and whether the team can replace a key at short notice. This guide builds both halves: an API key rotation procedure, and a webhook handler that survives a change of secret.
What you will build, and what you need
The end result is a webhook handler that reads a list of secrets, a table that blocks duplicate processing by event ID, and a key rotation runbook a colleague can follow without help.
You need a Stripe account in test mode, a webhook endpoint running locally or on staging, and somewhere to store secrets as environment variables.
The code below is in Python and has been simplified. Header parsing and the signature formula must follow each provider’s documentation exactly, including the timestamp check. If the official SDK already ships a verification function, prefer it in production.
Step 1: Know where a key lives before you change it
Open a runbook file and list every place that reads the key: each service, cron job, staging and production environment, and where the secret is stored. This is the least glamorous step, and often the one that decides the outcome. A forgotten old worker will keep using the old key and will only fail on the day that key expires.
How to check: every row in the table must name an owner and say how the secret is updated, for example a redeploy or just a restart.
Step 2: Rotate the API key as a canary
Rotate the key in the Stripe Dashboard. For up to 7 days afterwards, both keys work. Stripe recommends giving the new key to a small group of servers or services first, watching the error logs, and only then rolling it out widely.
If the client needs more than 7 days, say because it releases monthly, follow Stripe’s suggestion: create a new key yourself, move services over one at a time, then let the old key expire when you are done.
How to check: before letting the old key expire, look at its request log. Stripe advises expiring it only once the old key’s traffic has been zero for several hours or days. If the log still shows requests, the table from Step 1 is missing a place where the key is used.
Step 3: Write a verifier that accepts several secrets
This step decides whether webhooks break during rotation. According to Stripe’s documentation, the Stripe-Signature header can contain several v1 entries, and code must accept the request when at least one of them matches. The Standard Webhooks specification also defines the signature header as a space-delimited list, and states why: to support zero-downtime secret rotation.
So the handler needs to accept several secrets on its side and several signatures in the header:
import hmac
import os
# New secret first, old secret after, separated by commas
SECRETS = [s for s in os.environ["WEBHOOK_SECRETS"].split(",") if s]
def parse_v1(header: str) -> tuple[str | None, list[str]]:
# Simplified: parse using the exact format in the provider's docs
ts, sigs = None, []
for part in header.split(","):
key, _, value = part.partition("=")
if key == "t":
ts = value
elif key == "v1":
sigs.append(value)
return ts, sigs
def verify(header: str, body: bytes) -> bool:
ts, sigs = parse_v1(header)
for secret in SECRETS:
expected = compute_expected(secret, ts, body) # HMAC formula per the provider's docs
if any(hmac.compare_digest(expected, s) for s in sigs):
return True
return False
Two mistakes are common. The first is reading only the first v1 entry in the header. The second is assuming the header always carries exactly one signature. Both work fine until the day you rotate the secret.
How to check: write a test that builds a header with two signatures, one made with the old secret and one with the new. Swap their order and run it again: the result must still be True.
Step 4: Rotate the webhook secret once the code is ready
Roll the secret only after Step 3 is running in production. Stripe lets you expire the old secret immediately or delay expiry by up to 7 days. During that window, Stripe generates one signature for each secret still valid. Choose the delay, add the new secret to WEBHOOK_SECRETS, deploy, then remove the old secret from the list before it expires.
With OpenAI, the maximum overlap is 24 hours and the default is 0. Forget the parameter and the handler will reject every webhook until the new secret is deployed.
| Provider | Overlap window | Default |
|---|---|---|
| Stripe, API key (rotated in the Dashboard) | Up to 7 days | Both keys run together |
| Stripe, webhook secret | 0 to 7 days | You choose when rolling |
| OpenAI, webhook secret | 0 or 24 hours | Revoked immediately |
| AWS Secrets Manager, alternating users | Both users valid after rotation | Depends on the strategy you choose |
Step 5: Block duplicate processing when events are resent
Say the deploy runs late and the handler returns errors for an hour. In live mode, Stripe retries delivery for up to three days with exponential backoff. Some events will arrive again, and Stripe also notes that webhooks can be delivered more than once. The fix is to record the IDs of processed events and skip any ID already seen:
def handle(event: dict) -> int:
if already_processed(event["id"]): # look up the processed_events table
return 200
process(event)
mark_processed(event["id"])
return 200
How to check: send the same event twice in a test. Side effects, such as sending an email or writing a ledger entry, must happen only once.
What about the database?
The same principle applies to database passwords. AWS Secrets Manager explains that with the single-user rotation strategy there is a short gap between the moment the password changes in the database and the moment the secret is updated. Connections can be refused during that gap, so the application needs sensible retries.
The alternating-users strategy keeps two users, and after rotation both sets of credentials remain valid. AWS recommends it for applications that need high availability. If the client runs a single user with no retries, put this in the runbook before the first rotation.
When a secret has leaked, skip the overlap
Everything above is for planned rotation. OWASP warns that if a secret is suspected of leaking, continuing to accept it during an overlap window still lets someone else send forged requests. Revoke it at once, even if the integration is interrupted until the new secret is fully deployed.
GitHub also stresses that deleting a secret from the codebase is not enough. The most important step is revoking it at the provider. After that, use Step 5 to handle the events that get resent.
What this skill looks like at a client site
In your first week with a new client, ask one question: when did the team last rotate a key, and how long did it take? If nobody can answer, the table from Step 1 becomes the first thing you deliver. The question also teaches you the real architecture faster than reading the documentation.
On a CV, avoid the vague “managed secrets”. Describe the concrete work: built a webhook handler that accepts multiple secrets, rotated production keys as a canary, with zero downtime. If an FDE job ad mentions “production integrations”, this is a story to tell in the interview.
The first key rotation usually uncovers places where a key is used that nobody wrote down. From the second one on, it becomes routine maintenance.
Was this article useful?
Thanks for the feedback!