How does OpenClaw authenticate requests to its `/hooks` endpoints, what does it reject, and how should `hooks.token` be managed?
answer
- off by default, needs a token
- header, never the query string
- 401, then 429 after repeated failures
- not the Gateway auth token
- a token admits, it does not identify
basics
~20 sHook endpoints stay off until hooks.enabled is set with a nonempty hooks.token, sent as Authorization: Bearer or x-openclaw-token. A token query parameter gets 400, a wrong token 401, repeated failures 429. Use a dedicated token, never the Gateway auth secret.
solid answer
~50 s`hooks.enabled` defaults to false, and enabling it without `hooks.token` is refused with the error `hooks.enabled requires hooks.token`. Each request is a `POST` carrying the token as `Authorization: Bearer <token>` or `x-openclaw-token`. A `token` query parameter is rejected with 400 even when a valid header is also present, which keeps the secret out of URLs. A wrong or missing token gets 401, and after 20 failures in 60 seconds from one client further bad attempts get 429 with `Retry-After`; loopback is not exempt. Bodies over 256 KiB get 413. The token must differ from `gateway.auth.token` and the Gateway password: reuse logs a startup warning, `openclaw security audit` reports it as critical, and `openclaw doctor --fix` rotates a persisted one. A valid token proves a caller may use ingress, not who wrote the content, so also restrict `hooks.allowedAgentIds`, keep `hooks.allowRequestSessionKey` false, and keep the endpoint on loopback, a tailnet or a trusted proxy.
code
json · 9 lines{
"hooks": {
"enabled": true,
"token": "<long-random-hook-token>",
"path": "/hooks",
"allowedAgentIds": ["ci-reader"],
"allowRequestSessionKey": false
}
}go deeper
Remember that hooks are off by default and need hooks.token, sent in a header rather than the URL.
Explain the accepted headers, the 400 for query tokens, 401 for bad tokens and the 429 throttle after repeated failures.
Show the hardening judgment: a dedicated token distinct from Gateway auth, audit and doctor for reuse, allowedAgentIds pointed at a restricted reader agent.
Treat every hook sender as untrusted input: decide which agents and tools inbound events may reach and who owns rotating the shared secret.
## Ingress is opt-in OpenClaw's **hook endpoints** (`/hooks/wake`, `/hooks/agent` and mapped `/hooks/<name>` paths) let an outside service start agent work over HTTP. The whole surface is configured under `hooks.*` and is **off by default**: `hooks.enabled` defaults to `false`, and while it is off those paths fall through to normal Gateway routing, so a caller typically sees `404`. Turning it on requires a nonempty `hooks.token`; without one, resolving the hook configuration fails with the error `hooks.enabled requires hooks.token`. `hooks.token` is a plain shared secret string; SecretRef objects are not supported for it. ## How a request is checked 1. **Method and path.** Only `POST` is accepted (`405` otherwise). `hooks.path` defaults to `/hooks` and may not be `/`. 2. **Query-string tokens are refused first.** If the URL carries a `token` parameter, the request gets `400` even when a correct header is also present. Secrets in URLs tend to be copied into proxy and access logs. 3. **Token from a header.** `Authorization: Bearer <token>` is recommended; `x-openclaw-token: <token>` also works, and a nonempty Bearer value takes precedence. 4. **Failure throttling.** A wrong or missing token returns `401`. After **20 failed attempts in a 60-second window** from one client, further invalid attempts get `429` with `Retry-After`; a valid request resets the counter. **Loopback is not exempt**, and behind a proxy the client is identified only if trusted proxy attribution is configured correctly. 5. **Body limits.** The normal body limit is **256 KiB** (`413` above it), with a 30-second body-read timeout (`408`). ## Status codes at a glance | Status | Meaning | |---|---| | `400` | token in the query string, invalid JSON, or invalid routing or delivery fields | | `401` | missing or wrong hook token (check the hook token, not Gateway auth) | | `404` | hooks disabled, wrong `hooks.path`, or no matching mapping | | `413` | body over the limit | | `429` | throttled after repeated authentication failures | ## Managing the token - **Dedicated and long.** Generate a long random value used only for hooks. - **Never shared with Gateway auth.** If `hooks.token` equals `gateway.auth.token` or the Gateway password, startup logs a warning and `openclaw security audit` reports a **critical** finding: a leaked hook token would also open the Gateway API. `openclaw doctor --fix` rotates a persisted reused token; then update every external sender. - **Rotated deliberately.** A rotation means changing the config, restarting the Gateway (`openclaw gateway restart`) and updating CI secrets in one step. - **Kept out of URLs and logs**, which the query-string rejection enforces on the OpenClaw side. ## Narrowing what a valid token can do A hook token grants **ingress, not identity**: anyone holding it can submit content, so the payload is untrusted data. - `hooks.allowedAgentIds` limits which agents a request may target. Omitted or `["*"]` allows all; `[]` denies all. - `hooks.allowRequestSessionKey: false` (the default) stops callers from choosing session keys; if you enable it, constrain `hooks.allowedSessionKeyPrefixes`. - Point untrusted sources such as CI logs at a reader agent with restricted tools and workspace. Agent-hook content is safety-wrapped, but wrapping does not remove tools. - Keep `allowUnsafeExternalContent` off on mappings. ## A minimal smoke test 1. Merge the `hooks` block into the config and run `openclaw config validate`. 2. Restart the installed service with `openclaw gateway restart` so the new config loads. 3. Follow `openclaw logs --follow` in one terminal. 4. From the Gateway host, POST a harmless `/hooks/agent` request with `"deliver": false` and the Bearer header. 5. Expect `200` with `{"ok": true, "runId": ...}`, then look for `hook agent run completed` with that `runId` in the logs. 6. Repeat once with a wrong token to confirm a `401`, so you know the check is live before any external system holds the secret. ## Where the endpoint should listen The hook docs say to keep these endpoints behind loopback, a tailnet or a trusted reverse proxy, use HTTPS for remote callers, and expose only the hook path. How the Gateway itself is bound and exposed is covered with Gateway exposure; the point here is that a valid token is the only credential the hooks ask for, so the network path should be narrow too.
- A CI system gets 401 from an OpenClaw hook even though the Gateway dashboard login works with the same secret. Why?Hook requests are checked against `hooks.token`, not Gateway auth, so the dashboard secret is the wrong credential, and the two should be different values anyway. Also confirm any reverse proxy forwards the `Authorization` header unchanged. If the CI kept retrying with the wrong value, it may now be getting 429; fix the token and honour `Retry-After`.
- Why restrict `hooks.allowedAgentIds` when the token is already secret?The token only proves a caller may use ingress; it says nothing about who wrote the payload, and every holder of it can target any allowed agent. Limiting `hooks.allowedAgentIds` to a reader agent with narrow tools means a leaked token or a poisoned CI log can reach only that agent, not the owner's main assistant with full workspace access. An empty list denies all agents.
- Passing the hook token as a ?token= query parameter is fine over HTTPS.
- Reusing gateway.auth.token for hooks is safer because there is one secret to rotate.
- A valid hook token proves who wrote the payload, so its content can be trusted.
- Requests from loopback are exempt from hook authentication throttling.
- Setting hooks.enabled to true without a token leaves the endpoints open.