|
| 1 | +# ZSend Webhooks |
| 2 | + |
| 3 | +**Language:** English | [繁體中文](README.zh-TW.md) |
| 4 | + |
| 5 | +ZSend Webhooks is a webhook receiver deployed on Cloudflare Workers. It accepts `POST` requests from ZSend, verifies `x-zsend-signature` and `x-zsend-timestamp`, and returns success only after confirming that the payload has not been tampered with. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- Accepts only `POST` webhook requests |
| 10 | +- Verifies HMAC SHA-256 signatures with `ZSEND_WEBHOOKS_SECRET` |
| 11 | +- Supports the ZSend test event: `X-ZSend-Event: test` |
| 12 | +- Runs on Cloudflare Workers |
| 13 | +- Tests Worker behavior with Vitest and `@cloudflare/vitest-pool-workers` |
| 14 | + |
| 15 | +## Zeabur Test Event Exception |
| 16 | + |
| 17 | +The current implementation has a temporary exception for `X-ZSend-Event: test`: whenever this event is received, the Worker returns `200 OK` directly without running signature verification. |
| 18 | + |
| 19 | +This exists to work around a current Zeabur behavior. Zeabur sends the `test` event immediately after a webhook secret is provided, potentially before the endpoint has had a chance to finish configuring the matching `ZSEND_WEBHOOKS_SECRET`. At that moment, the receiver may not yet have the same secret as Zeabur, so the test event cannot pass the normal `x-zsend-signature` verification flow. The exception lets Zeabur correctly detect that the endpoint is alive. |
| 20 | + |
| 21 | +After Zeabur fixes the test webhook signature behavior, this exception is expected to be removed so every webhook request goes through the same signature verification flow. |
| 22 | + |
| 23 | +## Requirements |
| 24 | + |
| 25 | +- Node.js 22+ |
| 26 | +- npm |
| 27 | +- A Cloudflare account with Wrangler logged in |
| 28 | + |
| 29 | +## Installation |
| 30 | + |
| 31 | +```sh |
| 32 | +npm ci |
| 33 | +``` |
| 34 | + |
| 35 | +Create a local environment file: |
| 36 | + |
| 37 | +```sh |
| 38 | +# macOS / Linux |
| 39 | +cp .env.example .env |
| 40 | + |
| 41 | +# Windows PowerShell |
| 42 | +Copy-Item .env.example .env |
| 43 | +``` |
| 44 | + |
| 45 | +Then set the secret used for webhook signature verification: |
| 46 | + |
| 47 | +```env |
| 48 | +ZSEND_WEBHOOKS_SECRET=your_secret_key_here |
| 49 | +``` |
| 50 | + |
| 51 | +## Local Development |
| 52 | + |
| 53 | +Start the Cloudflare Workers development server: |
| 54 | + |
| 55 | +```sh |
| 56 | +npm run dev |
| 57 | +``` |
| 58 | + |
| 59 | +To use the project `start` script: |
| 60 | + |
| 61 | +```sh |
| 62 | +npm start |
| 63 | +``` |
| 64 | + |
| 65 | +## Deployment |
| 66 | + |
| 67 | +First, store `ZSEND_WEBHOOKS_SECRET` as a Cloudflare Workers secret: |
| 68 | + |
| 69 | +```sh |
| 70 | +npx wrangler secret put ZSEND_WEBHOOKS_SECRET |
| 71 | +``` |
| 72 | + |
| 73 | +Deploy the Worker: |
| 74 | + |
| 75 | +```sh |
| 76 | +npm run deploy |
| 77 | +``` |
| 78 | + |
| 79 | +## Webhook Request Format |
| 80 | + |
| 81 | +The Worker checks the following headers: |
| 82 | + |
| 83 | +| Header | Description | |
| 84 | +| ------------------- | ---------------------------------------------------------- | |
| 85 | +| `x-zsend-event` | ZSend event name; `test` currently returns `OK!` directly | |
| 86 | +| `x-zsend-signature` | HMAC signature in the format `sha256=<hex_digest>` | |
| 87 | +| `x-zsend-timestamp` | Timestamp used to build the signature verification message | |
| 88 | + |
| 89 | +The request body must be valid JSON. |
| 90 | + |
| 91 | +Signature verification uses this message format: |
| 92 | + |
| 93 | +```ts |
| 94 | +const message = `${timestamp}.${rawBody}`; |
| 95 | +const digest = await hmacSha256Hex(secret, message); |
| 96 | +const signature = `sha256=${digest}`; |
| 97 | +``` |
| 98 | + |
| 99 | +Note: `rawBody` must be the exact raw string sent as the request body. Reformatting JSON will produce a different signature. |
| 100 | + |
| 101 | +## Responses |
| 102 | + |
| 103 | +| Status | Body | Scenario | |
| 104 | +| ------ | -------------------------------------------------------------- | ---------------------------------------------------- | |
| 105 | +| `200` | `OK!` | Valid signature, or `X-ZSend-Event: test` is present | |
| 106 | +| `400` | `Missing signature or timestamp` | Missing signature or timestamp | |
| 107 | +| `400` | `Invalid JSON` | Body is not valid JSON | |
| 108 | +| `401` | `Invalid signature` | Signature verification failed | |
| 109 | +| `405` | `Method Not Allowed` | Request method is not `POST` | |
| 110 | +| `500` | `Server configuration error: ZSEND_WEBHOOKS_SECRET is not set` | Worker is missing `ZSEND_WEBHOOKS_SECRET` | |
| 111 | + |
| 112 | +## Tests and Checks |
| 113 | + |
| 114 | +```sh |
| 115 | +npm run test:run |
| 116 | +npm run typecheck |
| 117 | +npm run format:check |
| 118 | +npm run types:check |
| 119 | +``` |
| 120 | + |
| 121 | +During development, you can also use watch mode: |
| 122 | + |
| 123 | +```sh |
| 124 | +npm test |
| 125 | +``` |
| 126 | + |
| 127 | +## Project Structure |
| 128 | + |
| 129 | +```txt |
| 130 | +src/_index.ts Worker fetch entry point |
| 131 | +src/router.ts Webhook request validation and response logic |
| 132 | +src/unit/hmac.ts HMAC SHA-256 helper |
| 133 | +test/index.spec.ts Worker unit and integration tests |
| 134 | +wrangler.jsonc Cloudflare Workers configuration |
| 135 | +``` |
0 commit comments