Files
OmniRoute/open-sse/services/compression/fidelityGateStep.ts
Diego Rodrigues de Sa e Souza aa0ac7da87 docs(security): document full LOCAL_ONLY route set + GHSA-fhh6-4qxv-rpqj + audit path (#5599) (#5748)
Expand ROUTE_GUARD_TIERS.md Tier 1 (LOCAL_ONLY):
- link the GHSA advisory and explain the attack class (RCE via a subprocess spawn
  reachable from non-loopback traffic)
- replace the 3-example prefix table with the full LOCAL_ONLY set, mirroring
  LOCAL_ONLY_API_PREFIXES / LOCAL_ONLY_API_PATTERNS in routeGuard.ts (the
  authoritative source; check-route-guard-membership enforces the code side)
- add an "Operator guidance & auditing" section for users behind
  nginx/Cloudflare/Tailscale: don't forge X-Forwarded-For loopback, keep the
  manage-scope bypass minimal, and how to audit non-loopback access

Docs-only; SECURITY.md already links here.

Closes #5599
2026-07-01 01:09:20 -03:00

45 lines
1.8 KiB
TypeScript

import { extractTextContent } from "./messageContent.ts";
import { checkFidelity, type FidelityGateConfig } from "./fidelityGate.ts";
import type { CompressionResult } from "./types.ts";
import type { StackAccumulator } from "./stackedStepCore.ts";
import { getCompressionEngine } from "./engines/registry.ts";
function bodyToText(body: Record<string, unknown>): string {
const messages = body.messages;
if (!Array.isArray(messages)) return "";
return messages
.map((m) => extractTextContent((m as { content?: unknown }).content as never))
.join("\n");
}
/**
* Fidelity gate (opt-in, independent of TV1). Called at each advance point AFTER mergeStackStep
* pushed the step's breakdown entry. Off → zero-cost `return true` (byte-identical legacy). On a
* fidelity failure it marks the just-pushed breakdown entry rejected (no advance) and returns false.
*/
export function gateAdvance(
result: CompressionResult,
inputBody: Record<string, unknown>,
fidelityGate: FidelityGateConfig | undefined,
acc: StackAccumulator,
engineId?: string
): boolean {
if (!fidelityGate?.enabled) return true;
if (engineId && getCompressionEngine(engineId)?.sampling) return true; // lossy-by-design, CCR-recoverable
const verdict = checkFidelity(bodyToText(inputBody), bodyToText(result.body), fidelityGate);
if (verdict.passed) return true;
// mergeStackStep only pushed a breakdown entry when result.stats exists; only then is
// acc.breakdown's last entry THIS step's (else it would be the prior engine's — don't touch it).
if (result.stats) {
const last = acc.breakdown[acc.breakdown.length - 1];
if (last) {
last.rejected = true;
last.rejectReason = verdict.detail ?? verdict.failedInvariant;
last.compressedTokens = last.originalTokens;
last.savingsPercent = 0;
}
}
acc.fallbackApplied = true;
return false;
}