fix(codex/synthesize/verify): neutralize prompt injection + harden error/edge paths

CRITICAL: untrusted extracted text fenced (inert-data delimiter + sentinel neutralize) before codex calls that run with headless bypass. HIGH: codexJson + listRecords JSON.parse guarded; fingerprint/atomFingerprint scoped by cluster id (no silent skip on shared/empty source refs); headOk uses a fresh timeout per request; synthesize reports degraded when --deep falls back. Codex scratch in one private 0700 mkdtemp. verifyDeep now covered by a stub-codex selftest.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Зарубин Филипп
2026-06-18 16:52:10 +03:00
co-authored by Claude Opus 4.8
parent 45c03a499a
commit 6714bec65a
3 changed files with 100 additions and 33 deletions
+50 -13
View File
@@ -4,7 +4,7 @@
// network: HEAD reachability + a codex web cross-check of the claim.
// ponytail: local gate is deliberately structural (no HEAD by default) so the
// conservative pipeline never reaches the network. Reachability+LLM only on --deep.
import { codexAvailable, codexJson } from './mnemazine-codex.mjs'
import { codexAvailable, codexJson, fenceUntrusted } from './mnemazine-codex.mjs'
// Local gate: a source URL present means the claim is at least anchored
// ("assumed"); none means we cannot back it at all ("unknown"). Never claims
@@ -25,18 +25,24 @@ const VERIFY_SCHEMA = {
}
}
async function headOk(url, timeoutMs) {
async function fetchWithTimeout(url, opts, timeoutMs) {
const ctrl = new AbortController()
const t = setTimeout(() => ctrl.abort(), timeoutMs)
try {
const ctrl = new AbortController()
const t = setTimeout(() => ctrl.abort(), timeoutMs)
let res = await fetch(url, { method: 'HEAD', redirect: 'follow', signal: ctrl.signal }).catch(() => null)
// Some servers reject HEAD — fall back to a ranged GET.
if (!res || !res.ok) {
res = await fetch(url, { method: 'GET', redirect: 'follow', headers: { Range: 'bytes=0-0' }, signal: ctrl.signal }).catch(() => null)
}
return await fetch(url, { ...opts, redirect: 'follow', signal: ctrl.signal }).catch(() => null)
} finally {
clearTimeout(t)
return !!res && res.ok
} catch { return false }
}
}
async function headOk(url, timeoutMs) {
let res = await fetchWithTimeout(url, { method: 'HEAD' }, timeoutMs)
// Some servers reject HEAD — fall back to a ranged GET with a FRESH timeout
// (a shared aborted controller would otherwise kill the fallback instantly).
if (!res || !res.ok) {
res = await fetchWithTimeout(url, { method: 'GET', headers: { Range: 'bytes=0-0' } }, timeoutMs)
}
return !!res && res.ok
}
// Deep gate: confirm reachability, then ask codex (with its own web search) to
@@ -55,8 +61,7 @@ export async function verifyDeep(claim, urls = [], options = {}) {
try {
const prompt = `Verify whether the SOURCES below support the CLAIM. Use web search to check. Return status: "verified" only if a source clearly supports the claim, "assumed" if a source is relevant but does not clearly confirm it, "unknown" if no source supports it. Be strict.
CLAIM:
${String(claim).slice(0, 4000)}
${fenceUntrusted('CLAIM', String(claim).slice(0, 4000))}
SOURCES:
${live.length ? live.join('\n') : '(none reachable)'}`
@@ -69,11 +74,43 @@ ${live.length ? live.join('\n') : '(none reachable)'}`
}
}
// Internal: exercise the codex cross-check path against MNEMAZINE_CODEX_BIN
// (used by --selftest with a stub bin; no real network/LLM). No reachable URL,
// so headOk yields [] and the verdict comes purely from the stubbed codex JSON.
if (process.argv.includes('--deep-once')) {
const verdict = await verifyDeep('stub claim', [], { codexTimeoutMs: 10000 })
console.log(JSON.stringify(verdict))
process.exit(0)
}
// Self-check: run `node scripts/mnemazine-verify.mjs --selftest`
if (process.argv.includes('--selftest')) {
const a = verifyLocal(['https://x.test'])
const b = verifyLocal([])
if (a.status !== 'assumed') throw new Error('expected assumed for url present')
if (b.status !== 'unknown') throw new Error('expected unknown for no url')
// Deep path with a stub codex bin: confirm codexJson round-trips and the
// status enum is honoured end-to-end (was entirely untested before).
const { promises: fsp } = await import('node:fs')
const os = await import('node:os')
const path = await import('node:path')
const { spawnSync } = await import('node:child_process')
const { fileURLToPath } = await import('node:url')
const dir = await fsp.mkdtemp(path.join(os.tmpdir(), 'mnemazine-verify-selftest-'))
const bin = path.join(dir, 'fakecodex')
await fsp.writeFile(bin, `#!/usr/bin/env bash
out=""; while [ $# -gt 0 ]; do [ "$1" = "-o" ] && { out="$2"; shift; }; shift; done
cat >/dev/null
printf '%s' '{"status":"verified","evidence":"stub confirms","checked":["https://e.test"]}' > "$out"
`, { mode: 0o755 })
const res = spawnSync(process.execPath, [fileURLToPath(import.meta.url), '--deep-once'], {
env: { ...process.env, MNEMAZINE_CODEX_BIN: bin }, encoding: 'utf8'
})
await fsp.rm(dir, { recursive: true, force: true })
if (res.status !== 0) throw new Error(`deep-once failed: ${res.stderr}`)
const verdict = JSON.parse(res.stdout)
if (verdict.status !== 'verified') throw new Error(`expected verified from stub codex, got ${verdict.status}`)
console.log('verify selftest ok')
}