Repository navigation
chore(server): post the other webhooks with postJson, as Send to CMS does (#1437 review) - #1568
Conversation
…does (ansvisor#1437 review) Co-Authored-By: Claude Opus 5.5 <[email protected]>
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good, with follow-ups. No workspace has an active webhook on prod today, so I'm merging it now for the guarded senders, and item 1 (a redirect marks an opportunity Sent although nothing arrived) can follow in its own PR. Please do that one first, before a workspace sets a webhook up.
What I checked:
-
Own change. One commit on 3b650fb:
webhook-dispatch.js, the three routes incontent.js, both tests and the docs. -
postJsonitself. I ran it against local servers on Node 20 (the server image) and Node 22.- Addresses. With
IS_CLOUD=trueit refused all 20 forms I tried: private, loopback, link-local, CGNAT, metadata and reserved addresses, over IPv4 and IPv6. That includes mapped and NAT64 forms, decimal and hex IPv4, and names that resolve to loopback. Nothing reached the local server. The check runs in the connection's own lookup, on every address the name resolves to. - Redirects. None is followed. A 302 or a 307 comes back as the answer.
- Time. The limit covers the whole request. A server that never answers, one that sends headers and then nothing, and one that trickles its body all stop at the limit.
- Size. A 1 MB answer is read only to 8 KB.
- Ports. Any port is allowed on a public address, by design, as in Send to CMS.
- Addresses. With
-
Self-hosted or Cloud.
isCloud()readsIS_CLOUDfrom the environment on every call. No route passeslookup, and every sender runs in the API process. -
Every webhook sender now uses
postJson. That covers Send to CMS,dispatchBrandWebhook(daily_pulse.created,action_task.sent), the single and bulk opportunity sends, and the test. Nothing else inserver/orweb/posts to an address a workspace configures. The web's server actions only call our API. -
What changes for a redirecting webhook. The old
fetchfollowed redirects. I checked how:- 301, 302 and 303 turned the POST into a GET with no body, so those webhooks never got a payload.
- 307 and 308 re-sent the POST with its body.
Now:
- Daily Pulse records
webhook_sent: false. - A task send answers 502
webhook_failed, as for any non-2xx. - An opportunity send: see item 1.
-
What changes for a slow webhook. Nothing: 15 s as before. The single send answers 500, the bulk send counts it failed, the pulse isn't marked sent, and a task send answers 502.
-
Logging and retries. Logging is as before: warn in
dispatchBrandWebhook, error in the single send, a count in the bulk send. There are no retries, before or after. -
The test route. It returns the same keys, and the dialog reads only
success,statusanderror. A URL that redirected to a page answering a GET used to pass the test even though real sends never arrived; now it fails, which is right. -
Docs. They match the code: 15 s (10 for the test), no redirects, public addresses only on Cloud.
-
Speed. No new regex. The new loops are bounded by the configs and the ids already sent.
-
Tests. The two files (11 tests) pass on the head. So do the
safe-post,task-send, pulse and content route tests (93 in all). Withcontent.jsandwebhook-dispatch.jsput back to the base, 9 of the 11 fail. CI passes. -
Merge. It merges clean on 55b49e7, and the merged change is the same as the PR's own diff. #1507, #1573 and #1581 also edit
webhooks.mdx. Each one still merges clean after this one, with no line duplicated. -
Before the API deploy. This is server-only, so nothing changes for users until the API is deployed.
Follow-ups:
-
(first) A redirect marks an opportunity Sent although nothing arrived (
server/src/routes/content.js:1121-1159,:937-960).- A webhook that answers 307 or 308 used to get the payload. Now the send stops at the redirect, the toast says "Sent to workflow", and the row is marked
sent. - Failing a 3xx changes nothing a workflow sees, because none of them got the payload. So it stays outside the "any answer marks sent" question that the PR leaves for later. Send to CMS already fails a 3xx.
- Single send:
if (webhookRes.status >= 300 && webhookRes.status < 400) { return res.status(502).json({ error: 'The webhook address redirects. Use the address it redirects to.', code: 'webhook_redirect', webhookStatus: webhookRes.status, }); }
- Bulk:
if (webhookRes.status >= 300 && webhookRes.status < 400) { failed++; continue; }before the write. - The test at
content-webhooks.test.js:200then expects 502 and no write. Add a bulk case that counts a 302 as failed. - Docs (
webhooks.mdx:113): "When your endpoint responds with anything but a redirect, the opportunity is markedsent… A redirect counts as not delivered, and the opportunity keeps its status."
- A webhook that answers 307 or 308 used to get the payload. Now the send stops at the redirect, the toast says "Sent to workflow", and the row is marked
-
A 2xx whose answer is cut off counts as failed (
server/src/lib/safe-post.js:96-97).- When the endpoint sends its status and then drops the connection,
postJsonrejects with the rawabortederror. The old code kept the 200 (text().catch(() => '')) and marked it sent. - Now the single send answers 500, the bulk send counts it failed, and a task send answers 502 "didn't accept", although the endpoint did accept it.
- Once the status has arrived, resolve with it and with what was read:
res.on('error', done).
- When the endpoint sends its status and then drops the connection,
-
The test's messages.
- On Cloud, a non-http(s) address or one with sign-in details gets
checkPageUrl's wording: "Only http and https pages can be read", "URLs with credentials are not read". Check the scheme and the credentials incheckWebhookUrlfirst in both modes (safe-post.js:33-47), and callcheckPageUrlonly for the IP check. - "The webhook could not be reached" drops the reason.
PageFetchErrorcarrieserrno(ENOTFOUND, ECONNREFUSED, a TLS error), so add it to the test'serror. - A redirect shows as "HTTP 302". Optionally, return the
locationfrompostJsonfor a 3xx and have the dialog say where it points.
- On Cloud, a non-http(s) address or one with sign-in details gets
-
The test route. Tie it to a brand the caller can edit the webhook for. The dialog has the
brandId, and only admins and managers can save a webhook. Give it its own small per-user limit as well, like 10 a minute. -
Docs. Name the
User-Agent(Ansvisor-Webhook/1.0) under Delivery, so a firewall can allow it.
…ts (#1590) No redirect is followed since #1568, so a 3xx means the payload never arrived. A single send answers 502 `webhook_redirect` ("The webhook address redirects. Use the address it redirects to."). A bulk send counts it failed. Either way the opportunity keeps its status, as Send to CMS does for a 3xx. Any other answer still marks it sent. The docs say so (api-reference/webhooks.mdx). Co-authored-by: Cihan <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
…ed, and tie the test to a brand (#1605) * fix(content): don't mark an opportunity sent when its webhook redirects No redirect is followed since #1568, so a 3xx means the payload never arrived. A single send answers 502 `webhook_redirect` ("The webhook address redirects. Use the address it redirects to."). A bulk send counts it failed. Either way the opportunity keeps its status, as Send to CMS does for a 3xx. Any other answer still marks it sent. The docs say so (api-reference/webhooks.mdx). Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(content): keep a cut-off 2xx as sent, say why a webhook test failed, and tie the test to a brand Follow-ups 2–5 from #1568. - postJson: once the status is in, a dropped connection resolves with that status and what was read, so a 2xx whose answer is cut off still counts as delivered. A 3xx also returns its location, made absolute. - checkWebhookUrl checks the scheme and sign-in details first in both modes, with its own words. On the cloud checkPageUrl runs only for the address check. - The webhook test: - takes a brandId the caller can edit the webhook for (admins and managers); - has its own limit (10 a minute, webhookTestLimiter); - names a network error's code ("…could not be reached (ENOTFOUND)"). The dialog says where a redirect points, and why a test was refused. - Docs: the User-Agent (Ansvisor-Webhook/1.0), cut-off answers, redirects, error codes, and who may run the test. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Cihan <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Gökhan Geyik <[email protected]>
Summary
The follow-up from #1437's review (item 5): the other webhook senders post with
postJson(server/src/lib/safe-post.js), as Send to CMS does, instead of plainfetch:dispatchBrandWebhook(lib/webhook-dispatch.js), which sendsdaily_pulse.createdand the Action Center'saction_task.sent;opportunity.sent,routes/content.js);POST /api/content/webhook-test).They get the same guards:
What changes for a workspace:
dispatchBrandWebhookcounts it failed, as it counts any non-2xx.senton it, as it did on any answer before (a 500 included). The new test pins that.User-Agent: Ansvisor-Webhook/1.0and noAcceptheaders. The secret header is unchanged.postJson's reasons (for example10.0.0.5 is not a public address). For the test, that's the error under the field.Not changed here: whether an Opportunities send that gets a non-2xx should stay
sent. It always has. Making it fail (502, notsent;failedin bulk) changes what workflows see, so it's a separate PR if you want it.Docs:
api-reference/webhooks.mdx. Redirects and the Cloud address rule now apply to every event. The Opportunities line says any answer marks itsent.Related issue
Follow-up to #1437 (review item 5), part of #1331.
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
cd server && npx vitest run src/routes/content-webhooks.test.js src/lib/webhook-dispatch.test.jshttp://10.0.0.5/hookreports "10.0.0.5 is not a public address".Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)Server only:
npm run format,npm run lintandnpm test(2,912 tests) inserver/. After the pre-push review's fixes (tests and docs only), I ran the two test files and eslint again. No web change.🤖 Generated with Claude Code