Repository navigation
Conversation
🦋 Changeset detectedLatest commit: 4b62ec5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 17 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/registry-moderation
@emdash-cms/registry-verification
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
There was a problem hiding this comment.
This is a clean, additive feature that solves a real pain point: native plugin routes need the unparsed body to verify webhook signatures and to handle non-JSON deliveries. The approach is idiomatic (opt-in flag on the route, dispatcher reads text() once, handler conditionally exposes rawBody) and fits EmDash's existing route flow without changing behavior for existing routes.
I did not run the test suite, linter, or builds (no shell/runtime access), so I take the author's test/type-check claims as unverified. Code-wise the change is straightforward, but I spotted two gaps worth addressing before merge:
-
The implementation in
emdash-runtime.tslacks an integration test. The new tests only exercisePluginRouteHandler.invokeand passrawBodyin manually; they don't verify thatEmDashRuntime.handlePluginApiRouteactually readsrequest.text(), buffers the string, parses JSON when valid, and leavesctx.inputundefined for non-JSON payloads. The repo already has an integration test following this exact pattern (plugin-media-route.test.ts), so a matchingplugin-raw-body-route.test.tswould close the gap. -
Docs/comments claim "exact raw bytes" while
ctx.rawBodyis a decoded string. Webhook signatures are HMACs over bytes, andrequest.text()/ctx.rawBody: stringis a UTF-8 decoding step away from bytes. In practice Stripe/GitHub/Svix deliver UTF-8 JSON, so re-encoding withnew TextEncoder()works, but the wording is technically incorrect and could mislead users handling binary or non-UTF8 payloads. Consider softening the claim or documenting the UTF-8 assumption.
Neither is a blocker, but both are worth a quick pass.
|
|
||
| // Buffer the body as text so routes with `rawBody: true` can see the | ||
| // exact bytes (webhook signature verification); parse JSON from the | ||
| // same buffer for `ctx.input`. | ||
| let body: unknown = undefined; | ||
| let rawBody: string | undefined; | ||
| try { | ||
| body = await request.json(); | ||
| rawBody = await request.text(); |
There was a problem hiding this comment.
[suggestion] The new unit tests in routes.test.ts pass rawBody directly into PluginRouteHandler.invoke; they don't exercise this block, which is the real behavioral change of the PR. There is already an integration-test pattern for EmDashRuntime.handlePluginApiRoute (see packages/core/tests/integration/runtime/plugin-media-route.test.ts). Adding an integration test would verify that request.text() is read once, JSON is parsed into ctx.input, non-JSON payloads leave ctx.input undefined, and ctx.rawBody is only populated for opted-in routes.
There was a problem hiding this comment.
Done — added tests/integration/runtime/plugin-raw-body-route.test.ts exercising handlePluginApiRoute end to end: (1) JSON payload parsed into ctx.input while ctx.rawBody keeps the exact delivered text (whitespace/key order preserved), (2) non-JSON payload leaves ctx.input undefined with ctx.rawBody intact, (3) ctx.rawBody stays undefined for a route that did not opt in.
|
|
||
| // Buffer the body as text so routes with `rawBody: true` can see the | ||
| // exact bytes (webhook signature verification); parse JSON from the |
There was a problem hiding this comment.
[suggestion] The comment says this buffers the body so routes can see the "exact bytes" for webhook signatures, but request.text() decodes the bytes to a UTF-8 string and ctx.rawBody is typed as string. For the common webhook case (UTF-8 JSON) this is fine — users can re-encode with new TextEncoder() — but for non-UTF8 or binary payloads it is not byte-exact. Consider softening the comment to "exact text" or documenting the UTF-8 assumption.
There was a problem hiding this comment.
Already addressed — the comment and both doc sections now describe ctx.rawBody as the body "as a UTF-8 decoded string" and explicitly note that binary/non-UTF8 bodies are not preserved byte-exactly (with the TextEncoder re-encode path for signature libraries).
| - **Native route handlers take a single argument** — `(ctx: RouteContext)` where `ctx.input`, `ctx.request`, and `ctx.requestMeta` are merged with the regular `PluginContext` properties. This is the opposite of standard format's two-argument shape. See [API routes](/plugins/creating-plugins/api-routes/) for the full surface (everything else is identical). | ||
|
|
||
| ## Raw request bodies (webhook signatures) | ||
|
|
There was a problem hiding this comment.
[suggestion] This says webhook providers sign the "exact raw bytes", but ctx.rawBody is a UTF-8 decoded string rather than bytes. In practice most webhook payloads are UTF-8 JSON and signature libraries accept a string, but the wording over-promises for binary or non-UTF8 deliveries. Consider changing to "exact raw text" or adding a note that ctx.rawBody is the UTF-8 decoded body.
There was a problem hiding this comment.
Already addressed — the comment and both doc sections now describe ctx.rawBody as the body "as a UTF-8 decoded string" and explicitly note that binary/non-UTF8 bodies are not preserved byte-exactly (with the TextEncoder re-encode path for signature libraries).
| }, | ||
| }, | ||
| ``` | ||
|
|
There was a problem hiding this comment.
[suggestion] Same wording issue as line 155: "exactly as received" implies the original byte stream, while ctx.rawBody is a decoded string. A small doc tweak would set the right expectation.
There was a problem hiding this comment.
Already addressed — the comment and both doc sections now describe ctx.rawBody as the body "as a UTF-8 decoded string" and explicitly note that binary/non-UTF8 bodies are not preserved byte-exactly (with the TextEncoder re-encode path for signature libraries).
|
Both points addressed in 4d62748:
|
Overlapping PRsThis PR modifies files that are also changed by other open PRs:
This may cause merge conflicts or duplicated work. A maintainer will coordinate. |
There was a problem hiding this comment.
This is a clean, additive re-review. The feature — opt-in rawBody: true for native plugin routes — is the right answer to the webhook-signature problem and is implemented in the existing route flow without changing behavior for existing routes.
Both gaps from the previous review are now closed:
-
Integration test added.
plugin-raw-body-route.test.tsexercises the full runtime path:EmDashRuntime.handlePluginApiRoutereadsrequest.text()once, parses JSON intoctx.inputfrom the same buffer, passes the raw string only to opted-in routes, and leaves non-JSON payloads available asctx.rawBodywithctx.inputundefined. -
"Exact raw bytes" wording corrected. The docs, code comment, and JSDoc now describe
ctx.rawBodyas a UTF-8 decoded string and explicitly note that binary bodies are not preserved byte-exactly. That matches whatrequest.text()actually delivers.
What I checked: the runtime dispatcher change in packages/core/src/emdash-runtime.ts, the conditional rawBody exposure in packages/core/src/plugins/routes.ts, the RouteContext/PluginRoute type additions, the new unit and integration tests, the changeset, and the doc page. I also confirmed that sandboxed plugins are intentionally untouched (they still go through handleSandboxedRoute with the original request), and that no AGENTS.md conventions are violated — no admin UI strings, no SQL, no new API envelopes, no content-table queries, and the changeset is user-facing.
I did not run the test suite, linter, or builds (no shell access), so I take the author's test/type-check claims as unverified. Statically, the code looks correct and the prior review feedback is fully addressed.
|
The |
4f6dbfe to
227ff77
Compare
|
Rebased onto current Full suite: 6444 passed. @marcusbellamyshaw-cell the conflict is cleared — happy to hear how it behaves against your downstream plugin. |
There was a problem hiding this comment.
This is the right change for the webhook-signature problem: an opt-in rawBody: true flag that surfaces a UTF-8 decoded request body to native plugin routes, without affecting existing routes or sandboxed plugins. I read the dispatcher change in packages/core/src/emdash-runtime.ts, the conditional exposure in packages/core/src/plugins/routes.ts, the RouteContext/PluginRoute type additions, the new unit and integration tests, the changeset, and the doc update.
Statically the implementation looks correct: one request.text() read, JSON parsed from the same buffer, rawBody forwarded only to routes that opt in, and bodyless methods keep rawBody undefined. Tests cover the full runtime dispatcher path, non-JSON payloads, and the opt-out case. The changeset is user-facing and the AGENTS.md conventions (no admin UI strings, no SQL, no new logged-out queries, no API envelope change) are respected.
The only unresolved issue is a misleading claim in the public docs. The doc page still says webhook providers sign the payload “exactly as delivered,” which contradicts the later note that ctx.rawBody is a UTF-8 decoded string and binary bodies are not preserved byte-exactly. The prior review stated this wording was corrected, but the current doc paragraph still contains the contradiction and could lead users to attempt binary signature verification through ctx.rawBody.
|
|
||
| ## Raw request bodies (webhook signatures) | ||
|
|
||
| The dispatcher parses the request body before your handler runs and exposes it as `ctx.input`; the body stream is consumed, so `ctx.request.text()` is unavailable. That's fine for normal routes — but webhook providers (Stripe, GitHub, Svix, …) sign the payload _exactly as delivered_, and an HMAC computed over a re-serialized `ctx.input` will never match (whitespace and key order don't survive a parse/stringify round-trip). |
There was a problem hiding this comment.
[needs fixing] The paragraph says webhook providers sign the payload “exactly as delivered,” but the same section later states that ctx.rawBody is a UTF-8 decoded string and that binary bodies are not preserved byte-exactly. Those two claims are contradictory: “exactly as delivered” implies byte fidelity, which request.text() does not provide. A reader might rely on this and attempt binary HMAC verification through ctx.rawBody.
| The dispatcher parses the request body before your handler runs and exposes it as `ctx.input`; the body stream is consumed, so `ctx.request.text()` is unavailable. That's fine for normal routes — but webhook providers (Stripe, GitHub, Svix, …) sign the payload _exactly as delivered_, and an HMAC computed over a re-serialized `ctx.input` will never match (whitespace and key order don't survive a parse/stringify round-trip). | |
| The dispatcher parses the request body before your handler runs and exposes it as `ctx.input`; the body stream is consumed, so `ctx.request.text()` is unavailable. That's fine for normal routes — but webhook providers (Stripe, GitHub, Svix, …) sign the payload they send as UTF-8 text, and an HMAC computed over a re-serialized `ctx.input` will never match (whitespace and key order don't survive a parse/stringify round-trip). |
| /** | ||
| * Like {@link parseRouteInput}, but for body methods also returns the body | ||
| * text the input was parsed from, so routes with `rawBody: true` can see the | ||
| * payload as delivered (webhook signature verification). One `text()` read, |
There was a problem hiding this comment.
[suggestion] The JSDoc says routes can see the “payload as delivered,” which is vaguer than the RouteContext JSDoc that explicitly calls it a UTF-8 decoded string and notes the binary-body caveat. Aligning the wording here makes the implementation comment consistent with the public type docs and avoids future maintainers assuming byte-exact preservation.
| * payload as delivered (webhook signature verification). One `text()` read, | |
| * UTF-8 decoded payload (e.g. for webhook signature verification). One `text()` read, |
There was a problem hiding this comment.
This is the right change for the webhook-signature problem: an opt-in rawBody: true flag that surfaces the UTF-8 decoded request body to native plugin routes without affecting existing routes. The approach is additive, scoped to native/trusted routes only, and leaves sandboxed plugins unchanged as documented.
I re-read the dispatcher change in packages/core/src/emdash-runtime.ts, the conditional exposure in packages/core/src/plugins/routes.ts, the RouteContext/PluginRoute type additions, the new unit and integration tests, the changeset, and the docs update.
Implementation checks out: one request.text() read per body-method request on native routes, JSON parsed from the same buffer, rawBody forwarded only to routes that set rawBody: true, and bodyless methods keep rawBody undefined. parseRouteInput is preserved as a thin wrapper, so existing sandboxed-route and helper callers see unchanged behavior. Tests cover the full dispatcher path, byte-exact text forwarding, non-JSON payloads, and the opt-out case. No new queries, no SQL, no API envelope change, no admin strings, and no logged-out hot-path cost.
The prior review raised one unresolved documentation concern: the docs said webhook providers signed the payload “exactly as delivered,” which conflicted with the note that ctx.rawBody is UTF-8 decoded and binary bodies are not byte-exact. The checked-out docs now read “webhook providers … sign the payload they send as UTF-8 text” and explicitly state “binary bodies are not preserved byte-exactly,” so that contradiction is resolved.
No blockers remain.
Adds a rawBody route option that preserves the delivered body as a UTF-8 string on ctx.rawBody so webhook handlers can verify payload signatures; ctx.input keeps working from the same buffer.
22b725c to
4b62ec5
Compare
|
Rebased onto current main to clear the conflict — the native-plugin tutorial was restructured upstream, so the raw-body docs section now lives under "Native route handlers" as a subsection. No code changes. |
|
Superseded by #3190, which covers the webhook-signature use case with declared request body modes ( |
What does this PR do?
Adds an opt-in
rawBody: trueflag for native plugin routes. Routes that set it receive the unparsed request body asctx.rawBody, alongside the parsedctx.input.Why: webhook providers (Stripe, GitHub, Svix, …) sign the exact raw bytes of the delivery. Since the dispatcher consumes the body stream to build
ctx.input(andctx.request.text()is guarded per #1293), plugins currently have no way to verify those signatures — an HMAC over a re-serializedctx.inputnever matches. Non-JSON payloads (form-encoded webhook deliveries) are also silently lost today.How: the dispatcher already buffers the body to parse it. It now reads
text()once, parsesctx.inputfrom the same buffer, and forwards the string only to routes that opted in. No extra I/O, no behavior change for existing routes. The #1293 guard message now points atrawBody: trueas the escape hatch. Native format only; sandboxed plugins cross a serialization boundary and would need separate plumbing.Discussion: #1982
Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain. (n/a — no admin UI changes)AI-generated code disclosure
Screenshots / test output
New tests cover: raw body delivered on opted-in routes (byte-exact, alongside validated input),
ctx.rawBodyundefined without the flag, and non-JSON payloads reaching the handler withinputundefined.