My subscription endpoint would have accepted a fake Apple receipt. What the server has to check.
A few days before the iPhone app was due to launch, I read my own subscription code again, line by line. The app buys a monthly plan through Apple and sends the result to my server, and the server switches the account to Pro. The server did one thing with what the app sent: it read it. It did not check who had signed it.
The endpoint was still behind a switch that was off, so nobody could use it. Shipping the app meant turning that switch on. From that moment any signed-in user could have sent a transaction they wrote themselves and received Pro for nothing.
What the app sends
With StoreKit 2, a purchase gives the app a signed transaction. It is a JWS: three parts separated by dots, a header, a payload and a signature. The payload holds the product, the bundle id, the transaction ids and the expiry date. The header holds the certificate chain that signed it.
The payload is easy to read, and that is the trap. Anyone can produce a JWS with any payload. What they cannot produce is one signed by Apple.
The checks, in order
- Accept one algorithm only, ES256. Reject everything else, including "none". A verifier that lets the token choose its own algorithm can be talked into not verifying.
- Take the certificate chain from the header and walk it: each certificate must be signed by the next.
- Pin the top of the chain to Apple Root CA G3. Keep Apple's root certificate on the server and compare against it. A chain that is valid but ends in somebody else's root is a forgery that passes every other test.
- Check the validity dates of every certificate in the chain.
- Verify the signature over the header and the payload with the key of the first certificate.
- Only now read the payload. My code does not parse it before step 5 passes, so no later line can use a value that was never verified.
- Compare the bundle id and the product id with your own. A real receipt from another app is still a real receipt.
- Check the expiry date and the revocation date.
Two rules that are not about signatures
A genuine transaction can be used twice. One person subscribes, then sends the same signed transaction from a second account, and a third. The server now writes down which account first presented each original transaction id, and refuses it from any other.
The second rule is about time. My first version gave 30 days of Pro for every accepted purchase. Now the end date is the expiry date in Apple's transaction, exactly. Sending the same transaction again changes nothing, which also makes the endpoint safe to call repeatedly.
That is how renewals reach the server. The app sends the current entitlement at every signed-in launch and whenever StoreKit reports an update. I do not use App Store Server Notifications yet. If renewals turn out to arrive late, that is the next thing to add.
Fail closed
If the bundle id is not configured on the server, the endpoint answers with an error and grants nothing. A missing setting should never mean "accept everything". The old switch that allowed unverified purchases is deleted, not turned off.
Testing a verifier without Apple
You cannot ask Apple to sign test data, so the tests build their own small certificate authority and sign transactions with it. The verifier takes the root certificate as a parameter in tests only. In production it is fixed to Apple's.
The suite has 16 cases. The ones that matter most:
- a payload changed after signing
- a perfectly valid chain that ends in my test root, checked against the real Apple root
- the algorithm set to "none", and to RS256
- an expired certificate in the chain
- a revoked transaction
- the same transaction presented by a second account
All 16 pass. One thing a synthetic test cannot prove is the positive path with a real Apple receipt. That takes one sandbox purchase from a device, and I treat the work as unfinished until that has been done.
If you have an endpoint like this
Send it a transaction you wrote yourself, with your own account. If the answer is anything other than a refusal, you have the same bug I had.