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
This commit is contained in:
Peter Spark committed 2026-04-05 04:35:50 +00:00
1 parent d47a061399
commit 1c65ed3158
7 files changed
+101 -33

No files matched your search

+1 -1
View File
@@ -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 <token>` 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**
+1
View File
@@ -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"
+1
View File
@@ -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"
+2 -32
View File
@@ -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<F
description: "Ingest one or more error events for the project bound to the API key.",
tags: ["ingest"],
security: [{ bearerAuth: [] }, { apiKeyAuth: [] }],
body: _ingestBodySchema,
body: ingestBatchBodyJsonSchema,
response: {
200: {
type: "object",
+30
View File
@@ -0,0 +1,30 @@
import { Ajv } from "ajv";
import { describe, expect, it } from "vitest";
import { ingestBatchBodyJsonSchema } from "./ingestJsonSchemas.js";
describe("ingestBatchBodyJsonSchema", () => {
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);
});
});
+39
View File
@@ -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;
@@ -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<RowDataPacket[]>(
`SELECT COUNT(*) AS c FROM er_incidents WHERE project_id = ?`,