Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

fix(content): don't mark an opportunity sent when its webhook redirects (#1590)

Cihanb1c1e98ed269
diff --git a/docs/api-reference/webhooks.mdx b/docs/api-reference/webhooks.mdx
index 7b70e5d5d..c70d8072a 100644
--- a/docs/api-reference/webhooks.mdx
+++ b/docs/api-reference/webhooks.mdx
@@ -110,7 +110,7 @@ Payloads are flat JSON objects with an `event` field at the top level.
- `brief` is `null` until a content brief has been generated for the opportunity.
- `callback_url` points at `POST /webhooks/content/{id}/status`; `callback_method` is `POST`. Send `{ "status": "in_progress" }` (or `new`, `reviewed`, `sent`, `done`, `dismissed`) as JSON and pass the same secret configured for this active webhook in the `X-Webhook-Secret` header. Configure a non-empty webhook secret in **Content → Configure Webhook** before using callbacks; callback authentication fails with 401 when no secret is configured. Callbacks are limited to 1,200 requests per 15 minutes per secret hash, with a separate per-IP limiter for failed (401) authentication attempts (10 per 15 minutes). They do not require a dashboard JWT.
-When your endpoint responds, with any status (a redirect too), the opportunity is marked `sent` and the response status plus the first 1,000 characters of its body are stored only if the opportunity's status and update timestamp have not changed since delivery began. If your workflow calls `callback_url` before replying (or a user updates the opportunity during delivery), the newer status is preserved and the late response does not overwrite it. If the request times out or the connection fails, the opportunity keeps its previous status.
+When your endpoint responds with anything but a redirect, the opportunity is marked `sent` and the response status plus the first 1,000 characters of its body are stored only if the opportunity's status and update timestamp have not changed since delivery began. If your workflow calls `callback_url` before replying (or a user updates the opportunity during delivery), the newer status is preserved and the late response does not overwrite it. A redirect (3xx) is never followed, so it counts as not delivered: the opportunity keeps its status, and a single send says the address redirects. Use the address it redirects to. If the request times out or the connection fails, the opportunity keeps its previous status too.
### `daily_pulse.created`
diff --git a/server/src/lib/safe-post.js b/server/src/lib/safe-post.js
index e795eca5a..830ac1d69 100644
--- a/server/src/lib/safe-post.js
+++ b/server/src/lib/safe-post.js
@@ -26,6 +26,9 @@ export const MAX_ANSWER_BYTES = 8 * 1024;
const USER_AGENT = 'Ansvisor-Webhook/1.0 (+https://ansvisor.com)';
+/** A 3xx: an answer that sends elsewhere, so the body never arrived (none is followed). */
+export const isRedirect = (status) => status >= 300 && status < 400;
+
/**
* A webhook address, checked as a page's is but on any port; throws
* 'invalid_url' or 'blocked'. `privateOk` (self-hosted) takes a private one.
diff --git a/server/src/lib/safe-post.test.js b/server/src/lib/safe-post.test.js
index 6ec6aef6d..fa38a0c29 100644
--- a/server/src/lib/safe-post.test.js
+++ b/server/src/lib/safe-post.test.js
@@ -1,7 +1,7 @@
import http from 'node:http';
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
import { PageFetchError, publicOnlyLookup } from './safe-fetch.js';
-import { MAX_ANSWER_BYTES, checkWebhookUrl, postJson } from './safe-post.js';
+import { MAX_ANSWER_BYTES, checkWebhookUrl, isRedirect, postJson } from './safe-post.js';
/**
* Posting a content package to a workspace's webhook (Send to CMS, #1331)
@@ -30,6 +30,19 @@ describe('checkWebhookUrl', () => {
});
});
+describe('isRedirect', () => {
+ it('takes a 3xx, and nothing either side of it', () => {
+ expect([299, 300, 302, 308, 399, 400].map(isRedirect)).toEqual([
+ false,
+ true,
+ true,
+ true,
+ true,
+ false,
+ ]);
+ });
+});
+
describe('postJson', () => {
let server;
let port;
diff --git a/server/src/routes/content-webhooks.test.js b/server/src/routes/content-webhooks.test.js
index f0810b8c3..ca32bb7c1 100644
--- a/server/src/routes/content-webhooks.test.js
+++ b/server/src/routes/content-webhooks.test.js
@@ -49,7 +49,10 @@ function query(table) {
async function startApp() {
vi.resetModules();
- vi.doMock('../lib/safe-post.js', () => ({ postJson: (...args) => postJson(...args) }));
+ vi.doMock('../lib/safe-post.js', async (importOriginal) => ({
+ ...(await importOriginal()),
+ postJson: (...args) => postJson(...args),
+ }));
// As the other content route tests: the jobs and workers aren't loaded.
vi.doMock('../lib/plan-guard.js', () => ({
PlanLimitError: class PlanLimitError extends Error {},
@@ -197,12 +200,27 @@ describe('POST /api/content/:id/send-webhook', () => {
});
});
- it('marks it sent on a redirect, as on any answer, keeping the status it got', async () => {
- postJson.mockResolvedValue({ status: 302, answer: '' });
+ it.each([301, 302, 307, 308])(
+ 'answers 502 on a %i, which no send follows, and marks nothing sent',
+ async (status) => {
+ postJson.mockResolvedValue({ status, answer: '' });
+ const res = await post('/o1/send-webhook');
+ expect(res.status).toBe(502);
+ expect(await res.json()).toEqual({
+ error: 'The webhook address redirects. Use the address it redirects to.',
+ code: 'webhook_redirect',
+ webhookStatus: status,
+ });
+ expect(updates.find((u) => u.table === 'content_opportunities')).toBeUndefined();
+ },
+ );
+
+ it('marks it sent on any other answer, an error too, keeping the status it got', async () => {
+ postJson.mockResolvedValue({ status: 400, answer: 'bad' });
const res = await post('/o1/send-webhook');
- expect(await res.json()).toMatchObject({ success: true, webhookStatus: 302 });
+ expect(await res.json()).toMatchObject({ success: true, webhookStatus: 400 });
const write = updates.find((u) => u.table === 'content_opportunities');
- expect(write.values).toMatchObject({ status: 'sent', webhook_response: { status: 302 } });
+ expect(write.values).toMatchObject({ status: 'sent', webhook_response: { status: 400 } });
});
it('answers 500 when the address is refused, and marks nothing sent', async () => {
@@ -239,4 +257,16 @@ describe('POST /api/content/bulk/send-webhook', () => {
expect(writes).toHaveLength(1);
expect(writes[0].values.webhook_response).toEqual({ status: 200, body: 'ok' });
});
+
+ it('counts a redirect failed and marks only the delivered one sent', async () => {
+ rows.content_opportunities = [OPPORTUNITY, { ...OPPORTUNITY, id: 'o2', title: 'FAQ page' }];
+ postJson
+ .mockResolvedValueOnce({ status: 302, answer: '' })
+ .mockResolvedValueOnce({ status: 200, answer: 'ok' });
+ const res = await post('/bulk/send-webhook', { ids: ['o1', 'o2'] });
+ expect(await res.json()).toEqual({ sent: 1, failed: 1 });
+ const writes = updates.filter((u) => u.table === 'content_opportunities');
+ expect(writes).toHaveLength(1);
+ expect(writes[0].values.webhook_response).toEqual({ status: 200, body: 'ok' });
+ });
});
diff --git a/server/src/routes/content.js b/server/src/routes/content.js
index 6472e427c..d8b0aac1b 100644
--- a/server/src/routes/content.js
+++ b/server/src/routes/content.js
@@ -17,7 +17,7 @@ import { resolveModel } from '../lib/ai-provider.js';
import { getLanguageName } from '../lib/languages.js';
import supabaseAdmin from '../config/supabase.js';
import { selectInChunks } from '../lib/chunked-in.js';
-import { postJson } from '../lib/safe-post.js';
+import { isRedirect, postJson } from '../lib/safe-post.js';
import {
assertBrandAccess,
assertOpportunitiesAccess,
@@ -937,6 +937,11 @@ router.post('/bulk/send-webhook', async (req, res) => {
const webhookRes = await postJson(webhookConfig.webhook_url, JSON.stringify(payload), {
headers,
});
+ // A redirect isn't followed, so nothing arrived: not sent.
+ if (isRedirect(webhookRes.status)) {
+ failed++;
+ continue;
+ }
const responseBody = webhookRes.answer;
// A workflow may call callback_url before it sends its HTTP response.
@@ -1121,6 +1126,15 @@ router.post('/:id/send-webhook', async (req, res) => {
const webhookRes = await postJson(webhookConfig.webhook_url, JSON.stringify(payload), {
headers,
});
+ // A redirect isn't followed, so the payload never arrived: the
+ // opportunity keeps its status, as Send to CMS fails a 3xx.
+ if (isRedirect(webhookRes.status)) {
+ return res.status(502).json({
+ error: 'The webhook address redirects. Use the address it redirects to.',
+ code: 'webhook_redirect',
+ webhookStatus: webhookRes.status,
+ });
+ }
const responseBody = webhookRes.answer;
const { data: statusUpdate } = await supabaseAdmin