@sovr/sql-proxy 0.0.2 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,239 @@
1
+ import { EventEmitter } from 'node:events';
2
+ import { EvalResult, PolicyEngine } from '@sovr/engine';
3
+ export { Channel, EvalRequest, EvalResult, PolicyRule, RiskLevel, SqlContext, Verdict } from '@sovr/engine';
4
+
5
+ /**
6
+ * @sovr/proxy-sql v2.0.0 — SQL Proxy with Full Billing System
7
+ *
8
+ * A transparent SQL proxy that sits between AI agents and databases,
9
+ * intercepting all SQL statements and routing them through the SOVR
10
+ * Policy Engine before execution.
11
+ *
12
+ * Features:
13
+ * 1. PostgreSQL wire protocol interception (frontend ↔ proxy ↔ backend)
14
+ * 2. Statement-level parsing and classification (17 statement types)
15
+ * 3. DDL blocking (DROP, ALTER, TRUNCATE)
16
+ * 4. High-risk DML detection (DELETE/UPDATE without WHERE)
17
+ * 5. SQL injection detection (UNION injection, tautology, comment injection, stacked queries)
18
+ * 6. Table whitelist/blacklist access control
19
+ * 7. Billing Reporter — 7 event types, batch flush, quota check, overage degradation
20
+ * 8. Mandatory API Key enforcement
21
+ * 9. Full audit logging of all intercepted queries
22
+ *
23
+ * Architecture:
24
+ * Agent → SQL Proxy (port 5433) → SOVR Policy Engine → PostgreSQL (port 5432)
25
+ * Billing: Every gate-check/block/allow → billingBuffer → batch POST → /api/v1/metering/batch
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * import { SqlProxy } from '@sovr/proxy-sql';
30
+ * import { PolicyEngine, DEFAULT_RULES } from '@sovr/engine';
31
+ *
32
+ * const engine = new PolicyEngine({ rules: DEFAULT_RULES });
33
+ * const proxy = new SqlProxy({
34
+ * engine,
35
+ * apiKey: process.env.SOVR_API_KEY!,
36
+ * listenPort: 5433,
37
+ * targetHost: 'localhost',
38
+ * targetPort: 5432,
39
+ * tableWhitelist: ['public.orders', 'public.products'],
40
+ * billing: { enabled: true },
41
+ * });
42
+ *
43
+ * await proxy.start();
44
+ * // Agent connects to localhost:5433 instead of 5432
45
+ * // All DDL and dangerous DML are intercepted and billed
46
+ * ```
47
+ */
48
+
49
+ /**
50
+ * SOVR Billing Event Types — aligned with the pricing model:
51
+ * 1. gate.check — low price or included in quota (every policy evaluation)
52
+ * 2. gate.block — usually free or very low price (blocked requests)
53
+ * 3. irreversible.allowed — HIGH PRICE core tax base (allowed irreversible ops)
54
+ * 4. trust_bundle.issued — medium-high price (Trust Bundle generation)
55
+ * 5. trust_bundle.exported — higher price (audit necessity)
56
+ * 6. replay.requested — optional, per-use or monthly (trace replay)
57
+ * 7. auditor.session — enterprise billing (external audit sessions)
58
+ */
59
+ type BillingEventType = 'gate.check' | 'gate.block' | 'irreversible.allowed' | 'trust_bundle.issued' | 'trust_bundle.exported' | 'replay.requested' | 'auditor.session';
60
+ interface BillingEvent {
61
+ event_type: BillingEventType;
62
+ action: string;
63
+ resource: string;
64
+ verdict: string;
65
+ api_key: string;
66
+ timestamp: number;
67
+ metadata?: Record<string, unknown>;
68
+ }
69
+ interface BillingConfig {
70
+ /** Enable billing event reporting (default: true) */
71
+ enabled?: boolean;
72
+ /** Metering endpoint URL (default: SOVR cloud) */
73
+ meteringEndpoint?: string;
74
+ /** Batch flush interval in milliseconds (default: 10000) */
75
+ flushIntervalMs?: number;
76
+ /** Maximum events in buffer before auto-flush (default: 500) */
77
+ bufferMax?: number;
78
+ }
79
+ interface SqlInjectionResult {
80
+ detected: boolean;
81
+ patterns: string[];
82
+ severity: 'none' | 'high' | 'critical';
83
+ }
84
+ interface SqlProxyConfig {
85
+ /** Policy engine instance */
86
+ engine: PolicyEngine;
87
+ /** SOVR API Key (REQUIRED — register at https://sovr.inc/register) */
88
+ apiKey?: string;
89
+ /** Port to listen on */
90
+ listenPort: number;
91
+ /** Target database host */
92
+ targetHost: string;
93
+ /** Target database port */
94
+ targetPort: number;
95
+ /** Listen host (default: 127.0.0.1) */
96
+ listenHost?: string;
97
+ /** Actor ID for audit trail */
98
+ actorId?: string;
99
+ /** Enable verbose logging */
100
+ verbose?: boolean;
101
+ /** Callback when a query is blocked */
102
+ onBlocked?: (info: BlockedQueryInfo) => void | Promise<void>;
103
+ /** Callback for all evaluated queries */
104
+ onEvaluated?: (info: EvaluatedQueryInfo) => void | Promise<void>;
105
+ /** v2.0: Billing configuration */
106
+ billing?: BillingConfig;
107
+ /** v2.0: Table whitelist — only these tables are accessible (if set) */
108
+ tableWhitelist?: string[];
109
+ /** v2.0: Table blacklist — these tables are always blocked */
110
+ tableBlacklist?: string[];
111
+ /** v2.0: Enable SQL injection detection (default: true) */
112
+ sqlInjectionDetection?: boolean;
113
+ }
114
+ interface BlockedQueryInfo {
115
+ sql: string;
116
+ statementType: StatementType;
117
+ tables: string[];
118
+ decision: EvalResult;
119
+ timestamp: number;
120
+ /** v2.0: SQL injection detection result */
121
+ injectionResult?: SqlInjectionResult;
122
+ }
123
+ interface EvaluatedQueryInfo {
124
+ sql: string;
125
+ statementType: StatementType;
126
+ tables: string[];
127
+ decision: EvalResult;
128
+ timestamp: number;
129
+ /** v2.0: SQL injection detection result */
130
+ injectionResult?: SqlInjectionResult;
131
+ }
132
+ interface SqlProxyStats {
133
+ totalQueries: number;
134
+ allowedQueries: number;
135
+ blockedQueries: number;
136
+ escalatedQueries: number;
137
+ activeConnections: number;
138
+ startedAt: number;
139
+ /** v2.0: SQL injection blocks */
140
+ injectionBlocks: number;
141
+ /** v2.0: Table ACL blocks */
142
+ tableAclBlocks: number;
143
+ /** v2.0: Billing stats */
144
+ billingEventsReported: number;
145
+ billingEventsDropped: number;
146
+ billingBufferSize: number;
147
+ /** v2.0: Per-event-type counters */
148
+ billingByType: Record<BillingEventType, number>;
149
+ }
150
+ type StatementType = 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE' | 'DROP' | 'ALTER' | 'TRUNCATE' | 'CREATE' | 'GRANT' | 'REVOKE' | 'BEGIN' | 'COMMIT' | 'ROLLBACK' | 'SET' | 'SHOW' | 'EXPLAIN' | 'COPY' | 'OTHER';
151
+ /** Parsed SQL statement info */
152
+ interface ParsedStatement {
153
+ type: StatementType;
154
+ tables: string[];
155
+ hasWhereClause: boolean;
156
+ isMultiStatement: boolean;
157
+ raw: string;
158
+ }
159
+ /**
160
+ * Lightweight SQL statement parser.
161
+ * Not a full SQL parser — classifies statements and extracts key metadata
162
+ * for policy evaluation without heavy dependencies.
163
+ */
164
+ declare function parseSQL(sql: string): ParsedStatement;
165
+ /**
166
+ * Detect SQL injection patterns in a raw SQL string.
167
+ * Returns detection result with matched pattern names and severity.
168
+ */
169
+ declare function detectSqlInjection(sql: string): SqlInjectionResult;
170
+ declare class SqlProxy extends EventEmitter {
171
+ private readonly engine;
172
+ private readonly apiKey;
173
+ private readonly listenPort;
174
+ private readonly listenHost;
175
+ private readonly targetHost;
176
+ private readonly targetPort;
177
+ private readonly actorId;
178
+ private readonly verbose;
179
+ private readonly onBlocked?;
180
+ private readonly onEvaluated?;
181
+ private server;
182
+ private readonly tableWhitelist;
183
+ private readonly tableBlacklist;
184
+ private readonly sqlInjectionDetection;
185
+ private billingEnabled;
186
+ private billingEndpoint;
187
+ private billingBuffer;
188
+ private billingFlushTimer;
189
+ private readonly BILLING_FLUSH_INTERVAL;
190
+ private readonly BILLING_BUFFER_MAX;
191
+ private stats;
192
+ constructor(config: SqlProxyConfig);
193
+ /**
194
+ * Start the SQL proxy server + billing reporter.
195
+ */
196
+ start(): Promise<void>;
197
+ /**
198
+ * Stop the SQL proxy server + flush remaining billing events.
199
+ */
200
+ stop(): Promise<void>;
201
+ /**
202
+ * Get proxy statistics (including billing stats).
203
+ */
204
+ getStats(): SqlProxyStats;
205
+ private initBillingReporter;
206
+ /**
207
+ * Record a billing event into the buffer.
208
+ * Fire-and-forget — never blocks the main request flow.
209
+ */
210
+ recordBillingEvent(event_type: BillingEventType, action: string, resource: string, verdict: string, metadata?: Record<string, unknown>): void;
211
+ /**
212
+ * Flush billing buffer to the metering endpoint.
213
+ * Batch POST — fire-and-forget with re-queue on failure.
214
+ */
215
+ private flushBillingBuffer;
216
+ /**
217
+ * Classify a gate-check result into the appropriate billing event type.
218
+ * Key pricing principle: "放行的不可逆动作" is the highest-price tax base.
219
+ */
220
+ private classifyBillingEvent;
221
+ /**
222
+ * Record billing event from a policy engine evaluation result.
223
+ * Automatically classifies the event type based on statement type + verdict.
224
+ */
225
+ private recordGateCheckBilling;
226
+ /**
227
+ * Check if the queried tables pass the ACL check.
228
+ * Returns null if allowed, or a reason string if blocked.
229
+ */
230
+ private checkTableAcl;
231
+ private handleConnection;
232
+ private processClientData;
233
+ private evaluateStatement;
234
+ private log;
235
+ /** v2.0: Async version check */
236
+ private _checkVersion;
237
+ }
238
+
239
+ export { type BillingConfig, type BillingEvent, type BillingEventType, type BlockedQueryInfo, type EvaluatedQueryInfo, type ParsedStatement, type SqlInjectionResult, SqlProxy, type SqlProxyConfig, type SqlProxyStats, type StatementType, detectSqlInjection, parseSQL };