Cloudflare WorkersSecurityCatch

Verifying Apple's JWS x5c Chain in 314 Lines With Zero Dependencies

Catch is now live on the App Store. Because setup only requires pasting a public webhook URL into App Store Connect, every incoming POST must verify the full x5c certificate chain up to Apple Root CA - G3. Here is how 314 lines of WebCrypto and a minimal DER parser handle it on Cloudflare Workers.

Catch is now live on the App Store (1.0, outside Mainland China, free with no IAP). Its whole premise is trust without credentials: developers paste one URL into App Store Connect’s server notifications instead of handing over App Store Connect API keys.

Because that webhook URL sits on the public internet, any caller can POST a fabricated ONE_TIME_CHARGE payload to /hook/<token>. Instead of pulling a Node X.509 stack into Cloudflare Workers, worker/src/verify.js verifies the full chain using WebCrypto and an ~80-line DER parser — 314 lines total, zero dependencies.

Seven Checks, Twice per Notification

App Store Server Notifications V2 wraps both the outer signedPayload and the inner data.signedTransactionInfo as separate JWS tokens. hook.js verifies both through verifyAppleJWS():

const opts = { rootFingerprint: env.APPLE_ROOT_FINGERPRINT };
payload = await verifyAppleJWS(signedPayload, opts);
tx = payload.data?.signedTransactionInfo
  ? await verifyAppleJWS(payload.data.signedTransactionInfo, opts)
  : {};

Inside verifyAppleJWS and verifyChain, validation matches Apple’s official library across seven checks:

  1. Require alg === "ES256" and an x5c array of at least two DER certificates.
  2. Hash the tail certificate’s raw DER bytes with SHA-256 and match against env.APPLE_ROOT_FINGERPRINT (Apple Root CA - G3).
  3. Enforce [notBefore, notAfter] on every certificate (handling both two-digit UTCTime < 50 ? "20" : "19" and four-digit GeneralizedTime).
  4. Require basicConstraints (2.5.29.19) with cA == TRUE on every certificate after the leaf (i > 0) — including rejecting an empty SEQUENCE, where ASN.1 defaults cA to FALSE.
  5. Verify each certificate’s tbsCertificate signature against its issuer’s SPKI public key up to the self-signed root.
  6. Check Apple’s custom extension OIDs (1.2.840.113635.100.6.11.1 on the leaf and 1.2.840.113635.100.6.2.1 on the intermediate, toggleable for self-signed test chains).
  7. Verify the JWS signature itself using the leaf’s SPKI key.

Two Byte-Level Traps in Hand-Rolled DER

Two ASN.1 details caused the most friction when wiring DER directly into crypto.subtle.verify:

First, an X.509 signatureValue is a BIT STRING, whose first content byte declares the number of unused trailing bits. Slicing the signature requires der.subarray(sigVal.contentStart + 1, sigVal.end).

Second, JWS signatures already arrive as fixed-width r || s, whereas X.509 ECDSA signatures are DER SEQUENCE { r INTEGER, s INTEGER }. ASN.1 prepends a 0x00 byte whenever an integer’s high bit is set and omits leading zeros on small values. derSigToRaw() + copyInt() strip sign-padding zeros and right-align each integer into size-byte slots (supporting P-256, P-384, and P-521 across SHA-256/384/512, since intermediate/root certs aren’t restricted to P-256):

function copyInt(src, dst, offset, size) {
  let i = 0;
  while (i < src.length - 1 && src[i] === 0) i++;
  const v = src.subarray(i);
  if (v.length > size) throw new VerifyError("signature integer too long");
  dst.set(v, offset + size - v.length);
}

Returning 200 on Forgeries, 5xx Only on Our Faults

In hook.js, catching a VerifyError writes the raw payload to D1’s dead_letters table (retained for 7 days) and returns HTTP 200. Probes learn nothing about whether a token exists or why verification failed, and Apple doesn’t retry garbage. Only storage/APNs failures or unexpected runtime exceptions return 5xx so Apple retries safely.

One deliberate boundary: verify.js does not perform live OCSP revocation checks in the request path to avoid adding outbound latency and failure modes inside stateless Workers. The full implementation is covered by 18 verification tests (including verifyChain against real Apple WWDR G6 ← Root G3 certificates) and 172 webhook tests in test/.

✦

Comments

  • Loading…

Comments are reviewed before publishing; email is visible only to me.