Skip to content

fix(assertions): enforce webhook timeout on the response body - #12087

Open
BlueX888 wants to merge 9 commits into
promptfoo:mainfrom
BlueX888:fix/webhook-body-timeout
Open

BlueX888 wants to merge 9 commits into
promptfoo:mainfrom
BlueX888:fix/webhook-body-timeout

Conversation

@BlueX888

@BlueX888 BlueX888 commented Nov 2, 2026

Copy link
Copy Markdown

Problem

handleWebhook uses fetchWithRetries(..., WEBHOOK_TIMEOUT) and then reads the response body directly. fetchWithTimeout only guards the wait for response headers — its abort timer is cleared once fetch resolves — so a webhook server that sends headers and then stalls the body hung the assertion until the transport's body timeout (~300s), ignoring WEBHOOK_TIMEOUT entirely.

#12014 fixed this class for buffered cache reads by adding readWithResponseDeadline; the webhook assertion was not covered.

Change

Read the webhook body through readWithResponseDeadline(response, () => response.json()), the same helper used in fetchWithCache, so the configured deadline covers headers + body. Untracked responses (mocks) fall back to a plain read, so existing consumers keep their behavior.

Tests

  • New test/assertions/webhook.test.ts: a loopback server that sends headers and a partial body (stalled) — with the fix the assertion fails at the deadline with Webhook error: Request timed out after 1000 ms instead of hanging; a completed-body control returns pass: true.
  • Targeted suites test/assertions/webhook.test.ts + test/assertions/runAssertion.test.ts: 247 passed. tsc --noEmit and Biome clean on the changed files.
  • Measured: base hangs past 5s (watchdog) with a 1s timeout; with the fix it aborts at ~1.0s.

Notes

Other sites with the same header-only deadline pattern (the n8n provider, several OpenAI providers, redteam indirect-web-pwn, etc.) are intentionally not changed here — some are streaming callers that own their response lifetime; this PR stays on the webhook assertion.

handleWebhook called fetchWithRetries with WEBHOOK_TIMEOUT and then read response.json() directly. fetchWithTimeout only guards the wait for response headers: its timer is cleared once fetch resolves, so a webhook server that sends headers and then stalls the body hung until the transport's body timeout (~300s), ignoring WEBHOOK_TIMEOUT.

Read the body through readWithResponseDeadline, the same mechanism added for buffered responses in promptfoo#12014, so the whole webhook response is covered by the configured timeout.

Adds a regression test with a local server that sends headers and a partial body, plus a completed-body control case.
Copilot AI balanced review requested due to automatic review settings November 2, 2026 01:10
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T03:41:11.117931Z e6a26c9 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@codecov

codecov Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.74%. Comparing base (ef6e023) to head (e6a26c9).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #12087   +/-   ##
=======================================
  Coverage   89.74%   89.74%           
=======================================
  Files        1051     1051           
  Lines       92867    92867           
  Branches    32914    32914           
=======================================
+ Hits        83343    83345    +2     
+ Misses       9524     9522    -2     
Flag Coverage Δ
backend 90.45% <100.00%> (+<0.01%) ⬆️
site 53.49% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9767fb7a74

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread test/assertions/webhook.test.ts Outdated
Comment on lines +90 to +91
expect(elapsed).toBeGreaterThanOrEqual(900);
expect(elapsed).toBeLessThan(4000);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Use a controlled clock for the stalled-body regression

On a loaded CI worker, the real timeout callback can run after the hard four-second bound, making this test fail even when the stalled body is correctly aborted. The test also waits through a real one-second timeout on every run. test/AGENTS.md explicitly requires fake timers for timing-sensitive tests; use a controllable response stream and advance the clock, following the existing pattern in test/cache.retry-cancellation.test.ts.

AGENTS.md reference: test/AGENTS.md:L320-L320

Useful? React with 👍 / 👎.

Comment thread src/assertions/webhook.ts
// The webhook timeout covers the whole response, including the body. Without
// this, a server that sends headers and then stalls the body hangs until the
// transport's own body timeout.
const jsonResponse = await readWithResponseDeadline(response, () => response.json());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Document the whole-response webhook deadline

A webhook that sends headers promptly but finishes its JSON body after the default five-second budget now fails the assertion, but the webhook documentation remains unchanged and does not explain WEBHOOK_TIMEOUT or its whole-response scope. src/assertions/AGENTS.md requires assertion behavior changes to update the expected-output documentation and its index. Document the default, override, and response-body deadline so users can configure slower webhook endpoints correctly.

AGENTS.md reference: src/assertions/AGENTS.md:L28-L29

Useful? React with 👍 / 👎.


async function startServer(handler: Parameters<typeof createServer>[1]): Promise<string> {
const server = createServer(handler);
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Isolate proxy settings for the loopback webhook tests

When a developer or CI runner has an external HTTP_PROXY or ALL_PROXY configured without a NO_PROXY exemption for 127.0.0.1, these tests send their requests through that proxy instead of reaching the local fixture. fetchWithProxy selects the proxy dispatcher in src/util/fetch/index.ts:308-317, and test/AGENTS.md confirms that shared setup preserves proxy settings. Clear and restore PROXY_ENV_KEYS around both tests, as test/fetch.compression.test.ts already does, or mock the transport.

AGENTS.md reference: test/AGENTS.md:L95-L96

Useful? React with 👍 / 👎.

…rrel

The direct module import widened the core -> node cross-layer edge count
(core -> node: 98 vs the 97 baseline), failing Check architecture
boundaries in CI. Re-export readWithResponseDeadline from
src/util/fetch/index.ts (already imported there for the rate-limit peek)
and import it through that barrel, so the dependency graph is unchanged.
Drive the stalled-body case with a mocked transport and fake timers, keep
loopback requests off ambient proxy settings, and document the
WEBHOOK_TIMEOUT whole-response deadline.
@BlueX888
BlueX888 requested a review from ianw-oai as a code owner November 3, 2026 03:16
@BlueX888

BlueX888 commented Nov 3, 2026

Copy link
Copy Markdown
Author

Addressed the three review points in cdd22c5:

  • The stalled-body test now uses a mocked transport and fake timers: it returns a Response with a partial body and lets vi.runAllTimersAsync() fire the response deadline. No real 1 s wait and no dependence on worker scheduling.
  • HTTP(S)_PROXY / ALL_PROXY and their lowercase variants are cleared and restored around the loopback tests, following test/fetch.compression.test.ts, so an ambient proxy without a 127.0.0.1 exemption cannot reroute the fixture request.
  • The Webhook section of expected-outputs/deterministic.md now documents WEBHOOK_TIMEOUT (default 5000 ms) and states that the deadline covers the whole response, including the body.

Verified locally: vitest run test/assertions/webhook.test.ts (2 passed), biome check, prettier --check and tsc --noEmit are clean.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cdd22c589d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/util/fetch/index.ts
import { getEffectiveRequestSignal } from './requestSignal';
import { readWithResponseDeadline, trackResponseDeadline } from './responseDeadline';

export { readWithResponseDeadline };

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Import the response-deadline helper directly

This forwarding export conflicts with the root AGENTS.md guidance: “Avoid re-exporting from files; import directly from the source module.” It adds a second import surface for an internal helper solely to route the webhook dependency through the fetch barrel. Remove this export and import readWithResponseDeadline directly from ../util/fetch/responseDeadline in webhook.ts, following the existing import in src/cache.ts. If that direct dependency changes the architecture edge count, review the baseline adjustment explicitly rather than introducing another re-export.

AGENTS.md reference: AGENTS.md:L314-L314

Useful? React with 👍 / 👎.

@BlueX888

BlueX888 commented Nov 3, 2026

Copy link
Copy Markdown
Author

On this one: the direct import is what I tried first. It adds a new cross-layer edge to the architecture graph (98 vs the baseline 97) and fails the repo's Check architecture boundaries job, which is why 312b32c switched to the src/util/fetch/index.ts barrel: the helper stays behind the existing fetch module boundary and the full matrix is green (72 checks at cdd22c5).

I could instead keep the direct import and raise the architecture baseline for that edge, but that changes a repo-wide guard for one internal helper. The barrel re-export seemed the smaller intervention. Happy to change it if maintainers prefer updating the baseline or moving the helper itself.

This branch has not been deployed

No deployments
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.

3 participants