-
Notifications
You must be signed in to change notification settings - Fork 0
fix(kpi): bind provenance log path identity #490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a903974
test(kpi): reject mismatched provenance log path
seonghobae 67866cc
fix(kpi): bind provenance log path identity
seonghobae b7dfe2d
fix(kpi): preserve optional logPath while binding present identity
seonghobae c484a13
test(kpi): cover provenance log path rejection in-process
seonghobae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import { createHash } from "node:crypto"; | ||
| import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join, resolve } from "node:path"; | ||
|
|
||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const originalEnvironment = { ...process.env }; | ||
| const originalArgv = [...process.argv]; | ||
|
|
||
| class ExitSignal extends Error { | ||
| constructor(readonly code: number) { | ||
| super(`EXIT:${code}`); | ||
| } | ||
| } | ||
|
|
||
| function restoreProcessState() { | ||
| for (const key of Object.keys(process.env)) { | ||
| if (!(key in originalEnvironment)) delete process.env[key]; | ||
| } | ||
| Object.assign(process.env, originalEnvironment); | ||
| process.argv.splice(0, process.argv.length, ...originalArgv); | ||
| } | ||
|
|
||
| function writeThirtyDayLog(path: string) { | ||
| const records = [ | ||
| { | ||
| event: "http_request", | ||
| route: "/exchange", | ||
| status_code: 200, | ||
| latency_ms: 120, | ||
| timestamp: "2026-06-01T00:00:00.000Z", | ||
| }, | ||
| { | ||
| event: "http_request", | ||
| route: "/exchange", | ||
| status_code: 200, | ||
| latency_ms: 150, | ||
| timestamp: "2026-07-01T03:00:00.000Z", | ||
| }, | ||
| ]; | ||
| const bytes = Buffer.from(`${records.map((record) => JSON.stringify(record)).join("\n")}\n`); | ||
| writeFileSync(path, bytes); | ||
| return { | ||
| logSha256: createHash("sha256").update(bytes).digest("hex"), | ||
| logBytes: bytes.byteLength, | ||
| }; | ||
| } | ||
|
|
||
| async function runStrictGate(logPath: string, provenancePath: string, evidencePath: string) { | ||
| restoreProcessState(); | ||
| Object.assign(process.env, { | ||
| NOEMA_KPI_PROVENANCE_PATH: provenancePath, | ||
| NOEMA_KPI_EVIDENCE_PATH: evidencePath, | ||
| NOEMA_KPI_STRICT: "1", | ||
| NOEMA_KPI_REQUIRE_WINDOW_DAYS: "30", | ||
| }); | ||
| process.argv.splice( | ||
| 0, | ||
| process.argv.length, | ||
| originalArgv[0] ?? process.execPath, | ||
| resolve(process.cwd(), "scripts/kpi-gate.mjs"), | ||
| logPath, | ||
| ); | ||
|
|
||
| vi.resetModules(); | ||
| const logs: string[] = []; | ||
| const logSpy = vi.spyOn(console, "log").mockImplementation((...values: unknown[]) => { | ||
| logs.push(values.map(String).join(" ")); | ||
| }); | ||
| const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); | ||
| const exitSpy = vi.spyOn(process, "exit").mockImplementation(((code?: number) => { | ||
| throw new ExitSignal(code ?? 0); | ||
| }) as never); | ||
|
|
||
| let exitCode: number | null = null; | ||
| try { | ||
| await import("../scripts/kpi-gate.mjs"); | ||
| } catch (error) { | ||
| if (error instanceof ExitSignal) exitCode = error.code; | ||
| else throw error; | ||
| } finally { | ||
| exitSpy.mockRestore(); | ||
| logSpy.mockRestore(); | ||
| errorSpy.mockRestore(); | ||
| restoreProcessState(); | ||
| } | ||
| return { exitCode, logs }; | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| restoreProcessState(); | ||
| }); | ||
|
|
||
| describe("strict KPI provenance log path identity", () => { | ||
| it("rejects provenance that claims a different logPath from the bytes being verified", async () => { | ||
| const directory = mkdtempSync(join(tmpdir(), "noema-kpi-log-path-identity-")); | ||
| try { | ||
| const logPath = join(directory, "exchange-30d.ndjson"); | ||
| const provenancePath = `${logPath}.provenance.json`; | ||
| const evidencePath = join(directory, "evidence.json"); | ||
| const identity = writeThirtyDayLog(logPath); | ||
| writeFileSync( | ||
| provenancePath, | ||
| `${JSON.stringify({ | ||
| sourceKind: "production", | ||
| sourceId: "cloudflare-logpush:noema-production", | ||
| sourceMethod: "log-url", | ||
| logPath: join(directory, "different-production-log.ndjson"), | ||
| records: 2, | ||
| collectedAt: "2026-07-02T00:00:00.000Z", | ||
| ...identity, | ||
| }, null, 2)}\n`, | ||
| ); | ||
|
|
||
| const result = await runStrictGate(logPath, provenancePath, evidencePath); | ||
|
|
||
| expect(result.exitCode).toBe(1); | ||
| expect(result.logs.join("\n")).toContain( | ||
| "KPI provenance logPath must exactly identify the production log being verified", | ||
| ); | ||
| } finally { | ||
| rmSync(directory, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Info: logPath check skipped when field absent
The check at kpi-gate.mjs runs only when
logPathis present, so provenance omitting it bypasses the binding. The bytes, digest, and record count of the verified log are still checked independently, so this is only advisory metadata. Non-string values fail closed.Was this helpful? React with 👍 or 👎 to provide feedback.