Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/31496-email-send-non-object-body.md
Original file line number Diff line number Diff line change
@@ -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`.
9 changes: 7 additions & 2 deletions packages/rest/src/email-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
48 changes: 48 additions & 0 deletions packages/rest/src/email-send-door-bounds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -413,3 +419,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 });
});
});
Loading