- Relax Fastify/AJV schema on ingest events (additionalProperties: true) so CRM/deploy smokes sending extra keys no longer fail validation with 400. - Add integration test when ERROR_REPORTING_TEST_DATABASE_URL is set. - Document BIT-SMOKE / BIT-931 triage in RUNBOOK_INGEST_DEBUG; note in ingest-hardening-runbook. Paperclip-Agent: 0b1fcf92-5fe3-4ac0-9f29-df4ab72f5376 Paperclip-Issue: 35542fda-44d4-4154-b95d-1a7de3566204 Paperclip-Run: 3682ff13-02fc-4749-a460-6414830dccc7 Made-with: Cursor
348 lines
11 KiB
TypeScript
348 lines
11 KiB
TypeScript
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
import mysql from "mysql2/promise";
|
|
import type { RowDataPacket } from "mysql2";
|
|
import { buildApp } from "./buildApp.js";
|
|
import type { IngestEnv } from "./config/env.js";
|
|
import { sha256Hex } from "./lib/hash.js";
|
|
|
|
const _testUrl = process.env.ERROR_REPORTING_TEST_DATABASE_URL;
|
|
|
|
describe.skipIf(!_testUrl)("two-project passive ingest (BIT-179)", () => {
|
|
let _pool: mysql.Pool;
|
|
let _app: Awaited<ReturnType<typeof buildApp>>;
|
|
const _tokenA = "er_tst_a_secret_one";
|
|
const _tokenB = "er_tst_b_secret_two";
|
|
const _tokenC = "er_tst_c_rate_limited_key";
|
|
let _projectIdA = "";
|
|
let _projectIdB = "";
|
|
|
|
beforeAll(async () => {
|
|
_pool = mysql.createPool({ uri: _testUrl!, connectionLimit: 4, waitForConnections: true });
|
|
const _c = await _pool.getConnection();
|
|
try {
|
|
await _c.query("SET FOREIGN_KEY_CHECKS = 0");
|
|
await _c.query(`TRUNCATE TABLE er_ingest_idempotency`);
|
|
await _c.query(`TRUNCATE TABLE er_incident_occurrences`);
|
|
await _c.query(`TRUNCATE TABLE er_incidents`);
|
|
await _c.query(`TRUNCATE TABLE er_ingest_keys`);
|
|
await _c.query(`TRUNCATE TABLE er_projects`);
|
|
await _c.query("SET FOREIGN_KEY_CHECKS = 1");
|
|
} finally {
|
|
_c.release();
|
|
}
|
|
await _pool.execute(`INSERT INTO er_projects (slug, name) VALUES ('pa', 'Project A')`);
|
|
await _pool.execute(`INSERT INTO er_projects (slug, name) VALUES ('pb', 'Project B')`);
|
|
const [rows] = await _pool.execute<RowDataPacket[]>(
|
|
`SELECT CAST(id AS CHAR) AS id, slug FROM er_projects WHERE slug IN ('pa', 'pb') ORDER BY slug`,
|
|
);
|
|
if (rows.length < 2) {
|
|
throw new Error("seed projects failed");
|
|
}
|
|
_projectIdA = String((rows[0] as { id: string }).id);
|
|
_projectIdB = String((rows[1] as { id: string }).id);
|
|
await _pool.execute(
|
|
`INSERT INTO er_ingest_keys (project_id, label, token_prefix, token_hash, status)
|
|
VALUES (?, 'default', 'tok_pr_a', ?, 'active')`,
|
|
[_projectIdA, sha256Hex(_tokenA)],
|
|
);
|
|
await _pool.execute(
|
|
`INSERT INTO er_ingest_keys (project_id, label, token_prefix, token_hash, status)
|
|
VALUES (?, 'default', 'tok_pr_b', ?, 'active')`,
|
|
[_projectIdB, sha256Hex(_tokenB)],
|
|
);
|
|
await _pool.execute(
|
|
`INSERT INTO er_ingest_keys (project_id, label, token_prefix, token_hash, status, rate_limit_max, rate_limit_window_ms)
|
|
VALUES (?, 'ratelimit', 'tok_rl_c', ?, 'active', 2, 60000)`,
|
|
[_projectIdA, sha256Hex(_tokenC)],
|
|
);
|
|
const _env: IngestEnv = {
|
|
PORT: 0,
|
|
DATABASE_URL: _testUrl!,
|
|
INGEST_RATE_LIMIT_MAX: 10_000,
|
|
INGEST_RATE_LIMIT_WINDOW_MS: 60_000,
|
|
INGEST_ANON_IP_MAX: 10_000,
|
|
INGEST_ANON_IP_WINDOW_MS: 60_000,
|
|
NODE_ENV: "test",
|
|
};
|
|
_app = await buildApp({ env: _env, pool: _pool });
|
|
await _app.ready();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await _app.close();
|
|
await _pool.end();
|
|
});
|
|
|
|
it("ingests distinct events for project A and B with separate keys", async () => {
|
|
const _resA = await _app.inject({
|
|
method: "POST",
|
|
url: "/v1/ingest/events",
|
|
headers: { authorization: `Bearer ${_tokenA}` },
|
|
payload: {
|
|
events: [
|
|
{
|
|
schema_version: 1,
|
|
occurred_at: "2026-03-22T10:00:00.000Z",
|
|
level: "error",
|
|
message: "failure from project A",
|
|
fingerprint: "fp-a-1",
|
|
},
|
|
],
|
|
},
|
|
});
|
|
expect(_resA.statusCode).toBe(200);
|
|
const _bodyA = JSON.parse(_resA.body) as {
|
|
results: Array<{ incident_id: string; fingerprint: string }>;
|
|
};
|
|
expect(_bodyA.results).toHaveLength(1);
|
|
expect(_bodyA.results[0]?.fingerprint).toBe("fp-a-1");
|
|
|
|
const _resB = await _app.inject({
|
|
method: "POST",
|
|
url: "/v1/ingest/events",
|
|
headers: { "x-api-key": _tokenB },
|
|
payload: {
|
|
events: [
|
|
{
|
|
schema_version: 1,
|
|
occurred_at: "2026-03-22T10:00:01.000Z",
|
|
level: "warn",
|
|
message: "warning from project B",
|
|
fingerprint: "fp-b-1",
|
|
},
|
|
],
|
|
},
|
|
});
|
|
expect(_resB.statusCode).toBe(200);
|
|
const _bodyB = JSON.parse(_resB.body) as {
|
|
results: Array<{ incident_id: string; fingerprint: string }>;
|
|
};
|
|
expect(_bodyB.results[0]?.fingerprint).toBe("fp-b-1");
|
|
expect(_bodyB.results[0]?.incident_id).not.toBe(_bodyA.results[0]?.incident_id);
|
|
|
|
const [checkA] = await _pool.execute<RowDataPacket[]>(
|
|
`SELECT COUNT(*) AS c FROM er_incidents WHERE project_id = ?`,
|
|
[_projectIdA],
|
|
);
|
|
expect(Number((checkA[0] as { c: number }).c)).toBe(1);
|
|
const [checkB] = await _pool.execute<RowDataPacket[]>(
|
|
`SELECT COUNT(*) AS c FROM er_incidents WHERE project_id = ?`,
|
|
[_projectIdB],
|
|
);
|
|
expect(Number((checkB[0] as { c: number }).c)).toBe(1);
|
|
});
|
|
|
|
it("accepts unknown event-level fields (forward-compatible for CRM / deploy 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:05:00.000Z",
|
|
level: "error",
|
|
message: "probe with extra keys",
|
|
fingerprint: "fp-extra-keys-1",
|
|
release: "1.0.0",
|
|
environment: "production",
|
|
},
|
|
],
|
|
},
|
|
});
|
|
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-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 = ?`,
|
|
[_projectIdA],
|
|
);
|
|
const _nBefore = Number((before[0] as { c: number }).c);
|
|
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:30:00.000Z",
|
|
level: "error",
|
|
message: "ResizeObserver loop completed with undelivered notifications.",
|
|
},
|
|
],
|
|
},
|
|
});
|
|
expect(_res.statusCode).toBe(200);
|
|
const _body = JSON.parse(_res.body) as {
|
|
noise_filtered_count: number;
|
|
results: Array<{ skipped?: boolean; rule_id?: string }>;
|
|
};
|
|
expect(_body.noise_filtered_count).toBe(1);
|
|
expect(_body.results[0]?.skipped).toBe(true);
|
|
expect(_body.results[0]?.rule_id).toBe("resize_observer_loop");
|
|
const [after] = await _pool.execute<RowDataPacket[]>(
|
|
`SELECT COUNT(*) AS c FROM er_incidents WHERE project_id = ?`,
|
|
[_projectIdA],
|
|
);
|
|
expect(Number((after[0] as { c: number }).c)).toBe(_nBefore);
|
|
});
|
|
|
|
it("replays identical idempotent requests", async () => {
|
|
const _payload = {
|
|
events: [
|
|
{
|
|
schema_version: 1,
|
|
occurred_at: "2026-03-22T11:00:00.000Z",
|
|
level: "info",
|
|
message: "idempotent row",
|
|
fingerprint: "fp-idem-1",
|
|
},
|
|
],
|
|
};
|
|
const _headers = {
|
|
authorization: `Bearer ${_tokenA}`,
|
|
"idempotency-key": "idem-key-1",
|
|
};
|
|
const _first = await _app.inject({
|
|
method: "POST",
|
|
url: "/v1/ingest/events",
|
|
headers: _headers,
|
|
payload: _payload,
|
|
});
|
|
expect(_first.statusCode).toBe(200);
|
|
const _second = await _app.inject({
|
|
method: "POST",
|
|
url: "/v1/ingest/events",
|
|
headers: _headers,
|
|
payload: _payload,
|
|
});
|
|
expect(_second.statusCode).toBe(200);
|
|
expect(_second.body).toBe(_first.body);
|
|
const [occ] = await _pool.execute<RowDataPacket[]>(
|
|
`SELECT COUNT(*) AS c FROM er_incident_occurrences`,
|
|
);
|
|
expect(Number((occ[0] as { c: number }).c)).toBe(3);
|
|
});
|
|
|
|
it("returns 409 when idempotency key is reused with a different body", async () => {
|
|
const _headers = {
|
|
authorization: `Bearer ${_tokenA}`,
|
|
"idempotency-key": "idem-key-2",
|
|
};
|
|
const _r1 = await _app.inject({
|
|
method: "POST",
|
|
url: "/v1/ingest/events",
|
|
headers: _headers,
|
|
payload: {
|
|
events: [
|
|
{
|
|
schema_version: 1,
|
|
occurred_at: "2026-03-22T12:00:00.000Z",
|
|
level: "error",
|
|
message: "first",
|
|
fingerprint: "fp-conflict-a",
|
|
},
|
|
],
|
|
},
|
|
});
|
|
expect(_r1.statusCode).toBe(200);
|
|
const _r2 = await _app.inject({
|
|
method: "POST",
|
|
url: "/v1/ingest/events",
|
|
headers: _headers,
|
|
payload: {
|
|
events: [
|
|
{
|
|
schema_version: 1,
|
|
occurred_at: "2026-03-22T12:00:00.000Z",
|
|
level: "error",
|
|
message: "second different",
|
|
fingerprint: "fp-conflict-b",
|
|
},
|
|
],
|
|
},
|
|
});
|
|
expect(_r2.statusCode).toBe(409);
|
|
});
|
|
|
|
it("returns 429 when per-key ingest limit is exceeded (er_ingest_keys rate columns)", async () => {
|
|
const _payload = (fp: string) => ({
|
|
events: [
|
|
{
|
|
schema_version: 1,
|
|
occurred_at: "2026-03-22T13:00:00.000Z",
|
|
level: "error",
|
|
message: "rate limit probe",
|
|
fingerprint: fp,
|
|
},
|
|
],
|
|
});
|
|
const _h = { authorization: `Bearer ${_tokenC}` };
|
|
const _r1 = await _app.inject({
|
|
method: "POST",
|
|
url: "/v1/ingest/events",
|
|
headers: _h,
|
|
payload: _payload("fp-rl-1"),
|
|
});
|
|
const _r2 = await _app.inject({
|
|
method: "POST",
|
|
url: "/v1/ingest/events",
|
|
headers: _h,
|
|
payload: _payload("fp-rl-2"),
|
|
});
|
|
const _r3 = await _app.inject({
|
|
method: "POST",
|
|
url: "/v1/ingest/events",
|
|
headers: _h,
|
|
payload: _payload("fp-rl-3"),
|
|
});
|
|
expect(_r1.statusCode).toBe(200);
|
|
expect(_r2.statusCode).toBe(200);
|
|
expect(_r3.statusCode).toBe(429);
|
|
const _j = JSON.parse(_r3.body) as { error?: string };
|
|
expect(_j.error).toBe("rate_limited");
|
|
});
|
|
|
|
it("returns 429 for anonymous callers after the IP budget (INGEST_ANON_*)", async () => {
|
|
const _env: IngestEnv = {
|
|
PORT: 0,
|
|
DATABASE_URL: _testUrl!,
|
|
INGEST_RATE_LIMIT_MAX: 10_000,
|
|
INGEST_RATE_LIMIT_WINDOW_MS: 60_000,
|
|
INGEST_ANON_IP_MAX: 2,
|
|
INGEST_ANON_IP_WINDOW_MS: 60_000,
|
|
NODE_ENV: "test",
|
|
};
|
|
const _anonApp = await buildApp({ env: _env, pool: _pool });
|
|
await _anonApp.ready();
|
|
const _payload = {
|
|
events: [
|
|
{
|
|
schema_version: 1,
|
|
occurred_at: "2026-03-22T14:00:00.000Z",
|
|
level: "error",
|
|
message: "no auth",
|
|
fingerprint: "fp-anon",
|
|
},
|
|
],
|
|
};
|
|
expect((await _anonApp.inject({ method: "POST", url: "/v1/ingest/events", payload: _payload })).statusCode).toBe(
|
|
401,
|
|
);
|
|
expect((await _anonApp.inject({ method: "POST", url: "/v1/ingest/events", payload: _payload })).statusCode).toBe(
|
|
401,
|
|
);
|
|
const _third = await _anonApp.inject({ method: "POST", url: "/v1/ingest/events", payload: _payload });
|
|
expect(_third.statusCode).toBe(429);
|
|
await _anonApp.close();
|
|
});
|
|
});
|