Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

fix(lead): reject null JSON payloads as client errors

Jun516715bcf456e43
diff --git a/.github/scripts/functions-lead-test.mjs b/.github/scripts/functions-lead-test.mjs
new file mode 100644
index 000000000..56ddcdbef
--- /dev/null
+++ b/.github/scripts/functions-lead-test.mjs
@@ -0,0 +1,88 @@
+// Plain-Node request validation tests for /lead. No network or Cloudflare
+// account: storage is an in-memory R2 stand-in.
+// node .github/scripts/functions-lead-test.mjs
+import assert from 'node:assert/strict';
+import { readFileSync } from 'node:fs';
+import { test } from 'node:test';
+
+// Import the real source as an ES module without changing package.json's
+// module mode or keeping a second copy of the handler in the test.
+const source = readFileSync(new URL('../../functions/lead.js', import.meta.url), 'utf8');
+const { onRequestPost } = await import(`data:text/javascript;base64,${Buffer.from(source).toString('base64')}`);
+
+function fakeR2(fail = false) {
+ const store = new Map();
+ let attempts = 0;
+ return {
+ store,
+ get attempts() { return attempts; },
+ async put(key, value) {
+ attempts++;
+ if (fail) throw new Error('R2 unavailable');
+ store.set(key, value);
+ },
+ };
+}
+
+async function post(raw, env) {
+ return onRequestPost({
+ request: new Request('https://xerj.org/lead', {
+ method: 'POST',
+ headers: { 'content-type': 'application/json' },
+ body: raw,
+ }),
+ env,
+ });
+}
+
+const invalid = [
+ ['JSON null', 'null'],
+ ['malformed JSON', '{'],
+ ['missing email', '{}'],
+ ['null email', '{"email":null}'],
+ ['numeric email', '{"email":42}'],
+ ['boolean email', '{"email":true}'],
+ ['object email', '{"email":{}}'],
+ ['empty email array', '{"email":[]}'],
+];
+for (const [name, raw] of invalid) {
+ test(`${name} returns 400 without writing`, async () => {
+ const LEADS = fakeR2();
+ const response = await post(raw, { LEADS });
+ assert.equal(response.status, 400);
+ assert.deepEqual(await response.json(), { ok: false, error: 'invalid email' });
+ assert.equal(LEADS.attempts, 0);
+ assert.equal(LEADS.store.size, 0);
+ });
+}
+
+test('valid email is normalized and stored once', async () => {
+ const LEADS = fakeR2();
+ const response = await post('{"email":" [email protected] "}', { LEADS });
+ assert.equal(response.status, 200);
+ assert.deepEqual(await response.json(), { ok: true, stored: true });
+ assert.equal(response.headers.get('content-type'), 'application/json');
+ assert.equal(response.headers.get('cache-control'), 'no-store');
+ assert.equal(LEADS.attempts, 1);
+ assert.equal(LEADS.store.size, 1);
+ const [[key, value]] = LEADS.store;
+ assert.ok(key.startsWith('leads/'));
+ const record = JSON.parse(value);
+ assert.equal(record.email, '[email protected]');
+ assert.equal(record.source, 'landing');
+});
+
+test('valid email without a bucket keeps the existing unstored response', async () => {
+ const response = await post('{"email":"[email protected]"}', {});
+ assert.equal(response.status, 200);
+ assert.deepEqual(await response.json(), { ok: true, stored: false });
+});
+
+test('a real storage failure still returns 500', async () => {
+ const LEADS = fakeR2(true);
+ const response = await post('{"email":"[email protected]"}', { LEADS });
+ assert.equal(response.status, 500);
+ assert.deepEqual(await response.json(), { ok: false, error: 'Error: R2 unavailable' });
+ assert.equal(LEADS.attempts, 1);
+ assert.equal(LEADS.store.size, 0);
+});
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 82b8d2a34..285015530 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -832,6 +832,9 @@ jobs:
- name: Install counter behaviour + privacy (functions/get.js)
run: node .github/scripts/functions-get-test.mjs
+ - name: Lead request validation (functions/lead.js)
+ run: node .github/scripts/functions-lead-test.mjs
+
- name: Install Rust
uses: dtolnay/rust-toolchain@032958afbdc797a9164d3bc0b56325c1308924a5 # 1.97.1
diff --git a/functions/lead.js b/functions/lead.js
index 9c16eabc4..1a0f03997 100644
--- a/functions/lead.js
+++ b/functions/lead.js
@@ -9,7 +9,7 @@ const JSON_HEADERS = { 'content-type': 'application/json', 'cache-control': 'no-
export async function onRequestPost({ request, env }) {
try {
- const body = await request.json().catch(() => ({}));
+ const body = (await request.json().catch(() => ({}))) ?? {};
const email = String(body.email || '').trim().toLowerCase();
if (!email || !email.includes('@') || email.length > 254) {
return new Response(JSON.stringify({ ok: false, error: 'invalid email' }), { status: 400, headers: JSON_HEADERS });