Skip to content

fix(content): keep a cut-off 2xx as sent, say why a webhook test failed, and tie the test to a brand - #1605

Merged
gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:fix/webhook-followups
Oct 27, 2026
Merged

gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:fix/webhook-followups

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Follow-ups 2–5 from #1568's approval (1 is #1590).

Stacked on #1590. This PR's own change is its commit after #1590's: 32dc544.

2. A 2xx cut off mid-answer counts as delivered (lib/safe-post.js).

  • Once the status has arrived, a dropped connection resolves with that status and what was read (res.on('error', done)), instead of rejecting with the raw aborted error.
  • So the single send marks it sent again, the bulk send counts it sent, and a task send isn't reported as refused.

3. The test's messages.

  • checkWebhookUrl checks the scheme and any sign-in details first, in both modes, in its own words: "Only http and https addresses can be posted to", "URLs with credentials are not posted to". On the cloud, checkPageUrl runs only for the address check.
  • A network failure names its code: "The webhook could not be reached (ENOTFOUND)", or ECONNREFUSED, or a TLS error.
  • postJson returns a 3xx's location, made absolute, and the dialog says where the redirect points.

4. The test route is tied to a brand (POST /api/content/webhook-test).

  • The body takes brandId (Zod).
  • Only an admin or manager of that brand's workspace may test, as only they can save a webhook. Others get 403 forbidden; another workspace's brand gets 403 not_in_workspace.
  • Its own limit: 10 a minute per user (webhookTestLimiter).
  • On the web, testWebhook(brandId, url, secret) returns why it failed. The dialog says it for a 403, a 429, a redirect and a network error (testRedirect, testForbidden).

5. Docs (api-reference/webhooks.mdx, Delivery):

  • the User-Agent (Ansvisor-Webhook/1.0 (+https://ansvisor.com), as USER_AGENT sends it), so a firewall can allow it;
  • cut-off answers, redirects and their address;
  • network error codes;
  • who may test, and the limit.

Related issue

Follow-up to #1568 (items 2–5).

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/lib/safe-post.test.js src/routes/content-webhooks.test.js src/middleware/rate-limiter.test.js
  2. cd web && yarn vitest run src/lib/actions/content-webhook.test.ts "src/app/[locale]/(dashboard)/dashboard/content/_webhook-settings.test.tsx"
  3. In Content Opportunities › Webhook Settings, as an admin:
    • Test Webhook with a closed port: "…could not be reached (ECONNREFUSED)";
    • with ftp://…: the scheme message;
    • with an address that redirects: where it points.

Checked in the browser at 1024 px, light and dark, with this branch's API and web against the dev DB. Nothing was saved, and the webhook_configs rows are unchanged.

  • Test Webhook with http://localhost:9/hook read "The webhook could not be reached (ECONNREFUSED)".
  • With ftp://example.com/hook it read "Only http and https addresses can be posted to".

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

Also run:

  • server/: npm run format, npm run lint and npm test (2,966 tests);
  • web/: yarn format, yarn lint (0 errors), yarn typecheck, yarn test (4,152 tests) and yarn build.

The fixer broke 18 behaviours one at a time to check the tests catch them.

The Test Webhook button still shows to members. They get the 403's message; hiding it would be its own change.

🤖 Generated with Claude Code

cihan-gyk and others added 2 commits October 27, 2026 17:23
No redirect is followed since ansvisor#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]>
…ed, and tie the test to a brand

Follow-ups 2–5 from ansvisor#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]>
@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 5eda3d9.

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. Rebase it onto main before merging: #1590 was squashed, so GitHub now shows a conflict.

What I checked:

  • Own change. One commit, 32dc544, on #1590's branch head (ca5fde6).

  • Merge.

    • On main (15b5b5f), server/src/lib/safe-post.js and server/src/routes/content.js conflict. That's only because #1590 went in as a squash: every file this PR touches is the same on main as on ca5fde6, apart from en.json, which merges on its own.
    • Replaying the own commit onto main (git rebase --onto origin/main ca5fde6e) has no conflicts and gives exactly the PR's own diff.
  • With #1590. A cut-off answer keeps its status, so:

    • a cut-off 3xx is still a redirect, which the single send, the bulk send, Daily Pulse, the task send and Send to CMS all count as not delivered;
    • a cut-off 2xx counts as delivered everywhere.

    location is only added to a 3xx's result. Every postJson caller still reads only status and answer.

  • Address checks. The scheme and credentials checks now run first. On Cloud, checkPageUrl and the public-only lookup still run on every request, and no redirect is followed. Self-hosted works as before.

  • Error text. The test answers with:

    • our own PageFetchError messages;
    • for a network failure, the error's string code (ENOTFOUND, ECONNREFUSED, a TLS code). A numeric errno is left out;
    • for a 3xx, the Location the webhook sent.

    Nothing about our own servers or network appears in any of them.

  • Access.

    • assertBrandAccess and then the profile's role, as cms.js does. This matches the webhook_configs policies: only admins and managers can insert, update or delete.
    • webhookTestLimiter allows 10 tests a minute per user.
    • Zod rejects a missing brand, a brand that isn't an id, and a missing address before anything is posted.
  • Web.

    • A 403 forbidden shows testForbidden. Any other refusal goes through apiFailure: 429 shows busy, and #1589's 402 subscription_inactive shows plan.
    • A redirect names its target. Every other failure shows the API's error.
  • Tests.

    • At the head, the safe-post, content-webhooks and rate-limiter tests pass (61).
    • With ca5fde6's source under the head's tests, 19 of them fail: the scheme and credentials wording, cut-off answers, location, network codes, the 400s, the 403s and the 429.
    • CI passes.
  • Before the API deploy. The web sends brandId, and the old API ignores it. The dialog shows the same messages as today. The 403, 429 and redirect messages appear once the API is deployed.

  • Speed. This PR adds no regex and no loop over user input.

Follow-ups:

  1. A 2xx still arriving at the time limit is reported as a timeout (server/src/lib/safe-post.js:124-131, docs/api-reference/webhooks.mdx:296).
    • I tested a webhook that sends 200 and part of the answer, then nothing more. With a 300 ms limit, postJson rejects with timeout, because the abort reaches req.on('error') first. So a send is marked failed, and a task send is reported as refused, although the status arrived.
    • Either set a flag in the response callback and, in req.on('error'), resolve with done() once a response has started, or make the docs line say "if the connection drops", not a slow answer.
  2. Members still see Test Webhook (_webhook-settings.tsx:252). They only learn they can't test after pressing it. As the description says, hiding or disabling the button can come in its own change.

@gkhngyk
gkhngyk merged commit 5a1f5a1 into ansvisor:main Oct 27, 2026
5 of 6 checks passed
gkhngyk pushed a commit that referenced this pull request Oct 28, 2026
…r doesn't arrive in time (#1668)

Follow-up from #1605 (item 1). A webhook that sends 200 and part of its
answer, then nothing more, was reported as a timeout: the time limit's
abort reaches the request's error handler before the answer's, so
postJson rejected with `timeout` though the status had arrived. A send
was marked failed, and a task send reported as refused.

postJson now remembers once the status has arrived; an error the request
hears after that (the time limit, the connection dropping) ends that
answer with what was read of it. The docs' Delivery line says so.

The new test fails on main: a 200 that stalls past a 100 ms limit.

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
gkhngyk pushed a commit that referenced this pull request Oct 28, 2026
…nd Save aren't pressed only to be refused (#1673)

* fix(content): show a member the webhook settings read-only, so Test and Save aren't pressed only to be refused

Follow-up from #1605 (item 2). Only admins and managers can save a
brand's webhook (webhook_configs' RLS) and test it (the API's
assertWebhookEditor). A member saw Test Webhook and Save Webhook as for
anyone and learned they couldn't only after pressing: a "forbidden" test
result, or a failed save.

The dialog now reads the role (useUserRole): for a member the address,
secret, events and on/off switch show as they are but disabled, Test and
Save are disabled, and a line says "Only admins and managers can change
or test this webhook." The webhooks docs say members see it read-only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): dim the webhook's Active switch for a member, as its other disabled fields

Review follow-up. The hand-made Active switch had no disabled styles, so
a member saw it in full colour with the pointer cursor although it does
nothing. It now takes disabled:cursor-not-allowed disabled:opacity-50,
as the disabled checkboxes beside it do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[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