Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(legal): stop recording an acceptance for a body we could not read

nirholas6c20a817eea7
diff --git a/api/legal/risk-ack.js b/api/legal/risk-ack.js
index 94d6bdcdb..edb4fb181 100644
--- a/api/legal/risk-ack.js
+++ b/api/legal/risk-ack.js
@@ -2,6 +2,8 @@
//
// POST /api/legal/risk-ack { version, context?, path? }
// → 200 { ok: true, recorded }
+// → 400 invalid_version, 413 payload_too_large,
+// 415 unsupported_media_type when the body is not a readable acceptance
//
// The client-side gate (public/risk-ack.js) fires this after the user accepts
// the real-funds risk acknowledgment. The acceptance itself lives in the
@@ -45,7 +47,19 @@ export default wrap(async function handler(req, res) {
const rl = await limits.publicIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
- const body = await readJson(req, 10_000).catch(() => null);
+ // Swallowing the read error collapsed every body failure into the version
+ // complaint below, so an oversized or non-JSON POST was told its `version`
+ // was wrong when the field was fine and the body was the problem. Report the
+ // real cause (413 oversized, 415 wrong content-type, 400 unparseable).
+ let body;
+ try {
+ body = await readJson(req, 10_000);
+ } catch (err) {
+ const status = err?.status === 413 || err?.status === 415 ? err.status : 400;
+ const code =
+ status === 413 ? 'payload_too_large' : status === 415 ? 'unsupported_media_type' : 'bad_request';
+ return error(res, status, code, err?.message || 'could not read request body');
+ }
const version = Number(body?.version);
if (!Number.isInteger(version) || version < 1 || version > RISK_ACK_VERSION) {
return error(
diff --git a/api/legal/tos-ack.js b/api/legal/tos-ack.js
index 2598886a8..96659e764 100644
--- a/api/legal/tos-ack.js
+++ b/api/legal/tos-ack.js
@@ -2,6 +2,8 @@
//
// POST /api/legal/tos-ack { version?, context?, path? }
// → 200 { ok: true, version, recorded }
+// → 400 invalid_body / invalid_version, 413 payload_too_large,
+// 415 unsupported_media_type when the body is not a readable acceptance
//
// The primary acceptance records are written inline by the auth endpoints
// (register / login / SIWE / SIWS / Privy verify) when the client sends
@@ -36,7 +38,28 @@ export default wrap(async function handler(req, res) {
const rl = await limits.publicIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
- const body = await readJson(req, 10_000).catch(() => null);
+ // An unreadable body must never become a recorded acceptance. Swallowing the
+ // read error left `body` null, and a null body then took the "no version
+ // sent, default to current" path below: a form POST with no JSON
+ // content-type, or a body past the size limit, answered 200 and wrote an
+ // evidentiary "accepted Terms v2" row for a request whose content was never
+ // read. Report what actually went wrong instead (413 oversized, 415 wrong
+ // content-type, 400 unparseable) and record nothing.
+ let body;
+ try {
+ body = await readJson(req, 10_000);
+ } catch (err) {
+ const status = err?.status === 413 || err?.status === 415 ? err.status : 400;
+ const code =
+ status === 413 ? 'payload_too_large' : status === 415 ? 'unsupported_media_type' : 'bad_request';
+ return error(res, status, code, err?.message || 'could not read request body');
+ }
+ // `{}` is a valid acceptance of the current version, but an array or any
+ // other non-object JSON is not an acceptance payload at all; it only reached
+ // the default-version path because `[].version` reads as undefined.
+ if (!body || typeof body !== 'object' || Array.isArray(body)) {
+ return error(res, 400, 'invalid_body', 'body must be a JSON object');
+ }
const raw = Number(body?.version);
const version = Number.isInteger(raw) && raw >= 1 && raw <= TOS_VERSION ? raw : TOS_VERSION;
if (body?.version !== undefined && version !== raw) {
diff --git a/tests/api/legal-tos-ack.test.js b/tests/api/legal-tos-ack.test.js
index e71518e0b..ff542ea64 100644
--- a/tests/api/legal-tos-ack.test.js
+++ b/tests/api/legal-tos-ack.test.js
@@ -44,14 +44,14 @@ const { default: handler } = await import('../../api/legal/tos-ack.js');
// ── Helpers ───────────────────────────────────────────────────────────────────
-function makeReq(body = null) {
- const bodyStr = body ? JSON.stringify(body) : '';
+function makeReq(body = null, { contentType = 'application/json', raw = null } = {}) {
+ const bodyStr = raw ?? (body ? JSON.stringify(body) : '');
const req = Readable.from([Buffer.from(bodyStr)]);
req.method = 'POST';
req.url = '/api/legal/tos-ack';
req.headers = {
host: 'app.test',
- ...(body !== null ? { 'content-type': 'application/json' } : {}),
+ ...(contentType ? { 'content-type': contentType } : {}),
};
return req;
}
@@ -71,8 +71,8 @@ function makeRes() {
};
}
-async function invoke(body) {
- const req = makeReq(body);
+async function invoke(body, opts) {
+ const req = makeReq(body, opts);
const res = makeRes();
await handler(req, res);
// logAudit and the users-table stamp are fire-and-forget microtasks; flush
@@ -173,6 +173,41 @@ describe('POST /api/legal/tos-ack', () => {
expect(body.error).toBe('invalid_version');
});
+ // The three cases below all used to answer 200 and write an evidentiary
+ // "accepted the current Terms" row: the body read failed (or produced a
+ // non-object), and an absent `version` then took the default-to-current
+ // path. A request we could not read is not evidence of an acceptance.
+ it('refuses a body sent without a JSON content-type', async () => {
+ const { status, body } = await invoke(null, {
+ contentType: 'application/x-www-form-urlencoded',
+ raw: 'version=2',
+ });
+ expect(status).toBe(415);
+ expect(body.error).toBe('unsupported_media_type');
+ expect(sqlState.calls).toHaveLength(0);
+ });
+
+ it('refuses a body past the size limit', async () => {
+ const { status, body } = await invoke({ version: TOS_VERSION, pad: 'a'.repeat(11_000) });
+ expect(status).toBe(413);
+ expect(body.error).toBe('payload_too_large');
+ expect(sqlState.calls).toHaveLength(0);
+ });
+
+ it('refuses a JSON body that is not an object', async () => {
+ const { status, body } = await invoke(null, { raw: '[]' });
+ expect(status).toBe(400);
+ expect(body.error).toBe('invalid_body');
+ expect(sqlState.calls).toHaveLength(0);
+ });
+
+ it('refuses an unparseable body', async () => {
+ const { status, body } = await invoke(null, { raw: '{oops' });
+ expect(status).toBe(400);
+ expect(body.error).toBe('bad_request');
+ expect(sqlState.calls).toHaveLength(0);
+ });
+
it('rate limits', async () => {
rlState.success = false;
const { status } = await invoke({});