Skip to content

chore(server): post the other webhooks with postJson, as Send to CMS does (#1437 review) - #1568

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:chore/webhooks-post-json
Oct 27, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:chore/webhooks-post-json

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

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 plain fetch:

  • dispatchBrandWebhook (lib/webhook-dispatch.js), which sends daily_pulse.created and the Action Center's action_task.sent;
  • the Opportunities send, single and bulk (opportunity.sent, routes/content.js);
  • the webhook test (POST /api/content/webhook-test).

They get the same guards:

  • On Ansvisor Cloud, a private or local address is refused, also when a name resolves to one, and no DNS rebinding. Self-hosted, the workspace's own network is still allowed.
  • No redirect is followed.
  • 15 seconds; 10 for the test, as before.
  • At most 8 KB of the answer is read. What's kept is still its first 1,000 characters.

What changes for a workspace:

  • A webhook address that redirects (for example http → https) no longer reaches the address it points to.
    • Test reports the 3xx as a failure.
    • dispatchBrandWebhook counts it failed, as it counts any non-2xx.
    • The Opportunities send marks the opportunity sent on it, as it did on any answer before (a 500 included). The new test pins that.
    • Use the address the redirect points to.
  • The request goes with User-Agent: Ansvisor-Webhook/1.0 and no Accept headers. The secret header is unchanged.
  • A refused address answers with postJson's reasons (for example 10.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, not sent; failed in 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 it sent.

Related issue

Follow-up to #1437 (review item 5), part of #1331.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. cd server && npx vitest run src/routes/content-webhooks.test.js src/lib/webhook-dispatch.test.js
  2. On a local stack, open Content → Configure Webhook, enter an address that redirects and click Test: it reports the 3xx. On Cloud, http://10.0.0.5/hook reports "10.0.0.5 is not a public address".

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Server only: npm run format, npm run lint and npm test (2,912 tests) in server/. 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

@mintlify

mintlify Bot commented Oct 27, 2026

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 9919588.

Approve preview

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in content.js, both tests and the docs.

  • postJson itself. I ran it against local servers on Node 20 (the server image) and Node 22.

    • Addresses. With IS_CLOUD=true it 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.
  • Self-hosted or Cloud. isCloud() reads IS_CLOUD from the environment on every call. No route passes lookup, 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 in server/ or web/ posts to an address a workspace configures. The web's server actions only call our API.

  • What changes for a redirecting webhook. The old fetch followed 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, status and error. 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). With content.js and webhook-dispatch.js put 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:

  1. (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:200 then 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 marked sent … A redirect counts as not delivered, and the opportunity keeps its status."
  2. 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, postJson rejects with the raw aborted error. 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).
  3. 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 in checkWebhookUrl first in both modes (safe-post.js:33-47), and call checkPageUrl only for the IP check.
    • "The webhook could not be reached" drops the reason. PageFetchError carries errno (ENOTFOUND, ECONNREFUSED, a TLS error), so add it to the test's error.
    • A redirect shows as "HTTP 302". Optionally, return the location from postJson for a 3xx and have the dialog say where it points.
  4. 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.

  5. Docs. Name the User-Agent (Ansvisor-Webhook/1.0) under Delivery, so a firewall can allow it.

@gkhngyk
gkhngyk merged commit d3e0571 into ansvisor:main Oct 27, 2026
7 checks passed
gkhngyk pushed a commit that referenced this pull request Oct 27, 2026
…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]>
gkhngyk added a commit that referenced this pull request Oct 27, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants