fix(billing): verify the signing scheme the secret actually says it is

Every delivery was refused as a signature mismatch, and nothing in the error
said why.

The provider signs one of two ways. A `whsec_` prefix means Standard Webhooks,
where the secret is a base64 key the verifier decodes; anything else is the
older scheme, where the secret is used as its own raw bytes. Which one a secret
belongs to is decided by when it was created, and every secret created now is
the new one.

The SDK's helper only implements the older scheme — it base64-encodes whatever
it is handed, so a Standard Webhooks secret becomes the literal bytes of the
string including its prefix, and every signature then fails against a key
derived quite differently.

The scheme is now read off the secret rather than configured, so rotating one
cannot put the two out of step. The verifier for the new scheme is the same
library the SDK uses underneath; nothing here hand-rolls crypto.

Tested with a real signature rather than only a rejection. A mismatch is easy to
assert by accident, and a test that only proved bad input is refused would have
passed against the broken version too.

Also teaches the product script to check shape and not just name: a one-time
product where a subscription is wanted cannot be subscribed to at all, and
reporting it as already-there hands back an id that fails at its first use.
This commit is contained in:
Wanjohi
2026-09-19 01:33:26 +03:00
parent 2dfb7f4007
commit 5b90bd0f0c
6 changed files with 146 additions and 41 deletions

View File

@@ -15,7 +15,8 @@
"hono": "^4.12.31",
"typescript": "^7.0.1-rc",
"zod": "^4.4.3",
"@polar-sh/sdk": "^0.49.0"
"@polar-sh/sdk": "^0.49.0",
"standardwebhooks": "^1.1.1"
}
},
"type": "module",