From 88e1a4411e92ee366845bd1d07ff8add00325eb1 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Nov 2026 13:02:54 +0000 Subject: [PATCH 1/3] test(rest): pin the email send door refusal of a non-object body Claude-Session: https://claude.ai/code/session_01FEkQ5mNZhe975x6zR3ULqS Co-authored-by: Claude --- .../rest/src/email-send-door-bounds.test.ts | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/packages/rest/src/email-send-door-bounds.test.ts b/packages/rest/src/email-send-door-bounds.test.ts index 6ca65d0c02f..0bf81b24771 100644 --- a/packages/rest/src/email-send-door-bounds.test.ts +++ b/packages/rest/src/email-send-door-bounds.test.ts @@ -413,3 +413,45 @@ describe('POST /api/v1/email/send — identity mail is not sent through this doo expect(input.organizationId).toBe(ORG_ADMIN.tenantId); }); }); + +describe('POST /api/v1/email/send — a body that is not a JSON object is the route\'s own 400, before the service', () => { + // An array is `typeof 'object'`, and a JSON `null` used to be defaulted to + // `{}`; either way the message reached `send` and the service answered + // VALIDATION_FAILED for a request the route itself should have refused. + it.each([ + ['an array of numbers', [1, 2]], + ['an empty array', []], + ['an array holding a well-formed message', [MESSAGE]], + ['a JSON null', null], + ['a JSON string', 'not an object'], + ['a JSON number', 42], + ])('%s is refused 400 INVALID_REQUEST, and nothing is sent', async (_label, body) => { + const { service, send } = emailDouble(); + const { post } = boot({ service, ctx: ADMIN }); + const out = await post(body); + + expect(out.status).toBe(400); + expect(out.body.code).toBe('INVALID_REQUEST'); + expect(send).not.toHaveBeenCalled(); + }); + + it('CONTROL: an object body still reaches the service, as sent', async () => { + const { service, send } = emailDouble(); + const { post } = boot({ service, ctx: ADMIN }); + const out = await post(MESSAGE); + + expect(out.status).toBe(200); + expect(send).toHaveBeenCalledTimes(1); + expect(send.mock.calls[0][0]).toMatchObject(MESSAGE); + }); + + it('CONTROL: an empty object is a message for the service to judge, not a missing body', async () => { + const { service, send } = emailDouble(); + const { post } = boot({ service, ctx: ADMIN }); + const out = await post({}); + + expect(out.status).toBe(200); + expect(send).toHaveBeenCalledTimes(1); + expect(send.mock.calls[0][0]).toEqual({ sentBy: ADMIN.userId }); + }); +}); From 1dfcd3e9c737a98eae95b9eb5c86e68f0a06f9fe Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Nov 2026 13:08:30 +0000 Subject: [PATCH 2/3] fix(rest): refuse a non-object email send body before the service The send route admitted a JSON array (typeof object) and defaulted a JSON null to {}, so both reached the email service and were refused there as VALIDATION_FAILED. A present body must now be a plain object; anything else takes the route's own 400 INVALID_REQUEST. Claude-Session: https://claude.ai/code/session_01FEkQ5mNZhe975x6zR3ULqS Co-authored-by: Claude --- .changeset/31496-email-send-non-object-body.md | 10 ++++++++++ packages/rest/src/email-routes.ts | 9 +++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 .changeset/31496-email-send-non-object-body.md diff --git a/.changeset/31496-email-send-non-object-body.md b/.changeset/31496-email-send-non-object-body.md new file mode 100644 index 00000000000..0cbbfdcc17a --- /dev/null +++ b/.changeset/31496-email-send-non-object-body.md @@ -0,0 +1,10 @@ +--- +'@objectstack/rest': patch +--- + +fix(rest): `POST /api/v1/email/send` refuses a JSON array or `null` body with its own `400 INVALID_REQUEST` before the email service is reached + +Clause-②: no +Metadata-protocol: none + +The send door's body check read `typeof body !== 'object'`, which an array passes, and it defaulted a JSON `null` body to `{}`. Either way the message reached the email service, which refused it as a malformed message (`400 VALIDATION_FAILED`, "subject is required"). The door now admits a present body only when it is a plain JSON object, so an array or `null` gets the same `400 INVALID_REQUEST` ("JSON body required") as a string or a number, and the service is never called. A request with no body is still the empty message for the service to judge, and every object body is forwarded exactly as before. Both kinds of body were refused with a 400 before this change and are refused with a 400 after it; only the code changes, from the service's `VALIDATION_FAILED` to the route's `INVALID_REQUEST`. diff --git a/packages/rest/src/email-routes.ts b/packages/rest/src/email-routes.ts index 09c1a869dfa..626f40efea2 100644 --- a/packages/rest/src/email-routes.ts +++ b/packages/rest/src/email-routes.ts @@ -264,8 +264,13 @@ export function registerEmailEndpoints(this: EmailRouteHost, basePath: string): return; } - const body = req.body ?? {}; - if (!body || typeof body !== 'object') { + // An absent body is the empty message, for the service to + // judge. A body that is present must be a JSON object: an + // array is `typeof 'object'` and a JSON `null` is present, and + // neither is a message, so both take this route's own 400 + // before the service is reached. + const body = req.body === undefined ? {} : req.body; + if (!isPlainObject(body)) { res.status(400).json({ code: 'INVALID_REQUEST', error: 'JSON body required' }); return; } From 0319dc519cf4ad6147279eefde2e4599be5f3953 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Nov 2026 13:47:05 +0000 Subject: [PATCH 3/3] test(rest): pay the objectql load at module top in the email door suite Claude-Session: https://claude.ai/code/session_01FEkQ5mNZhe975x6zR3ULqS Co-authored-by: Claude --- packages/rest/src/email-send-door-bounds.test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/rest/src/email-send-door-bounds.test.ts b/packages/rest/src/email-send-door-bounds.test.ts index 0bf81b24771..e60f39d9942 100644 --- a/packages/rest/src/email-send-door-bounds.test.ts +++ b/packages/rest/src/email-send-door-bounds.test.ts @@ -15,6 +15,12 @@ * each admission case proves what it was called WITH. */ +// The server under test reaches `@objectstack/objectql` only through a dynamic +// import inside a handler (`meta-object-state-routes.ts`), so the first case to +// call one would pay that module graph's load inside its clocked window. This +// module-top import pays it during collection instead. +import '@objectstack/objectql'; + import { describe, it, expect, vi } from 'vitest'; import { HonoHttpServer } from '@objectstack/plugin-hono-server'; import type { EmailAddress, IEmailService, SendEmailInput } from '@objectstack/spec/contracts';