/** * OutputSchemaGuard * * Validates LLM structured outputs (JSON, function calls) before they * reach downstream systems (databases, APIs, UIs). * * Addresses OWASP LLM05: Improper Output Handling. * * Why: LLMs can produce structured outputs containing: * - Unexpected actions ("delete_all" instead of "search") * - Injection in JSON values flowing to downstream parsers * - Hallucinated function calls that don't match available tools * - Hidden instructions in field values for downstream systems */ export interface OutputSchemaGuardConfig { /** Expected output schemas keyed by action/function name */ schemas?: Record; /** Scan all string values for injection patterns (default: true) */ scanForInjection?: boolean; /** Reject outputs with fields not in schema (default: false) */ strictSchema?: boolean; /** Max output size in characters (default: 100000) */ maxOutputSize?: number; } export interface OutputSchema { type: "object" | "array" | "string"; properties?: Record; required?: string[]; } export interface OutputSchemaResult { allowed: boolean; reason?: string; violations: string[]; schema_valid: boolean; injection_found: boolean; threats: Array<{ field: string; type: string; detail: string; }>; } export declare class OutputSchemaGuard { private config; constructor(config?: OutputSchemaGuardConfig); /** * Validate LLM structured output */ validate(output: any, schemaName?: string, requestId?: string): OutputSchemaResult; /** * Validate a function/tool call output from LLM */ validateFunctionCall(functionName: string, args: Record, requestId?: string): OutputSchemaResult; /** * Register a schema for an action/function */ registerSchema(name: string, schema: OutputSchema): void; private validateAgainstSchema; private scanForInjection; private safeStringify; }