From 1c65ed31587954e1107db189276a240aa4fb9b84 Mon Sep 17 00:00:00 2001 From: Peter Spark Date: Sun, 5 Apr 2026 04:35:50 +0000 Subject: [PATCH] ingest-api: allow unknown top-level keys on ingest batch (BIT-933) The Fastify JSON Schema for POST /v1/ingest/events used additionalProperties:false on the root object. CRM direct-forward smokes that attach batch metadata beside events were rejected with HTTP 400 before the handler. - ingestJsonSchemas.ts: shared event and batch schemas - ingestJsonSchemas.test.ts: Ajv regression without MySQL - integration.twoProjects.test.ts: end-to-end case when DB URL set - docs/RUNBOOK_INGEST_DEBUG.md: document batch-level forwards Paperclip-Agent: 0b1fcf92-5fe3-4ac0-9f29-df4ab72f5376 Paperclip-Issue: BIT-933 Paperclip-Run: f027c017-4cbf-4819-84bb-85922e5c0407 Made-with: Cursor --- docs/RUNBOOK_INGEST_DEBUG.md | 2 +- ingest-api/package-lock.json | 1 + ingest-api/package.json | 1 + ingest-api/src/buildApp.ts | 34 +--------------- ingest-api/src/ingestJsonSchemas.test.ts | 30 ++++++++++++++ ingest-api/src/ingestJsonSchemas.ts | 39 +++++++++++++++++++ .../src/integration.twoProjects.test.ts | 27 +++++++++++++ 7 files changed, 101 insertions(+), 33 deletions(-) create mode 100644 ingest-api/src/ingestJsonSchemas.test.ts create mode 100644 ingest-api/src/ingestJsonSchemas.ts diff --git a/docs/RUNBOOK_INGEST_DEBUG.md b/docs/RUNBOOK_INGEST_DEBUG.md index 84ca40d..d605f2a 100644 --- a/docs/RUNBOOK_INGEST_DEBUG.md +++ b/docs/RUNBOOK_INGEST_DEBUG.md @@ -17,7 +17,7 @@ 2. **URL:** must be the ingest service base (no path suffix in env) โ€” clients append `/v1/ingest/events` themselves (see [clients/php/ErrorReporter.php](../clients/php/ErrorReporter.php), [HTTP_CONTRACTS.md](HTTP_CONTRACTS.md)). 3. **Credentials:** `Authorization: Bearer ` or `X-Api-Key`; key must be **active** and **unexpired** in `er_ingest_keys` (ยง2). 4. **HTTP codes:** `401` / `429` / `5xx` / timeouts โ€” use sections below; CRM cURL timeouts are short (e.g. 4s) โ€” slow proxies can fail smokes before the hub responds. -5. **HTTP 400 from schema validation** should be rare; the hub ignores unknown **event-level** JSON keys and only requires `schema_version`, `occurred_at`, `level`, `message`. +5. **HTTP 400 from schema validation** should be rare; the hub ignores unknown **event-level** JSON keys and unknown **top-level** keys on the batch object (only `events` is required). Required per event: `schema_version`, `occurred_at`, `level`, `message`. **After the root cause is fixed** diff --git a/ingest-api/package-lock.json b/ingest-api/package-lock.json index bc5954f..7520b59 100644 --- a/ingest-api/package-lock.json +++ b/ingest-api/package-lock.json @@ -17,6 +17,7 @@ }, "devDependencies": { "@types/node": "^22.9.0", + "ajv": "^8.18.0", "tsx": "^4.19.2", "typescript": "^5.6.3", "vitest": "^2.1.0" diff --git a/ingest-api/package.json b/ingest-api/package.json index 0807ab2..c3af015 100644 --- a/ingest-api/package.json +++ b/ingest-api/package.json @@ -22,6 +22,7 @@ }, "devDependencies": { "@types/node": "^22.9.0", + "ajv": "^8.18.0", "tsx": "^4.19.2", "typescript": "^5.6.3", "vitest": "^2.1.0" diff --git a/ingest-api/src/buildApp.ts b/ingest-api/src/buildApp.ts index 080339e..3a96722 100644 --- a/ingest-api/src/buildApp.ts +++ b/ingest-api/src/buildApp.ts @@ -16,37 +16,7 @@ import prometheusPlugin, { ingestEventsTotal, ingestBatchSize, } from "./plugins/prometheus.js"; - -const _ingestEventSchema = { - type: "object", - /** Ignore unknown keys so integrators (CRM smoke checks, SDKs) can add forward-compatible metadata without 400. */ - additionalProperties: true, - required: ["schema_version", "occurred_at", "level", "message"], - properties: { - schema_version: { type: "integer", minimum: 1, maximum: 1 }, - occurred_at: { type: "string", minLength: 1, maxLength: 64 }, - level: { type: "string", minLength: 1, maxLength: 32 }, - message: { type: "string", minLength: 1, maxLength: 20000 }, - fingerprint: { type: "string", minLength: 1, maxLength: 128 }, - stacktrace: { type: "string", minLength: 1, maxLength: 500000 }, - stack_trace: { type: "string", minLength: 1, maxLength: 500000 }, - context: { type: "object", additionalProperties: true }, - }, -} as const; - -const _ingestBodySchema = { - type: "object", - additionalProperties: false, - required: ["events"], - properties: { - events: { - type: "array", - minItems: 1, - maxItems: 50, - items: _ingestEventSchema, - }, - }, -} as const; +import { ingestBatchBodyJsonSchema } from "./ingestJsonSchemas.js"; const _ingestResultStoredSchema = { type: "object", @@ -167,7 +137,7 @@ export async function buildApp(input: { env: IngestEnv; pool: Pool }): Promise { + const _ajv = new Ajv({ strict: false, allErrors: true }); + const _validate = _ajv.compile(ingestBatchBodyJsonSchema); + + it("allows unknown top-level properties (CRM direct-forward metadata)", () => { + const _ok = _validate({ + events: [ + { + schema_version: 1, + occurred_at: "2026-03-22T10:00:00.000Z", + level: "error", + message: "x", + }, + ], + forward_source: "crm_smoke", + }); + expect(_ok).toBe(true); + }); + + it("still requires events", () => { + const _ok = _validate({ + forward_source: "crm_smoke", + }); + expect(_ok).toBe(false); + }); +}); diff --git a/ingest-api/src/ingestJsonSchemas.ts b/ingest-api/src/ingestJsonSchemas.ts new file mode 100644 index 0000000..5c019b1 --- /dev/null +++ b/ingest-api/src/ingestJsonSchemas.ts @@ -0,0 +1,39 @@ +/** + * Fastify/OpenAPI JSON schemas for POST /v1/ingest/events. + * Kept in one module so contract tests can run without MySQL. + */ + +export const ingestEventJsonSchema = { + type: "object", + /** Ignore unknown keys so integrators (CRM smoke checks, SDKs) can add forward-compatible metadata without 400. */ + additionalProperties: true, + required: ["schema_version", "occurred_at", "level", "message"], + properties: { + schema_version: { type: "integer", minimum: 1, maximum: 1 }, + occurred_at: { type: "string", minLength: 1, maxLength: 64 }, + level: { type: "string", minLength: 1, maxLength: 32 }, + message: { type: "string", minLength: 1, maxLength: 20000 }, + fingerprint: { type: "string", minLength: 1, maxLength: 128 }, + stacktrace: { type: "string", minLength: 1, maxLength: 500000 }, + stack_trace: { type: "string", minLength: 1, maxLength: 500000 }, + context: { type: "object", additionalProperties: true }, + }, +} as const; + +export const ingestBatchBodyJsonSchema = { + type: "object", + /** + * CRM `/smoke/crm.php` direct-forward and other clients may attach batch-level + * metadata; the handler only reads `events`. + */ + additionalProperties: true, + required: ["events"], + properties: { + events: { + type: "array", + minItems: 1, + maxItems: 50, + items: ingestEventJsonSchema, + }, + }, +} as const; diff --git a/ingest-api/src/integration.twoProjects.test.ts b/ingest-api/src/integration.twoProjects.test.ts index 8d1b9db..16526e0 100644 --- a/ingest-api/src/integration.twoProjects.test.ts +++ b/ingest-api/src/integration.twoProjects.test.ts @@ -159,6 +159,33 @@ describe.skipIf(!_testUrl)("two-project passive ingest (BIT-179)", () => { expect(_body.results[0]?.fingerprint).toBe("fp-extra-keys-1"); }); + it("accepts unknown top-level batch fields (CRM direct-forward smokes)", async () => { + const _res = await _app.inject({ + method: "POST", + url: "/v1/ingest/events", + headers: { authorization: `Bearer ${_tokenA}` }, + payload: { + events: [ + { + schema_version: 1, + occurred_at: "2026-03-22T10:06:00.000Z", + level: "error", + message: "probe with extra batch keys", + fingerprint: "fp-extra-batch-keys-1", + }, + ], + forward_source: "crm_smoke", + client_batch_id: "batch-xyz", + }, + }); + expect(_res.statusCode).toBe(200); + const _body = JSON.parse(_res.body) as { + results: Array<{ incident_id: string; fingerprint: string }>; + }; + expect(_body.results).toHaveLength(1); + expect(_body.results[0]?.fingerprint).toBe("fp-extra-batch-keys-1"); + }); + it("skips known browser noise without inserting rows", async () => { const [before] = await _pool.execute( `SELECT COUNT(*) AS c FROM er_incidents WHERE project_id = ?`,