MCPcopy Create free account
hub / github.com/simstudioai/sim / verifySvixSignature

Function verifySvixSignature

apps/sim/lib/webhooks/providers/clerk.ts:21–53  ·  view source on GitHub ↗

* Verify a Clerk webhook signature using the Svix signing scheme. * Clerk uses Svix under the hood: HMAC-SHA256 of `${svix-id}.${svix-timestamp}.${body}` * signed with the base64-decoded `whsec_...` secret, compared against the * space-delimited, versioned (`v1, `) `svix-signature` header.

(
  secret: string,
  msgId: string,
  timestamp: string,
  signatures: string,
  rawBody: string
)

Source from the content-addressed store, hash-verified

19 * space-delimited, versioned (`v1,<sig>`) `svix-signature` header.
20 */
21function verifySvixSignature(
22 secret: string,
23 msgId: string,
24 timestamp: string,
25 signatures: string,
26 rawBody: string
27): boolean {
28 try {
29 const ts = Number.parseInt(timestamp, 10)
30 const now = Math.floor(Date.now() / 1000)
31 if (Number.isNaN(ts) || Math.abs(now - ts) > 5 * 60) {
32 return false
33 }
34
35 const secretBytes = Buffer.from(secret.replace(/^whsec_/, ''), 'base64')
36 const toSign = `${msgId}.${timestamp}.${rawBody}`
37 const expectedSignature = hmacSha256Base64(toSign, secretBytes)
38
39 const providedSignatures = signatures.split(' ')
40 for (const versionedSig of providedSignatures) {
41 const parts = versionedSig.split(',')
42 if (parts.length !== 2) continue
43 const sig = parts[1]
44 if (safeCompare(sig, expectedSignature)) {
45 return true
46 }
47 }
48 return false
49 } catch (error) {
50 logger.error('Error verifying Clerk Svix signature:', error)
51 return false
52 }
53}
54
55export const clerkHandler: WebhookProviderHandler = {
56 async verifyAuth({

Callers 1

verifyAuthFunction · 0.70

Calls 4

hmacSha256Base64Function · 0.90
safeCompareFunction · 0.90
errorMethod · 0.80
replaceMethod · 0.65

Tested by

no test coverage detected