Harden Mnemazine production gates
This commit is contained in:
+111
-11
@@ -4,6 +4,8 @@
|
||||
// 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 dns from 'node:dns/promises'
|
||||
import net from 'node:net'
|
||||
import { llmAvailable, llmJson, fenceUntrusted } from './mnemazine-llm.mjs'
|
||||
|
||||
// Local gate: a source URL present means the claim is at least anchored
|
||||
@@ -25,11 +27,17 @@ const VERIFY_SCHEMA = {
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchWithTimeout(url, opts, timeoutMs) {
|
||||
async function fetchWithTimeout(url, opts, timeoutMs, redirects = 3) {
|
||||
if (!await safeToFetch(url)) return null
|
||||
const ctrl = new AbortController()
|
||||
const t = setTimeout(() => ctrl.abort(), timeoutMs)
|
||||
try {
|
||||
return await fetch(url, { ...opts, redirect: 'follow', signal: ctrl.signal }).catch(() => null)
|
||||
const res = await fetch(url, { ...opts, redirect: 'manual', signal: ctrl.signal }).catch(() => null)
|
||||
if (redirects > 0 && res && res.status >= 300 && res.status < 400 && res.headers.get('location')) {
|
||||
const next = new URL(res.headers.get('location'), url).toString()
|
||||
return await fetchWithTimeout(next, opts, timeoutMs, redirects - 1)
|
||||
}
|
||||
return res
|
||||
} finally {
|
||||
clearTimeout(t)
|
||||
}
|
||||
@@ -45,29 +53,99 @@ async function headOk(url, timeoutMs) {
|
||||
return !!res && res.ok
|
||||
}
|
||||
|
||||
function privateIPv4(address) {
|
||||
const parts = address.split('.').map(Number)
|
||||
if (parts.length !== 4 || parts.some(n => !Number.isInteger(n) || n < 0 || n > 255)) return true
|
||||
const [a, b] = parts
|
||||
return a === 0 ||
|
||||
a === 10 ||
|
||||
a === 127 ||
|
||||
(a === 169 && b === 254) ||
|
||||
(a === 172 && b >= 16 && b <= 31) ||
|
||||
(a === 192 && b === 168) ||
|
||||
(a === 100 && b >= 64 && b <= 127) ||
|
||||
a >= 224
|
||||
}
|
||||
|
||||
function privateIPv6(address) {
|
||||
const value = address.toLowerCase()
|
||||
const mapped = value.match(/^::ffff:(\d+\.\d+\.\d+\.\d+)$/)
|
||||
if (mapped) return privateIPv4(mapped[1])
|
||||
return value === '::' ||
|
||||
value === '::1' ||
|
||||
value.startsWith('fc') ||
|
||||
value.startsWith('fd') ||
|
||||
value.startsWith('fe80:') ||
|
||||
value.startsWith('ff') ||
|
||||
value.startsWith('2001:db8:')
|
||||
}
|
||||
|
||||
function privateAddress(address) {
|
||||
const cleaned = String(address || '').replace(/^\[|\]$/g, '')
|
||||
const kind = net.isIP(cleaned)
|
||||
if (kind === 4) return privateIPv4(cleaned)
|
||||
if (kind === 6) return privateIPv6(cleaned)
|
||||
return false
|
||||
}
|
||||
|
||||
export function isPublicHttpUrl(url) {
|
||||
let parsed
|
||||
try { parsed = new URL(url) } catch { return false }
|
||||
if (!['http:', 'https:'].includes(parsed.protocol)) return false
|
||||
const hostname = parsed.hostname.replace(/^\[|\]$/g, '').replace(/\.$/, '').toLowerCase()
|
||||
if (!hostname || hostname === 'localhost' || hostname.endsWith('.localhost') || hostname.endsWith('.local')) return false
|
||||
if (privateAddress(hostname)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
async function safeToFetch(url) {
|
||||
if (!isPublicHttpUrl(url)) return false
|
||||
const parsed = new URL(url)
|
||||
const hostname = parsed.hostname.replace(/^\[|\]$/g, '').replace(/\.$/, '').toLowerCase()
|
||||
if (net.isIP(hostname)) return true
|
||||
try {
|
||||
const answers = await dns.lookup(hostname, { all: true, verbatim: true })
|
||||
return answers.length > 0 && answers.every(answer => !privateAddress(answer.address))
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Deep gate: confirm reachability, then ask codex (with its own web search) to
|
||||
// judge whether the listed sources actually support the claim. Degrades to the
|
||||
// local verdict if codex is unavailable or errors.
|
||||
export async function verifyDeep(claim, urls = [], options = {}) {
|
||||
const timeoutMs = options.timeoutMs || 8000
|
||||
const live = []
|
||||
for (const url of (urls || []).filter(Boolean)) {
|
||||
if (await headOk(url, timeoutMs)) live.push(url)
|
||||
const candidateUrls = (urls || []).filter(isPublicHttpUrl)
|
||||
if (options.assumeLiveUrls) {
|
||||
live.push(...candidateUrls)
|
||||
} else {
|
||||
for (const url of candidateUrls) {
|
||||
if (await headOk(url, timeoutMs)) live.push(url)
|
||||
}
|
||||
}
|
||||
if (!llmAvailable(options.provider)) {
|
||||
const local = verifyLocal(live)
|
||||
return { ...local, checked: live, note: `llm unavailable; reachable=${live.length}` }
|
||||
}
|
||||
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.
|
||||
const prompt = `Verify whether the SOURCES below support the CORE CLAIM. Use web search / fetch when needed. Return status:
|
||||
- "verified" if an official source or primary source supports the main factual claim, even if wording differs.
|
||||
- "assumed" if sources are only related or support only background context.
|
||||
- "unknown" if no source supports it.
|
||||
|
||||
Be strict on numbers, rankings, benchmarks, funding, star counts, downloads, dates, and performance claims: those require direct support. For descriptive repository/product capability claims, an official GitHub repo or official docs page is enough if it supports the core claim.
|
||||
|
||||
${fenceUntrusted('CLAIM', String(claim).slice(0, 4000))}
|
||||
|
||||
SOURCES:
|
||||
${live.length ? live.join('\n') : '(none reachable)'}`
|
||||
const res = await llmJson(prompt, VERIFY_SCHEMA, { timeoutMs: options.llmTimeoutMs, provider: options.provider, tools: ['WebSearch', 'WebFetch'] })
|
||||
const status = ['verified', 'assumed', 'unknown'].includes(res?.status) ? res.status : 'unknown'
|
||||
return { status, checked: res?.checked?.length ? res.checked : live, evidence: res?.evidence || '', note: 'llm cross-check' }
|
||||
let status = ['verified', 'assumed', 'unknown'].includes(res?.status) ? res.status : 'unknown'
|
||||
if (status === 'verified' && !live.length) status = 'unknown'
|
||||
const checked = Array.isArray(res?.checked) ? res.checked.filter(isPublicHttpUrl) : []
|
||||
return { status, checked: checked.length ? checked : live, evidence: res?.evidence || '', note: 'llm cross-check' }
|
||||
} catch (err) {
|
||||
const local = verifyLocal(live)
|
||||
return { ...local, checked: live, note: `deep verify failed: ${err.message}` }
|
||||
@@ -78,7 +156,10 @@ ${live.length ? live.join('\n') : '(none reachable)'}`
|
||||
// (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 })
|
||||
const claim = process.env.MNEMAZINE_VERIFY_TEST_CLAIM || 'stub claim'
|
||||
const urls = String(process.env.MNEMAZINE_VERIFY_TEST_URLS || '').split(',').map(s => s.trim()).filter(Boolean)
|
||||
const assumeLiveUrls = process.env.MNEMAZINE_VERIFY_TEST_ASSUME_LIVE === '1'
|
||||
const verdict = await verifyDeep(claim, urls, { llmTimeoutMs: 10000, assumeLiveUrls })
|
||||
console.log(JSON.stringify(verdict))
|
||||
process.exit(0)
|
||||
}
|
||||
@@ -89,6 +170,9 @@ if (process.argv.includes('--selftest')) {
|
||||
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')
|
||||
if (isPublicHttpUrl('http://127.0.0.1/')) throw new Error('loopback URL passed public URL gate')
|
||||
if (isPublicHttpUrl('http://169.254.169.254/latest/meta-data/')) throw new Error('link-local URL passed public URL gate')
|
||||
if (await headOk('http://127.0.0.1/', 50)) throw new Error('loopback URL passed fetch gate')
|
||||
|
||||
// Deep path with a stub codex bin (provider=codex): confirm llmJson round-trips
|
||||
// and the status enum is honoured end-to-end. Claude backend shares the same
|
||||
@@ -103,15 +187,31 @@ if (process.argv.includes('--selftest')) {
|
||||
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"
|
||||
status="\${FAKE_CODEX_STATUS:-verified}"
|
||||
printf '{"status":"%s","evidence":"stub confirms","checked":["https://e.test"]}' "$status" > "$out"
|
||||
`, { mode: 0o755 })
|
||||
const res = spawnSync(process.execPath, [fileURLToPath(import.meta.url), '--deep-once'], {
|
||||
env: { ...process.env, MNEMAZINE_LLM: 'codex', MNEMAZINE_CODEX_BIN: bin }, encoding: 'utf8'
|
||||
env: { ...process.env, MNEMAZINE_LLM: 'codex', MNEMAZINE_CODEX_BIN: bin, MNEMAZINE_VERIFY_TEST_ASSUME_LIVE: '1', MNEMAZINE_VERIFY_TEST_URLS: 'https://e.test' }, 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}`)
|
||||
const assumed = spawnSync(process.execPath, [fileURLToPath(import.meta.url), '--deep-once'], {
|
||||
env: {
|
||||
...process.env,
|
||||
MNEMAZINE_LLM: 'codex',
|
||||
MNEMAZINE_CODEX_BIN: bin,
|
||||
MNEMAZINE_VERIFY_TEST_ASSUME_LIVE: '1',
|
||||
MNEMAZINE_VERIFY_TEST_URLS: 'https://github.com/microsoft/markitdown',
|
||||
MNEMAZINE_VERIFY_TEST_CLAIM: 'MarkItDown is maintained by Microsoft and has unsupported extra benchmark claims.',
|
||||
FAKE_CODEX_STATUS: 'assumed'
|
||||
},
|
||||
encoding: 'utf8'
|
||||
})
|
||||
await fsp.rm(dir, { recursive: true, force: true })
|
||||
if (assumed.status !== 0) throw new Error(`deep-once assumed failed: ${assumed.stderr}`)
|
||||
const assumedVerdict = JSON.parse(assumed.stdout)
|
||||
if (assumedVerdict.status !== 'assumed') throw new Error(`expected assumed to stay assumed, got ${assumedVerdict.status}`)
|
||||
|
||||
console.log('verify selftest ok')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user