Files
PaperClipAI/server
Luke AbellandLuke Abell 26cff7ebf4 fix(plugins): record webhook deliveries whose body holds NUL (#16985)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Plugins receive inbound webhooks on `POST
/api/plugins/:pluginId/webhooks/:endpointKey`
> - The route records each delivery in `plugin_webhook_deliveries`
before it calls the worker
> - The row keeps the parsed body and the headers in jsonb columns
> - PostgreSQL refuses a jsonb value that holds U+0000 or an unpaired
UTF-16 surrogate (22P05 "unsupported Unicode escape sequence")
> - So a delivery whose body has a NUL fails at the insert. No row is
written, the worker is not called, and the sender gets a 500 and retries
into the same failure
> - This pull request stores a jsonb-safe copy of the body and headers,
and gives the worker the request as sent
> - The benefit is that such a delivery is recorded and handled

## Linked Issues or Issue Description

**What happened?**
A plugin webhook delivery whose JSON body held `\^@` failed with
`PostgresError: unsupported Unicode escape sequence` (code 22P05) at
`insert into "plugin_webhook_deliveries"`. The route answered 500 and
did not call the worker. A provider that retries on 5xx sent the same
delivery again, and it failed again.

**Expected behavior**
The route records the delivery, calls the worker with the body as sent,
and answers 200.

**Steps to reproduce**
1. Install a plugin that has the `webhooks.receive` capability and
declares a webhook endpoint.
2. Send `{"text":"a\^@b"}` with `Content-Type: application/json` to
`POST /api/plugins/<pluginId>/webhooks/<endpointKey>`.
3. The answer is 500, and `plugin_webhook_deliveries` has no row for the
delivery.

**Paperclip version or commit**
`master` at efcc8a7d8. `server/src/routes/plugins.ts` is the same on the
current `master`.

Refs #16918 (the same 22P05 error class on run status writes; this pull
request uses its rule: drop NUL, turn an unpaired surrogate into U+FFFD)
Refs #16782 (what the delivery row stores)

## What Changed

- `server/src/routes/plugins.ts`: add the exported helper
`pluginWebhookJsonbValue()`. It reads the `JSON.stringify` text that the
driver sends. It drops each real `\^@` escape. It replaces each `\udXXX`
escape (the form `JSON.stringify` gives an unpaired surrogate) with `�`.
This applies to keys and values. An escaped backslash before a literal
`u0000` stays unchanged. A clean value comes back unchanged (the same
object).
- The delivery insert stores `pluginWebhookJsonbValue(payload)` and
`pluginWebhookJsonbValue(rawHeaders)`. The worker call does not change:
it still gets `rawBody` and `parsedBody` as sent, so a signature check
on the raw body does not change.
- `server/src/__tests__/plugin-webhook-unicode.test.ts`: unit tests for
the helper, and a route test on embedded Postgres that posts a body with
a NUL and an unpaired surrogate.

## Verification

- `cd server && npx vitest run
src/__tests__/plugin-webhook-unicode.test.ts`: 3 passed (embedded
Postgres included).
- With the change to `plugins.ts` reverted, the route test fails with
`expected 500 to be 200`, which is the reported error.
- `tsc --noEmit` in `server/`: no errors in the changed files.

## Risks

- Low risk. The stored copy is different only when the body or headers
hold a NUL or an unpaired surrogate. PostgreSQL refuses those values
today, so no such row exists now.
- A reader of `plugin_webhook_deliveries.payload` sees the NUL dropped.
The worker and the signature check see the request as sent.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- Provider: Anthropic. Model: Claude Opus 5.5 (`claude-opus-5-5`), with
tool use and code execution, in Claude Code.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Luke Abell <3343530+LukeAbell@users.noreply.github.com>
2026-12-05 00:03:05 -08:00
..