@conarium-ai/core 0.1.0

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.
Files changed (188) hide show
  1. package/CHANGELOG.md +131 -0
  2. package/LICENSE +21 -0
  3. package/README.md +387 -0
  4. package/SECURITY.md +226 -0
  5. package/bin/conarium-anchor-service.mjs +77 -0
  6. package/bin/conarium-anchor-upgrade.mjs +108 -0
  7. package/bin/conarium-coverage.mjs +425 -0
  8. package/bin/conarium-doctor.mjs +480 -0
  9. package/bin/conarium-init.mjs +184 -0
  10. package/bin/conarium-reconcile.mjs +441 -0
  11. package/bin/conarium-stamp.mjs +161 -0
  12. package/bin/conarium-verify.mjs +610 -0
  13. package/conarium.config.json +43 -0
  14. package/deploy/anchor-service/README.md +79 -0
  15. package/deploy/anchor-service/conarium-anchor.service +21 -0
  16. package/deploy/anchor-service/dry-run.mjs +122 -0
  17. package/deploy/anchor-service/ecosystem.config.cjs +21 -0
  18. package/deploy/anchor-service/env.example +11 -0
  19. package/dist/anchor-service.d.ts +39 -0
  20. package/dist/anchor-service.d.ts.map +1 -0
  21. package/dist/anchor-service.js +278 -0
  22. package/dist/anchor-service.js.map +1 -0
  23. package/dist/anchor.d.ts +123 -0
  24. package/dist/anchor.d.ts.map +1 -0
  25. package/dist/anchor.js +246 -0
  26. package/dist/anchor.js.map +1 -0
  27. package/dist/audit-hash.d.ts +4 -0
  28. package/dist/audit-hash.d.ts.map +1 -0
  29. package/dist/audit-hash.js +12 -0
  30. package/dist/audit-hash.js.map +1 -0
  31. package/dist/audit.d.ts +104 -0
  32. package/dist/audit.d.ts.map +1 -0
  33. package/dist/audit.js +391 -0
  34. package/dist/audit.js.map +1 -0
  35. package/dist/config.d.ts +273 -0
  36. package/dist/config.d.ts.map +1 -0
  37. package/dist/config.js +44 -0
  38. package/dist/config.js.map +1 -0
  39. package/dist/connectors/docs.d.ts +23 -0
  40. package/dist/connectors/docs.d.ts.map +1 -0
  41. package/dist/connectors/docs.js +140 -0
  42. package/dist/connectors/docs.js.map +1 -0
  43. package/dist/connectors/index.d.ts +10 -0
  44. package/dist/connectors/index.d.ts.map +1 -0
  45. package/dist/connectors/index.js +37 -0
  46. package/dist/connectors/index.js.map +1 -0
  47. package/dist/connectors/jira.d.ts +16 -0
  48. package/dist/connectors/jira.d.ts.map +1 -0
  49. package/dist/connectors/jira.js +67 -0
  50. package/dist/connectors/jira.js.map +1 -0
  51. package/dist/connectors/openapi.d.ts +28 -0
  52. package/dist/connectors/openapi.d.ts.map +1 -0
  53. package/dist/connectors/openapi.js +447 -0
  54. package/dist/connectors/openapi.js.map +1 -0
  55. package/dist/connectors/postgres.d.ts +17 -0
  56. package/dist/connectors/postgres.d.ts.map +1 -0
  57. package/dist/connectors/postgres.js +131 -0
  58. package/dist/connectors/postgres.js.map +1 -0
  59. package/dist/connectors/slack.d.ts +16 -0
  60. package/dist/connectors/slack.d.ts.map +1 -0
  61. package/dist/connectors/slack.js +65 -0
  62. package/dist/connectors/slack.js.map +1 -0
  63. package/dist/connectors/supabase_rest.d.ts +35 -0
  64. package/dist/connectors/supabase_rest.d.ts.map +1 -0
  65. package/dist/connectors/supabase_rest.js +153 -0
  66. package/dist/connectors/supabase_rest.js.map +1 -0
  67. package/dist/connectors/zion.d.ts +24 -0
  68. package/dist/connectors/zion.d.ts.map +1 -0
  69. package/dist/connectors/zion.js +76 -0
  70. package/dist/connectors/zion.js.map +1 -0
  71. package/dist/console.d.ts +30 -0
  72. package/dist/console.d.ts.map +1 -0
  73. package/dist/console.js +257 -0
  74. package/dist/console.js.map +1 -0
  75. package/dist/coverage.d.ts +100 -0
  76. package/dist/coverage.d.ts.map +1 -0
  77. package/dist/coverage.js +175 -0
  78. package/dist/coverage.js.map +1 -0
  79. package/dist/executor.d.ts +12 -0
  80. package/dist/executor.d.ts.map +1 -0
  81. package/dist/executor.js +77 -0
  82. package/dist/executor.js.map +1 -0
  83. package/dist/governance.d.ts +124 -0
  84. package/dist/governance.d.ts.map +1 -0
  85. package/dist/governance.js +977 -0
  86. package/dist/governance.js.map +1 -0
  87. package/dist/http.d.ts +48 -0
  88. package/dist/http.d.ts.map +1 -0
  89. package/dist/http.js +221 -0
  90. package/dist/http.js.map +1 -0
  91. package/dist/index.d.ts +3 -0
  92. package/dist/index.d.ts.map +1 -0
  93. package/dist/index.js +25 -0
  94. package/dist/index.js.map +1 -0
  95. package/dist/keys.d.ts +51 -0
  96. package/dist/keys.d.ts.map +1 -0
  97. package/dist/keys.js +174 -0
  98. package/dist/keys.js.map +1 -0
  99. package/dist/license.d.ts +35 -0
  100. package/dist/license.d.ts.map +1 -0
  101. package/dist/license.js +122 -0
  102. package/dist/license.js.map +1 -0
  103. package/dist/llm-gate.d.ts +17 -0
  104. package/dist/llm-gate.d.ts.map +1 -0
  105. package/dist/llm-gate.js +19 -0
  106. package/dist/llm-gate.js.map +1 -0
  107. package/dist/openapi_connector.d.ts +6 -0
  108. package/dist/openapi_connector.d.ts.map +1 -0
  109. package/dist/openapi_connector.js +145 -0
  110. package/dist/openapi_connector.js.map +1 -0
  111. package/dist/pseudonymize.d.ts +19 -0
  112. package/dist/pseudonymize.d.ts.map +1 -0
  113. package/dist/pseudonymize.js +25 -0
  114. package/dist/pseudonymize.js.map +1 -0
  115. package/dist/rate_limit.d.ts +41 -0
  116. package/dist/rate_limit.d.ts.map +1 -0
  117. package/dist/rate_limit.js +66 -0
  118. package/dist/rate_limit.js.map +1 -0
  119. package/dist/receipt.d.ts +161 -0
  120. package/dist/receipt.d.ts.map +1 -0
  121. package/dist/receipt.js +210 -0
  122. package/dist/receipt.js.map +1 -0
  123. package/dist/scripts/discover_schema.d.ts +2 -0
  124. package/dist/scripts/discover_schema.d.ts.map +1 -0
  125. package/dist/scripts/discover_schema.js +75 -0
  126. package/dist/scripts/discover_schema.js.map +1 -0
  127. package/dist/scripts/dogfood_prove.d.ts +2 -0
  128. package/dist/scripts/dogfood_prove.d.ts.map +1 -0
  129. package/dist/scripts/dogfood_prove.js +41 -0
  130. package/dist/scripts/dogfood_prove.js.map +1 -0
  131. package/dist/search_policy.d.ts +23 -0
  132. package/dist/search_policy.d.ts.map +1 -0
  133. package/dist/search_policy.js +79 -0
  134. package/dist/search_policy.js.map +1 -0
  135. package/dist/server.d.ts +29 -0
  136. package/dist/server.d.ts.map +1 -0
  137. package/dist/server.js +379 -0
  138. package/dist/server.js.map +1 -0
  139. package/dist/tokens.d.ts +14 -0
  140. package/dist/tokens.d.ts.map +1 -0
  141. package/dist/tokens.js +58 -0
  142. package/dist/tokens.js.map +1 -0
  143. package/dist/types.d.ts +132 -0
  144. package/dist/types.d.ts.map +1 -0
  145. package/dist/types.js +2 -0
  146. package/dist/types.js.map +1 -0
  147. package/docs/ARCHITECTURE.md +42 -0
  148. package/docs/CONSENT-BINDING-SPEC.md +136 -0
  149. package/docs/CONSENT-BINDING-SPEC.md.anchors.jsonl +1 -0
  150. package/docs/PRIOR-ART.md +213 -0
  151. package/docs/PRIOR-ART.md.anchors.jsonl +3 -0
  152. package/docs/RECEIPT-SPEC.md +301 -0
  153. package/docs/RECEIPT-SPEC.md.anchors.jsonl +1 -0
  154. package/docs/dogfood/2026-07-31-anchor.anchors.jsonl +1 -0
  155. package/docs/dogfood/2026-08-06-reconcile.md +140 -0
  156. package/docs/plans/2026-07-02-redteam-harness.md +416 -0
  157. package/docs/specs/2026-07-02-redteam-harness-design.md +90 -0
  158. package/docs/superpowers/TODO-gate1-f4.md +23 -0
  159. package/docs/superpowers/plans/2026-07-29-conarium-receipt-plan.md +200 -0
  160. package/docs/superpowers/plans/2026-07-29-katman0-kapanis-plan.md +231 -0
  161. package/docs/superpowers/plans/2026-07-31-per-user-identity.md +667 -0
  162. package/docs/superpowers/specs/2026-07-29-conarium-receipt-design.md +202 -0
  163. package/docs/superpowers/specs/2026-07-31-per-user-identity-design.md +144 -0
  164. package/docs/superpowers/specs/2026-08-01-coverage-proof-design.md +139 -0
  165. package/docs/superpowers/specs/2026-08-05-receipt-meta-provenance-design.md +116 -0
  166. package/examples/claude-desktop-config.json +9 -0
  167. package/examples/cursor-mcp-settings.json +10 -0
  168. package/package.json +99 -0
  169. package/scripts/audit-chain-check.mjs +119 -0
  170. package/scripts/conarium_sabah_brief.mjs +107 -0
  171. package/scripts/gen-test-vectors.mjs +193 -0
  172. package/scripts/pg-snapshot.sql +27 -0
  173. package/scripts/start-mcp-c1.mjs +70 -0
  174. package/test-vectors/001-single-receipt/receipts.jsonl +1 -0
  175. package/test-vectors/002-chain-of-three/receipts.jsonl +3 -0
  176. package/test-vectors/003-tampered-field/receipts.jsonl +1 -0
  177. package/test-vectors/004-prevhash-break/receipts.jsonl +2 -0
  178. package/test-vectors/005-seq-gap/receipts.jsonl +2 -0
  179. package/test-vectors/006-bad-signature/receipts.jsonl +1 -0
  180. package/test-vectors/007-schema-invalid/receipts.jsonl +1 -0
  181. package/test-vectors/008-unsigned-no-pubkey/receipts.jsonl +1 -0
  182. package/test-vectors/009-unsigned-but-pubkey-given/receipts.jsonl +1 -0
  183. package/test-vectors/README.md +92 -0
  184. package/test-vectors/expected-hashes.json +24 -0
  185. package/test-vectors/keys/vector-key.SECRET-TEST-ONLY.pem +6 -0
  186. package/test-vectors/keys/vector-key.pub.pem +3 -0
  187. package/test-vectors/keys/vector-key.pub.pem.keyid +1 -0
  188. package/test-vectors/manifest.json +95 -0
@@ -0,0 +1,977 @@
1
+ import { parse, toSql } from 'pgsql-ast-parser';
2
+ export class PolicyError extends Error {
3
+ metadata;
4
+ constructor(message, metadata) {
5
+ super(message);
6
+ this.name = 'PolicyError';
7
+ this.metadata = metadata;
8
+ }
9
+ }
10
+ // Glob-ish match: exact, or prefix with trailing '*', or '*' wildcard.
11
+ function match(pattern, value) {
12
+ const p = pattern.trim().toLowerCase();
13
+ const v = value.trim().toLowerCase();
14
+ if (p === '*' || p === '*.*')
15
+ return true;
16
+ if (p.endsWith('.*'))
17
+ return v.startsWith(p.slice(0, -1)); // "billing.*" -> "billing."
18
+ if (p.startsWith('*.'))
19
+ return v.endsWith(p.slice(1)); // "*.email" -> ".email"
20
+ if (p.endsWith('*'))
21
+ return v.startsWith(p.slice(0, -1));
22
+ return p === v;
23
+ }
24
+ /**
25
+ * Free-text carry-over of values the policy already calls PII.
26
+ *
27
+ * The gap this closes: `maskColumns` masks `customer_name`, but the same name
28
+ * written into a `note` column left verbatim, so an operator who had masked
29
+ * every name column still shipped names to the model. There is no name
30
+ * detector here and none is claimed — the only strings treated as names are
31
+ * ones THIS policy already declared, which keeps the rule deterministic and
32
+ * explainable ("your own policy called this value PII; we honoured it
33
+ * everywhere it appears") instead of probabilistic.
34
+ *
35
+ * Deliberate limits, restated in docs so nobody reads more into it:
36
+ * - a name that appears ONLY in free text is not detected here (see the
37
+ * labelled-name pass for the narrow, contextual half of that problem);
38
+ * - values under MIN_KNOWN_VALUE_LENGTH are ignored, because a one or two
39
+ * character value matches everywhere and would shred the output;
40
+ * - longest-first, so masking "Ayse Demir" never leaves a dangling "Demir".
41
+ */
42
+ const MIN_KNOWN_VALUE_LENGTH = 3;
43
+ function escapeRegExp(value) {
44
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
45
+ }
46
+ function knownValueMatchers(values) {
47
+ const unique = new Set();
48
+ for (const value of values) {
49
+ const trimmed = value.trim();
50
+ if (trimmed.length >= MIN_KNOWN_VALUE_LENGTH)
51
+ unique.add(trimmed);
52
+ }
53
+ return [...unique]
54
+ .sort((a, b) => b.length - a.length)
55
+ // Unicode-aware boundaries: \b is wrong for Turkish (it treats "ş" as a
56
+ // boundary), so "Ali" must not match inside "Kalite" but must match at
57
+ // "Ali," or "(Ali)".
58
+ .map(v => new RegExp(`(?<![\\p{L}\\p{N}])${escapeRegExp(v)}(?![\\p{L}\\p{N}])`, 'giu'));
59
+ }
60
+ function redactKnownValues(text, matchers) {
61
+ let out = text;
62
+ let count = 0;
63
+ for (const matcher of matchers) {
64
+ out = out.replace(matcher, () => {
65
+ count++;
66
+ return '[MASKED_PII]';
67
+ });
68
+ }
69
+ return { text: out, count };
70
+ }
71
+ /**
72
+ * A capitalised run of one to three words — the shape of a written name once
73
+ * something else has already told us a name is coming.
74
+ */
75
+ const NAME_SHAPE = String.raw `\p{Lu}\p{L}+(?:\s+\p{Lu}\p{L}+){0,2}`;
76
+ /** "Sn. Ahmet Yılmaz", "Dr. Ayşe Demir" — the title marks it, not a dictionary. */
77
+ const TITLED_NAME_RE = new RegExp(String.raw `(?<![\p{L}\p{N}])(Sn\.|Sayın|Sayin|Bayan|Bay|Mrs\.|Mr\.|Ms\.|Dr\.|Av\.|Prof\.)\s+(${NAME_SHAPE})`, 'gu');
78
+ /**
79
+ * "Yetkili: Ayşe Demir", "customer: John Smith" — the field label marks it.
80
+ * The leading boundary is what keeps `filename: Rapor` out: the `name` inside
81
+ * `filename` is preceded by a letter, so it never starts a match.
82
+ */
83
+ const LABELLED_NAME_RE = new RegExp(String.raw `(?<![\p{L}\p{N}])(müşteri|musteri|yetkili|kişi|kisi|temsilci|adı soyadı|adi soyadi|ad soyad|customer|contact|full name|name)\s*:\s*(${NAME_SHAPE})`, 'giu');
84
+ const WRITE_TOKENS = [
85
+ 'DROP ', 'TRUNCATE ', 'DELETE ', 'UPDATE ', 'INSERT ', 'ALTER ', 'CREATE ',
86
+ 'GRANT ', 'REVOKE ', 'MERGE ', 'COPY ', 'CALL ', 'DO ', 'VACUUM ',
87
+ ];
88
+ const SAFE_BUILTIN_FUNCTIONS = new Set([
89
+ 'abs',
90
+ 'avg',
91
+ 'btrim',
92
+ 'ceil',
93
+ 'ceiling',
94
+ 'char_length',
95
+ 'coalesce',
96
+ 'concat',
97
+ 'concat_ws',
98
+ 'convert_to',
99
+ 'count',
100
+ 'encode',
101
+ 'floor',
102
+ 'greatest',
103
+ 'least',
104
+ 'length',
105
+ 'lower',
106
+ 'ltrim',
107
+ 'max',
108
+ 'min',
109
+ 'nullif',
110
+ 'octet_length',
111
+ 'regexp_replace',
112
+ 'regexp_split_to_array',
113
+ 'replace',
114
+ 'round',
115
+ 'rtrim',
116
+ 'split_part',
117
+ 'sum',
118
+ 'trim',
119
+ 'upper',
120
+ ]);
121
+ const BLOCKED_DUMP_FUNCTIONS = new Set([
122
+ 'array_agg',
123
+ 'json_agg',
124
+ 'jsonb_agg',
125
+ 'row_to_json',
126
+ 'string_agg',
127
+ ]);
128
+ export class Governance {
129
+ policy;
130
+ profileName;
131
+ constructor(policy = {}, profileName = null) {
132
+ this.policy = policy;
133
+ this.profileName = profileName;
134
+ }
135
+ /** Name of the masking profile in force, or null for the base policy. */
136
+ appliedProfile() {
137
+ return this.profileName;
138
+ }
139
+ /**
140
+ * Resolve the policy for a specific person.
141
+ *
142
+ * Masking that fits an AI agent does not fit the data controller: the owner
143
+ * asking "which customer owes the most" needs the name. Rather than a global
144
+ * switch that would turn the guarantee off for everyone, a named profile
145
+ * overlays `maskColumns`/`maxRows` for one identified person, and the receipt
146
+ * records which profile was in force (`policy.id`).
147
+ *
148
+ * Fail-closed at every step — anything unexpected falls back to the BASE
149
+ * policy, never to a wider one:
150
+ * - no actor, or actor authenticated with a SHARED token → base. A shared
151
+ * token means "whoever holds this string", and handing unmasked PII to a
152
+ * bearer is the failure mode the product exists to prevent.
153
+ * - actor not listed in `actorProfiles` → base
154
+ * - profile name not found in `profiles` → base
155
+ */
156
+ forActor(actor) {
157
+ if (!actor)
158
+ return this;
159
+ if (actor.assurance !== 'per-user-token')
160
+ return this;
161
+ const wanted = this.policy.actorProfiles?.[actor.id];
162
+ if (!wanted)
163
+ return this;
164
+ const profile = this.policy.profiles?.[wanted];
165
+ if (!profile) {
166
+ console.error(`[conarium:policy] actor "${actor.id}" is mapped to profile "${wanted}", which is not defined in policy.profiles — falling back to the base policy`);
167
+ return this;
168
+ }
169
+ // Only these three fields. Table/tool/connector permissions are NOT overlayable,
170
+ // so a profile cannot widen reachability — only legibility within it.
171
+ // `maskLabelledNames` belongs to that same class: it changes how much of a
172
+ // reachable row one identified person can read, never which rows exist.
173
+ const merged = {
174
+ ...this.policy,
175
+ ...(profile.maskColumns !== undefined ? { maskColumns: profile.maskColumns } : {}),
176
+ ...(profile.maxRows !== undefined ? { maxRows: profile.maxRows } : {}),
177
+ ...(profile.maskLabelledNames !== undefined ? { maskLabelledNames: profile.maskLabelledNames } : {}),
178
+ };
179
+ return new Governance(merged, wanted);
180
+ }
181
+ allowsTable(qualified) {
182
+ const { allowTables, denyTables } = this.policy;
183
+ if (!this.isSchemaQualifiedTable(qualified))
184
+ return false;
185
+ if (denyTables?.some(p => match(p, qualified)))
186
+ return false;
187
+ // 🔒 DEFAULT-DENY (Codex denetimi 2026-07-06, P1): allowTables tanımlı DEĞİLSE hiçbir
188
+ // tabloya izin verme. docs.html "Nothing is allowed unless you allow it" + README
189
+ // "denied by default" bunu vaat ediyordu; eski kod (return true) TAM TERSİYDİ = güvenlik
190
+ // ürününde vaad ihlali. Açık mod isteyen (playground/demo) explicit allowTables:['*'] verir.
191
+ if (allowTables && allowTables.length > 0) {
192
+ return allowTables.some(p => match(p, qualified));
193
+ }
194
+ return false;
195
+ }
196
+ filterTables(tables) {
197
+ return tables.filter(t => this.allowsTable(`${t.schema}.${t.name}`));
198
+ }
199
+ /**
200
+ * Araç izni — `allowTools` / `denyTools`.
201
+ *
202
+ * 🔴 2026-08-12: bu alanlar KONSOLDA düzenlenebiliyor, `types.ts`'te belgeli ve
203
+ * testi vardı, ama kontrol yalnızca hiçbir yerden çağrılmayan `ApiGovernance`
204
+ * içinde duruyordu — yani operatör "şu aracı kapattım" der, hiçbir şey kapanmazdı.
205
+ * Delik değildi (yazma işlemleri connector'da zaten reddediliyor, okuma
206
+ * `allowsTable`'dan geçiyor), ama YANLIŞ BEYANDI: arayüz bir söz veriyor, motor
207
+ * tutmuyordu. Bu ürünün tek iddiası "söylediğini gerçekten yapmak" olduğu için
208
+ * ölü bir politika alanı, çalışan bir açıktan daha pahalıdır.
209
+ *
210
+ * ⚠️ Bilerek fail-OPEN — `allowsTable`/`allowsConnector`'ın aksine. `allowTools`
211
+ * tanımsızsa her araç geçer, çünkü asıl kapı zaten tablo politikası ve o
212
+ * fail-closed. Burayı da fail-closed yapmak, `allowTools` yazmamış her mevcut
213
+ * kurulumu sessizce kırardı; bu, kapatmaya çalıştığımız hatanın aynısı olurdu.
214
+ * Bu alan koruma katmanı değil, DARALTMA katmanıdır.
215
+ */
216
+ allowsTool(toolName) {
217
+ const { allowTools, denyTools } = this.policy;
218
+ if (denyTools?.some(p => match(p, toolName)))
219
+ return false;
220
+ if (allowTools && allowTools.length > 0)
221
+ return allowTools.some(p => match(p, toolName));
222
+ return true;
223
+ }
224
+ /**
225
+ * Fail-closed, like `allowsTable`. Previously an unset `allowConnectors`
226
+ * meant "every connector is reachable", while an unset `allowTables` meant
227
+ * "no table is readable" — two opposite defaults in the same policy object.
228
+ * A gateway whose whole claim is fail-closed cannot ship that asymmetry.
229
+ *
230
+ * BREAKING: a config with connectors but no `allowConnectors` now reaches
231
+ * nothing. That is deliberate — see `bootDeps`, which refuses to start with
232
+ * a named error rather than silently serving zero connectors.
233
+ */
234
+ allowsConnector(connectorName) {
235
+ const { allowConnectors, denyConnectors } = this.policy;
236
+ if (denyConnectors?.some(p => match(p, connectorName)))
237
+ return false;
238
+ if (!allowConnectors || allowConnectors.length === 0)
239
+ return false;
240
+ return allowConnectors.some(p => match(p, connectorName));
241
+ }
242
+ guardQuery(sql) {
243
+ const emptyState = this.createAnalysisState();
244
+ const norm = ` ${sql.trim().toUpperCase().replace(/\s+/g, ' ')} `;
245
+ const head = norm.trimStart();
246
+ if (!head.startsWith('SELECT') && !head.startsWith('WITH')) {
247
+ this.deny(emptyState, 'Only read-only SELECT/WITH queries are permitted.');
248
+ }
249
+ for (const tok of WRITE_TOKENS) {
250
+ const regex = new RegExp(`\\b${tok.trim()}\\b`);
251
+ if (regex.test(norm))
252
+ this.deny(emptyState, `Blocked write operation: ${tok.trim()}`);
253
+ }
254
+ // Row-locking clauses acquire locks (a side effect) — refuse them on governed reads.
255
+ if (/\bFOR\s+(?:UPDATE|SHARE|NO\s+KEY\s+UPDATE|KEY\s+SHARE)\b/.test(norm)) {
256
+ this.deny(emptyState, 'Row-locking clauses (FOR UPDATE/SHARE) are not permitted.');
257
+ }
258
+ let ast;
259
+ try {
260
+ ast = parse(sql);
261
+ }
262
+ catch (err) {
263
+ this.deny(emptyState, `Failed to parse SQL: ${err.message}`);
264
+ }
265
+ if (ast.length > 1) {
266
+ this.deny(emptyState, 'Multiple statements are not permitted.');
267
+ }
268
+ const state = this.createAnalysisState();
269
+ const output = this.analyzeRead(ast[0], state, new Set());
270
+ this.applyRowCap(ast[0]);
271
+ let rewrittenSql = toSql.statement(ast[0]);
272
+ // KÜME İŞLEMLERİ (UNION / UNION ALL / INTERSECT / EXCEPT) — satır sınırı boşluğu.
273
+ // applyRowCap yalnızca 'select' ve 'with' düğümlerine LIMIT ekleyebiliyor;
274
+ // küme işlemleri limitTarget'tan undefined dönüp SESSİZCE sınırsız geçiyordu.
275
+ // Bu, ürünün "row caps stop bulk extraction" vaadini deliyordu: politika ve
276
+ // maskeleme çalışıyor, ama satır sayısı sınırsız.
277
+ //
278
+ // AST'de küme düğümü limit taşıyamıyor (yalnız type/left/right), üstelik parser
279
+ // kullanıcının yazdığı `A UNION ALL B LIMIT 50`'yi sağ kola koyup anlamı
280
+ // değiştiriyor. O yüzden sınır DIŞARIDAN sarmalanarak uygulanıyor — iç SQL
281
+ // zaten doğrulanmış ve yeniden yazılmış AST'den geliyor.
282
+ if (!this.limitTarget(ast[0])) {
283
+ rewrittenSql = `SELECT * FROM (${rewrittenSql}) AS conarium_capped LIMIT ${this.maxRows()}`;
284
+ }
285
+ const metadata = this.metadataFrom(state, {
286
+ rewrittenSql,
287
+ appliedRowCap: this.maxRows(),
288
+ maskedFields: output.maskedOutputs,
289
+ });
290
+ return { sql: rewrittenSql, aliases: output.aliases, metadata };
291
+ }
292
+ maxRows() {
293
+ return this.policy.maxRows ?? 100;
294
+ }
295
+ redact(result, aliases = {}, metadata) {
296
+ const maskedFieldLookup = new Set((metadata?.maskedFields ?? []).map(f => f.toLowerCase()));
297
+ const maskedFields = new Set(metadata?.maskedFields ?? []);
298
+ let maskedCount = 0;
299
+ // PASS 1 — collect the raw values this policy has already declared to be PII.
300
+ // See `knownValueMatchers`: the same name that gets masked in `customer_name`
301
+ // was leaving verbatim inside a free-text `note` on the very same row.
302
+ const declared = [];
303
+ for (const row of result.rows) {
304
+ for (const key of Object.keys(row)) {
305
+ if (!this.masksColumn(row, key, aliases, maskedFieldLookup))
306
+ continue;
307
+ const value = row[key];
308
+ if (typeof value === 'string')
309
+ declared.push(value);
310
+ }
311
+ }
312
+ const matchers = knownValueMatchers(declared);
313
+ // PASS 2 — mask.
314
+ const rows = result.rows.map(row => {
315
+ const out = { ...row };
316
+ for (const key of Object.keys(out)) {
317
+ if (this.masksColumn(out, key, aliases, maskedFieldLookup)) {
318
+ out[key] = '[MASKED_PII]';
319
+ maskedFields.add(key);
320
+ maskedCount++;
321
+ continue;
322
+ }
323
+ const scanRes = this.maskPII(out[key]);
324
+ out[key] = scanRes.masked;
325
+ if (scanRes.count > 0) {
326
+ maskedFields.add(key);
327
+ maskedCount += scanRes.count;
328
+ }
329
+ if (typeof out[key] === 'string' && matchers.length > 0) {
330
+ const carried = redactKnownValues(out[key], matchers);
331
+ if (carried.count > 0) {
332
+ out[key] = carried.text;
333
+ maskedFields.add(key);
334
+ maskedCount += carried.count;
335
+ }
336
+ }
337
+ }
338
+ return out;
339
+ });
340
+ const governance = this.metadataFromMetadata(metadata, {
341
+ maskedFields,
342
+ maskedCount,
343
+ truncated: result.rowCount > this.maxRows(),
344
+ });
345
+ return { ...result, rows, governance };
346
+ }
347
+ /** True when `key` of `row` is masked by policy (or by upstream query analysis). */
348
+ masksColumn(row, key, aliases, maskedFieldLookup) {
349
+ const keyLower = key.toLowerCase();
350
+ if (maskedFieldLookup.has(keyLower))
351
+ return true;
352
+ const masks = this.policy.maskColumns ?? [];
353
+ const table = typeof row._table === 'string' ? row._table : '';
354
+ const sourceKey = aliases?.[keyLower] || key;
355
+ const qualifiedSource = table ? `${table}.${sourceKey}` : sourceKey;
356
+ // Also mask by bare COLUMN NAME (last path segment of any mask rule). This
357
+ // closes the SELECT * / star-projection leak: when the executable rows carry
358
+ // no _table qualifier, a fully-qualified rule like public.customers.address
359
+ // would otherwise never match. For a PII tool, over-masking a configured
360
+ // column name across governed output is the safe direction.
361
+ return masks.some(m => match(m, qualifiedSource) || match(m, sourceKey) || match(m, key) ||
362
+ m.slice(m.lastIndexOf('.') + 1).toLowerCase() === keyLower);
363
+ }
364
+ maskPII(obj) {
365
+ if (!obj)
366
+ return { masked: obj, count: 0 };
367
+ // SAYISAL PII (NEO guvenlik taramasi, 2026-07-31 — deneysel dogrulandi).
368
+ // Maskeleme yalnizca string ve object dallarinda calisiyordu; number/bigint
369
+ // hicbir kontrolden gecmeden HAM donuyordu. Olculen sonuc:
370
+ // tckn_metin "12345678901" -> [MASKED_PII] · tckn_sayi 12345678901 -> SIZDI
371
+ // Turkiye'de TCKN ve telefonun bigint tutulmasi yaygin; bu teorik degil.
372
+ //
373
+ // Sayi metne cevrilip AYNI desenlerden geciriliyor — ayri bir "hangi sayi
374
+ // PII'dir" kural seti tutmak ikinci bir kaynak yaratir ve biri bayatlar.
375
+ // Desen tutmazsa sayi TIPI KORUNARAK aynen doner: id/adet/fiyat/yil bozulmaz.
376
+ // Bilinerek kabul edilen yanlis pozitif: 10, 11 veya 13-16 haneli bir siparis
377
+ // numarasi da maskelenir. Maskeleme urununde guvenli yon budur.
378
+ if (typeof obj === 'number' || typeof obj === 'bigint') {
379
+ const sonuc = this.maskPII(String(obj));
380
+ return sonuc.count > 0 ? sonuc : { masked: obj, count: 0 };
381
+ }
382
+ if (typeof obj === 'string') {
383
+ let count = 0;
384
+ let masked = obj;
385
+ const emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g;
386
+ const tcknRegex = /\b[1-9][0-9]{10}\b/g;
387
+ const phoneRegex = /(?:\+?\d{1,3}[\s-]?)?\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}\b/g;
388
+ const cardRegex = /\b(?:\d[ -]*?){13,16}\b/g;
389
+ masked = masked.replace(emailRegex, () => { count++; return '[MASKED_PII]'; });
390
+ masked = masked.replace(tcknRegex, () => { count++; return '[MASKED_PII]'; });
391
+ masked = masked.replace(phoneRegex, () => { count++; return '[MASKED_PII]'; });
392
+ masked = masked.replace(cardRegex, () => { count++; return '[MASKED_PII]'; });
393
+ // Sertleştirme (Codex denetimi 2026-07-06, P1): README "secrets are redacted in the
394
+ // response stream" diyor ama yanıt yolu (maskPII) sadece PII yakalıyordu — API key /
395
+ // token / şifre / bağlantı-dizesi kimliği MODELE ham gidiyordu (ör. api_key sütunu
396
+ // maskColumns'ta değilse). Audit yolu (audit.ts maskArgs) bunu zaten yakalıyordu;
397
+ // aynı dedektörleri yanıt yoluna da taşıdık. Ürünün güvenlik vaadi = bu.
398
+ // sk- ailesi: yeni OpenAI anahtarları sk-proj-... (tire içerir) → tire/alt-çizgiye izin ver.
399
+ const secretRe = /\b(?:sk-[A-Za-z0-9_-]{12,}|sk_live_[A-Za-z0-9]{6,}|sk_test_[A-Za-z0-9]{6,}|AKIA[0-9A-Z]{16}|ghp_[A-Za-z0-9]{20,}|gsk_[A-Za-z0-9]{20,}|xox[baprs]-[A-Za-z0-9-]{8,}|eyJ[A-Za-z0-9._-]{20,})\b/g;
400
+ masked = masked.replace(secretRe, () => { count++; return '[MASKED_SECRET]'; });
401
+ masked = masked.replace(/([a-zA-Z][a-zA-Z0-9+.-]*:\/\/[^:@\s/"']+:)[^@\s/"']+(@)/g, (_m, p1, p2) => { count++; return `${p1}[MASKED_SECRET]${p2}`; });
402
+ masked = masked.replace(/(Bearer\s+)[A-Za-z0-9._~+/=-]{6,}/gi, (_m, p1) => { count++; return `${p1}[MASKED_SECRET]`; });
403
+ masked = masked.replace(/((?:password|passwd|pwd|secret|token|api[_-]?key|access[_-]?key|authorization)["'\s]*[:=]["'\s]*)[^"'\s,;}]{4,}/gi, (_m, p1) => { count++; return `${p1}[MASKED_SECRET]`; });
404
+ // LABELLED NAMES. A person's name is the one identifier this gateway could
405
+ // not see: emails, national ids, phones and cards have shape, names do not.
406
+ // Column policy caught `customer_name`; a name written into a free-text
407
+ // note went to the model verbatim.
408
+ //
409
+ // What this does NOT do, deliberately: detect a bare name in running prose
410
+ // ("Ahmet called yesterday"). That needs NER — a model, a dictionary and a
411
+ // confidence score — and this gateway's whole claim is that its decisions
412
+ // are deterministic and reproducible from the rule alone. Only names the
413
+ // TEXT ITSELF marks as names, via a title or a field label, are masked.
414
+ // The residual gap is documented rather than papered over.
415
+ if (this.policy.maskLabelledNames !== false) {
416
+ masked = masked.replace(TITLED_NAME_RE, (_m, title) => {
417
+ count++;
418
+ return `${title} [MASKED_PII]`;
419
+ });
420
+ masked = masked.replace(LABELLED_NAME_RE, (_m, label) => {
421
+ count++;
422
+ return `${label}: [MASKED_PII]`;
423
+ });
424
+ }
425
+ // Sertleştirme (Claude, 2026-07-02): encode(...,'base64') ile kaçırılan PII'yi de yakala.
426
+ // NEO red-team harness bu deliği buldu (pii-base64 BYPASS). Saf base64 bir metin
427
+ // çözülünce e-posta/TCKN içeriyorsa maskele.
428
+ const b64candidate = masked.trim();
429
+ if (/^[A-Za-z0-9+/]{12,}={0,2}$/.test(b64candidate)) {
430
+ try {
431
+ const decoded = Buffer.from(b64candidate, 'base64').toString('utf8');
432
+ if (/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/.test(decoded) || /\b[1-9][0-9]{10}\b/.test(decoded)) {
433
+ count++;
434
+ masked = '[MASKED_PII]';
435
+ }
436
+ }
437
+ catch { /* base64 değil */ }
438
+ }
439
+ return { masked, count };
440
+ }
441
+ if (Array.isArray(obj)) {
442
+ let totalCount = 0;
443
+ const maskedArray = obj.map(item => {
444
+ const res = this.maskPII(item);
445
+ totalCount += res.count;
446
+ return res.masked;
447
+ });
448
+ return { masked: maskedArray, count: totalCount };
449
+ }
450
+ if (typeof obj === 'object') {
451
+ let totalCount = 0;
452
+ const out = {};
453
+ for (const [k, v] of Object.entries(obj)) {
454
+ if (typeof v === 'string') {
455
+ const lowerKey = k.toLowerCase();
456
+ if (lowerKey.includes('email') || lowerKey.includes('phone') || lowerKey.includes('tckn') || lowerKey.includes('card')) {
457
+ out[k] = '[MASKED_PII]';
458
+ totalCount++;
459
+ }
460
+ else if (/secret|token|password|passwd|pwd|api[_-]?key|access[_-]?key|\bkey\b|credential/.test(lowerKey)) {
461
+ // Sütun ADI sır ima ediyorsa (api_key, secret, token, password) değeri her hâlükârda maskele.
462
+ out[k] = '[MASKED_SECRET]';
463
+ totalCount++;
464
+ }
465
+ else {
466
+ const res = this.maskPII(v);
467
+ out[k] = res.masked;
468
+ totalCount += res.count;
469
+ }
470
+ }
471
+ else {
472
+ const res = this.maskPII(v);
473
+ out[k] = res.masked;
474
+ totalCount += res.count;
475
+ }
476
+ }
477
+ return { masked: out, count: totalCount };
478
+ }
479
+ return { masked: obj, count: 0 };
480
+ }
481
+ createAnalysisState() {
482
+ return {
483
+ accessedTables: new Set(),
484
+ accessedFunctions: new Set(),
485
+ };
486
+ }
487
+ metadataFrom(state, opts = {}) {
488
+ return {
489
+ accessedTables: [...state.accessedTables].sort(),
490
+ accessedFunctions: [...state.accessedFunctions].sort(),
491
+ rewrittenSql: opts.rewrittenSql,
492
+ appliedRowCap: opts.appliedRowCap,
493
+ maskedFields: [...(opts.maskedFields ?? new Set())].sort(),
494
+ maskedCount: opts.maskedCount ?? 0,
495
+ truncated: opts.truncated,
496
+ denied: opts.denied ?? false,
497
+ denyReason: opts.denyReason,
498
+ };
499
+ }
500
+ metadataFromMetadata(metadata, opts) {
501
+ return {
502
+ accessedTables: metadata?.accessedTables ?? [],
503
+ accessedFunctions: metadata?.accessedFunctions ?? [],
504
+ rewrittenSql: metadata?.rewrittenSql,
505
+ appliedRowCap: metadata?.appliedRowCap ?? this.maxRows(),
506
+ maskedFields: [...opts.maskedFields].sort(),
507
+ maskedCount: opts.maskedCount,
508
+ truncated: opts.truncated,
509
+ denied: metadata?.denied ?? false,
510
+ denyReason: metadata?.denyReason,
511
+ };
512
+ }
513
+ deny(state, reason) {
514
+ throw new PolicyError(reason, this.metadataFrom(state, {
515
+ appliedRowCap: this.maxRows(),
516
+ denied: true,
517
+ denyReason: reason,
518
+ }));
519
+ }
520
+ isSchemaQualifiedTable(table) {
521
+ const parts = table.trim().split('.');
522
+ return parts.length === 2 && parts.every(Boolean);
523
+ }
524
+ normalizeIdentifier(value) {
525
+ return value.trim().toLowerCase();
526
+ }
527
+ qualifiedTable(schema, table) {
528
+ return `${this.normalizeIdentifier(schema)}.${this.normalizeIdentifier(table)}`;
529
+ }
530
+ analyzeRead(statement, state, cteNames, cteOutputs = new Map()) {
531
+ if (statement.type === 'with') {
532
+ const scopedCtes = new Set(cteNames);
533
+ const scopedOutputs = new Map(cteOutputs);
534
+ for (const binding of statement.bind) {
535
+ scopedCtes.add(this.normalizeIdentifier(binding.alias.name));
536
+ }
537
+ for (const binding of statement.bind) {
538
+ const analyzed = this.analyzeRead(binding.statement, state, scopedCtes, scopedOutputs);
539
+ scopedOutputs.set(this.normalizeIdentifier(binding.alias.name), analyzed.maskedOutputs);
540
+ }
541
+ return this.analyzeRead(statement.in, state, scopedCtes, scopedOutputs);
542
+ }
543
+ if (statement.type === 'with recursive') {
544
+ const scopedCtes = new Set(cteNames);
545
+ const cteName = this.normalizeIdentifier(statement.alias.name);
546
+ scopedCtes.add(cteName);
547
+ const scopedOutputs = new Map(cteOutputs);
548
+ const analyzed = this.analyzeRead(statement.bind, state, scopedCtes, scopedOutputs);
549
+ scopedOutputs.set(cteName, analyzed.maskedOutputs);
550
+ return this.analyzeRead(statement.in, state, scopedCtes, scopedOutputs);
551
+ }
552
+ if (statement.type === 'select') {
553
+ return this.analyzeSelect(statement, state, cteNames, cteOutputs);
554
+ }
555
+ if (statement.type === 'union' || statement.type === 'union all') {
556
+ const left = this.analyzeRead(statement.left, state, cteNames, cteOutputs);
557
+ const right = this.analyzeRead(statement.right, state, cteNames, cteOutputs);
558
+ return {
559
+ maskedOutputs: new Set([...left.maskedOutputs, ...right.maskedOutputs]),
560
+ aliases: { ...left.aliases, ...right.aliases },
561
+ };
562
+ }
563
+ if (statement.type === 'values') {
564
+ return { maskedOutputs: new Set(), aliases: {} };
565
+ }
566
+ this.deny(state, 'Only read-only SELECT/WITH queries are permitted.');
567
+ }
568
+ analyzeSelect(statement, state, cteNames, cteOutputs) {
569
+ const scope = { sources: new Map() };
570
+ const maskedOutputs = new Set();
571
+ const aliases = {};
572
+ for (const from of statement.from ?? []) {
573
+ this.collectFrom(from, state, scope, cteNames, cteOutputs);
574
+ }
575
+ statement.where && this.collectExpr(statement.where, state, scope, cteNames, cteOutputs);
576
+ statement.having && this.collectExpr(statement.having, state, scope, cteNames, cteOutputs);
577
+ for (const expr of statement.groupBy ?? [])
578
+ this.collectExpr(expr, state, scope, cteNames, cteOutputs);
579
+ for (const order of statement.orderBy ?? [])
580
+ this.collectOrderBy(order, state, scope, cteNames, cteOutputs);
581
+ const columns = statement.columns ?? [];
582
+ columns.forEach((column, index) => {
583
+ this.collectExpr(column.expr, state, scope, cteNames, cteOutputs);
584
+ if (column.alias && column.expr.type === 'ref' && column.expr.name !== '*') {
585
+ aliases[this.normalizeIdentifier(column.alias.name)] = this.normalizeIdentifier(column.expr.name);
586
+ }
587
+ if (!this.expressionReferencesMasked(column.expr, scope, cteNames, cteOutputs))
588
+ return;
589
+ const output = this.outputNamesForColumn(column, index, scope);
590
+ if (!output.reliable) {
591
+ this.deny(state, 'Query output expression references a masked column but has no reliable output name to redact.');
592
+ }
593
+ for (const name of output.names)
594
+ maskedOutputs.add(name);
595
+ });
596
+ return { maskedOutputs, aliases };
597
+ }
598
+ collectFrom(from, state, scope, cteNames, cteOutputs) {
599
+ if (from.type === 'table') {
600
+ const tableName = this.normalizeIdentifier(from.name.name);
601
+ const alias = from.name.alias ? this.normalizeIdentifier(from.name.alias) : tableName;
602
+ if (!from.name.schema && cteNames.has(tableName)) {
603
+ this.addSource(scope, alias, {
604
+ kind: 'derived',
605
+ maskedColumns: cteOutputs.get(tableName) ?? new Set(),
606
+ });
607
+ if (from.join?.on)
608
+ this.collectExpr(from.join.on, state, scope, cteNames, cteOutputs);
609
+ return;
610
+ }
611
+ if (!from.name.schema) {
612
+ this.deny(state, `Unqualified table '${from.name.name}' is not permitted; use schema.table.`);
613
+ }
614
+ // The parser folds unquoted identifiers to lowercase (PostgreSQL semantics), so an
615
+ // uppercase character here can only come from a quoted identifier — and Postgres
616
+ // treats `public."Customers"` as a DIFFERENT table than public.customers. Folding it
617
+ // for policy matching would let it ride an allow-list entry it doesn't belong to.
618
+ if (from.name.name !== from.name.name.toLowerCase() || from.name.schema !== from.name.schema.toLowerCase()) {
619
+ this.deny(state, `Quoted mixed-case identifier '${from.name.schema}.${from.name.name}' is not permitted; policy matching is lowercase-only.`);
620
+ }
621
+ const qualified = this.qualifiedTable(from.name.schema, from.name.name);
622
+ state.accessedTables.add(qualified);
623
+ this.enforceTableAccess(qualified, state);
624
+ this.addSource(scope, alias, {
625
+ kind: 'table',
626
+ schema: this.normalizeIdentifier(from.name.schema),
627
+ table: tableName,
628
+ });
629
+ if (from.join?.on)
630
+ this.collectExpr(from.join.on, state, scope, cteNames, cteOutputs);
631
+ return;
632
+ }
633
+ if (from.type === 'statement') {
634
+ const analyzed = this.analyzeRead(from.statement, state, cteNames, cteOutputs);
635
+ this.addSource(scope, this.normalizeIdentifier(from.alias), {
636
+ kind: 'derived',
637
+ maskedColumns: analyzed.maskedOutputs,
638
+ });
639
+ if (from.join?.on)
640
+ this.collectExpr(from.join.on, state, scope, cteNames, cteOutputs);
641
+ return;
642
+ }
643
+ if (from.type === 'call') {
644
+ this.inspectFunction(from, state);
645
+ for (const arg of from.args)
646
+ this.collectExpr(arg, state, scope, cteNames, cteOutputs);
647
+ for (const order of from.orderBy ?? [])
648
+ this.collectOrderBy(order, state, scope, cteNames, cteOutputs);
649
+ if (from.filter)
650
+ this.collectExpr(from.filter, state, scope, cteNames, cteOutputs);
651
+ if (from.withinGroup)
652
+ this.collectOrderBy(from.withinGroup, state, scope, cteNames, cteOutputs);
653
+ if (from.over?.orderBy)
654
+ for (const order of from.over.orderBy)
655
+ this.collectOrderBy(order, state, scope, cteNames, cteOutputs);
656
+ if (from.over?.partitionBy)
657
+ for (const expr of from.over.partitionBy)
658
+ this.collectExpr(expr, state, scope, cteNames, cteOutputs);
659
+ if (from.join?.on)
660
+ this.collectExpr(from.join.on, state, scope, cteNames, cteOutputs);
661
+ }
662
+ }
663
+ addSource(scope, alias, source) {
664
+ scope.sources.set(alias, source);
665
+ }
666
+ enforceTableAccess(qualified, state) {
667
+ if (qualified.startsWith('pg_catalog.') || qualified.startsWith('information_schema.') || qualified.startsWith('public.pg_')) {
668
+ this.deny(state, `Access to system catalog '${qualified}' is forbidden.`);
669
+ }
670
+ if (!this.allowsTable(qualified)) {
671
+ this.deny(state, `Access to table '${qualified}' is not permitted by policy.`);
672
+ }
673
+ }
674
+ collectOrderBy(order, state, scope, cteNames, cteOutputs) {
675
+ this.collectExpr(order.by, state, scope, cteNames, cteOutputs);
676
+ }
677
+ collectExpr(expr, state, scope, cteNames, cteOutputs) {
678
+ switch (expr.type) {
679
+ case 'call':
680
+ this.inspectFunction(expr, state);
681
+ for (const arg of expr.args)
682
+ this.collectExpr(arg, state, scope, cteNames, cteOutputs);
683
+ for (const order of expr.orderBy ?? [])
684
+ this.collectOrderBy(order, state, scope, cteNames, cteOutputs);
685
+ if (expr.filter)
686
+ this.collectExpr(expr.filter, state, scope, cteNames, cteOutputs);
687
+ if (expr.withinGroup)
688
+ this.collectOrderBy(expr.withinGroup, state, scope, cteNames, cteOutputs);
689
+ if (expr.over?.orderBy)
690
+ for (const order of expr.over.orderBy)
691
+ this.collectOrderBy(order, state, scope, cteNames, cteOutputs);
692
+ if (expr.over?.partitionBy)
693
+ for (const item of expr.over.partitionBy)
694
+ this.collectExpr(item, state, scope, cteNames, cteOutputs);
695
+ return;
696
+ case 'cast':
697
+ this.collectExpr(expr.operand, state, scope, cteNames, cteOutputs);
698
+ return;
699
+ case 'binary':
700
+ this.collectExpr(expr.left, state, scope, cteNames, cteOutputs);
701
+ this.collectExpr(expr.right, state, scope, cteNames, cteOutputs);
702
+ return;
703
+ case 'unary':
704
+ this.collectExpr(expr.operand, state, scope, cteNames, cteOutputs);
705
+ return;
706
+ case 'ternary':
707
+ this.collectExpr(expr.value, state, scope, cteNames, cteOutputs);
708
+ this.collectExpr(expr.lo, state, scope, cteNames, cteOutputs);
709
+ this.collectExpr(expr.hi, state, scope, cteNames, cteOutputs);
710
+ return;
711
+ case 'case':
712
+ if (expr.value)
713
+ this.collectExpr(expr.value, state, scope, cteNames, cteOutputs);
714
+ for (const when of expr.whens) {
715
+ this.collectExpr(when.when, state, scope, cteNames, cteOutputs);
716
+ this.collectExpr(when.value, state, scope, cteNames, cteOutputs);
717
+ }
718
+ if (expr.else)
719
+ this.collectExpr(expr.else, state, scope, cteNames, cteOutputs);
720
+ return;
721
+ case 'list':
722
+ case 'array':
723
+ for (const item of expr.expressions)
724
+ this.collectExpr(item, state, scope, cteNames, cteOutputs);
725
+ return;
726
+ case 'array select':
727
+ this.analyzeRead(expr.select, state, cteNames, cteOutputs);
728
+ return;
729
+ case 'member':
730
+ this.collectExpr(expr.operand, state, scope, cteNames, cteOutputs);
731
+ return;
732
+ case 'extract':
733
+ this.collectExpr(expr.from, state, scope, cteNames, cteOutputs);
734
+ return;
735
+ case 'overlay':
736
+ this.collectExpr(expr.value, state, scope, cteNames, cteOutputs);
737
+ this.collectExpr(expr.placing, state, scope, cteNames, cteOutputs);
738
+ this.collectExpr(expr.from, state, scope, cteNames, cteOutputs);
739
+ if (expr.for)
740
+ this.collectExpr(expr.for, state, scope, cteNames, cteOutputs);
741
+ return;
742
+ case 'substring':
743
+ this.collectExpr(expr.value, state, scope, cteNames, cteOutputs);
744
+ if (expr.from)
745
+ this.collectExpr(expr.from, state, scope, cteNames, cteOutputs);
746
+ if (expr.for)
747
+ this.collectExpr(expr.for, state, scope, cteNames, cteOutputs);
748
+ return;
749
+ case 'arrayIndex':
750
+ this.collectExpr(expr.array, state, scope, cteNames, cteOutputs);
751
+ this.collectExpr(expr.index, state, scope, cteNames, cteOutputs);
752
+ return;
753
+ case 'select':
754
+ case 'with':
755
+ case 'with recursive':
756
+ case 'union':
757
+ case 'union all':
758
+ case 'values':
759
+ this.analyzeRead(expr, state, cteNames, cteOutputs);
760
+ return;
761
+ default:
762
+ return;
763
+ }
764
+ }
765
+ inspectFunction(call, state) {
766
+ const fn = this.functionId(call.function);
767
+ state.accessedFunctions.add(fn);
768
+ const baseName = this.normalizeIdentifier(call.function.name);
769
+ if (BLOCKED_DUMP_FUNCTIONS.has(baseName)) {
770
+ this.deny(state, `High-risk aggregate/serialization function '${fn}' is not permitted by policy.`);
771
+ }
772
+ const schema = call.function.schema ? this.normalizeIdentifier(call.function.schema) : undefined;
773
+ const isSafeBuiltin = SAFE_BUILTIN_FUNCTIONS.has(baseName) && (!schema || schema === 'pg_catalog');
774
+ if (!isSafeBuiltin) {
775
+ this.deny(state, `Function '${fn}' is not permitted by policy.`);
776
+ }
777
+ }
778
+ functionId(fn) {
779
+ return fn.schema ? `${this.normalizeIdentifier(fn.schema)}.${this.normalizeIdentifier(fn.name)}` : this.normalizeIdentifier(fn.name);
780
+ }
781
+ expressionReferencesMasked(expr, scope, cteNames, cteOutputs) {
782
+ switch (expr.type) {
783
+ case 'ref':
784
+ return this.refReferencesMasked(expr, scope);
785
+ case 'call':
786
+ return expr.args.some(arg => this.expressionReferencesMasked(arg, scope, cteNames, cteOutputs))
787
+ || (expr.filter ? this.expressionReferencesMasked(expr.filter, scope, cteNames, cteOutputs) : false)
788
+ || (expr.orderBy ?? []).some(order => this.expressionReferencesMasked(order.by, scope, cteNames, cteOutputs))
789
+ || (expr.withinGroup ? this.expressionReferencesMasked(expr.withinGroup.by, scope, cteNames, cteOutputs) : false)
790
+ || (expr.over?.orderBy ?? []).some(order => this.expressionReferencesMasked(order.by, scope, cteNames, cteOutputs))
791
+ || (expr.over?.partitionBy ?? []).some(item => this.expressionReferencesMasked(item, scope, cteNames, cteOutputs));
792
+ case 'cast':
793
+ return this.expressionReferencesMasked(expr.operand, scope, cteNames, cteOutputs);
794
+ case 'binary':
795
+ return this.expressionReferencesMasked(expr.left, scope, cteNames, cteOutputs) || this.expressionReferencesMasked(expr.right, scope, cteNames, cteOutputs);
796
+ case 'unary':
797
+ return this.expressionReferencesMasked(expr.operand, scope, cteNames, cteOutputs);
798
+ case 'ternary':
799
+ return this.expressionReferencesMasked(expr.value, scope, cteNames, cteOutputs)
800
+ || this.expressionReferencesMasked(expr.lo, scope, cteNames, cteOutputs)
801
+ || this.expressionReferencesMasked(expr.hi, scope, cteNames, cteOutputs);
802
+ case 'case':
803
+ return (expr.value ? this.expressionReferencesMasked(expr.value, scope, cteNames, cteOutputs) : false)
804
+ || expr.whens.some(when => this.expressionReferencesMasked(when.when, scope, cteNames, cteOutputs) || this.expressionReferencesMasked(when.value, scope, cteNames, cteOutputs))
805
+ || (expr.else ? this.expressionReferencesMasked(expr.else, scope, cteNames, cteOutputs) : false);
806
+ case 'list':
807
+ case 'array':
808
+ return expr.expressions.some(item => this.expressionReferencesMasked(item, scope, cteNames, cteOutputs));
809
+ case 'array select':
810
+ return this.analyzeRead(expr.select, this.createAnalysisState(), cteNames, cteOutputs).maskedOutputs.size > 0;
811
+ case 'member':
812
+ return this.expressionReferencesMasked(expr.operand, scope, cteNames, cteOutputs);
813
+ case 'extract':
814
+ return this.expressionReferencesMasked(expr.from, scope, cteNames, cteOutputs);
815
+ case 'overlay':
816
+ return this.expressionReferencesMasked(expr.value, scope, cteNames, cteOutputs)
817
+ || this.expressionReferencesMasked(expr.placing, scope, cteNames, cteOutputs)
818
+ || this.expressionReferencesMasked(expr.from, scope, cteNames, cteOutputs)
819
+ || (expr.for ? this.expressionReferencesMasked(expr.for, scope, cteNames, cteOutputs) : false);
820
+ case 'substring':
821
+ return this.expressionReferencesMasked(expr.value, scope, cteNames, cteOutputs)
822
+ || (expr.from ? this.expressionReferencesMasked(expr.from, scope, cteNames, cteOutputs) : false)
823
+ || (expr.for ? this.expressionReferencesMasked(expr.for, scope, cteNames, cteOutputs) : false);
824
+ case 'arrayIndex':
825
+ return this.expressionReferencesMasked(expr.array, scope, cteNames, cteOutputs) || this.expressionReferencesMasked(expr.index, scope, cteNames, cteOutputs);
826
+ case 'select':
827
+ case 'with':
828
+ case 'with recursive':
829
+ case 'union':
830
+ case 'union all':
831
+ case 'values':
832
+ return this.analyzeRead(expr, this.createAnalysisState(), cteNames, cteOutputs).maskedOutputs.size > 0;
833
+ default:
834
+ return false;
835
+ }
836
+ }
837
+ refReferencesMasked(ref, scope) {
838
+ const column = this.normalizeIdentifier(ref.name);
839
+ if (column === '*') {
840
+ return [...scope.sources.values()].some(source => (source.maskedColumns?.size ?? 0) > 0);
841
+ }
842
+ if (ref.table?.schema) {
843
+ return this.matchesMaskedColumn(this.normalizeIdentifier(ref.table.schema), this.normalizeIdentifier(ref.table.name), column);
844
+ }
845
+ if (ref.table?.name) {
846
+ const source = scope.sources.get(this.normalizeIdentifier(ref.table.name));
847
+ if (source?.kind === 'derived')
848
+ return source.maskedColumns?.has(column) ?? false;
849
+ if (source?.kind === 'table' && source.schema && source.table) {
850
+ return this.matchesMaskedColumn(source.schema, source.table, column);
851
+ }
852
+ return this.matchesMaskedColumn(undefined, this.normalizeIdentifier(ref.table.name), column);
853
+ }
854
+ for (const source of scope.sources.values()) {
855
+ if (source.kind === 'derived' && source.maskedColumns?.has(column))
856
+ return true;
857
+ if (source.kind === 'table' && source.schema && source.table && this.matchesMaskedColumn(source.schema, source.table, column))
858
+ return true;
859
+ }
860
+ return this.matchesMaskedColumn(undefined, undefined, column);
861
+ }
862
+ matchesMaskedColumn(schema, table, column) {
863
+ const masks = this.policy.maskColumns ?? [];
864
+ const candidates = new Set([column]);
865
+ if (table)
866
+ candidates.add(`${table}.${column}`);
867
+ if (schema && table)
868
+ candidates.add(`${schema}.${table}.${column}`);
869
+ if (schema)
870
+ candidates.add(`${schema}.${column}`);
871
+ return masks.some(mask => [...candidates].some(candidate => match(mask, candidate)));
872
+ }
873
+ outputNamesForColumn(column, index, scope) {
874
+ if (column.alias)
875
+ return { names: [column.alias.name], reliable: true };
876
+ const expr = column.expr;
877
+ if (expr.type === 'ref') {
878
+ if (expr.name === '*') {
879
+ const names = [...scope.sources.values()].flatMap(source => [...(source.maskedColumns ?? new Set())]);
880
+ return { names, reliable: names.length > 0 };
881
+ }
882
+ return { names: [expr.name], reliable: true };
883
+ }
884
+ if (expr.type === 'cast' && expr.operand.type === 'ref' && expr.operand.name !== '*') {
885
+ return { names: [expr.operand.name], reliable: true };
886
+ }
887
+ if (expr.type === 'call') {
888
+ return { names: [expr.function.name], reliable: false };
889
+ }
890
+ return { names: [`column_${index + 1}`], reliable: false };
891
+ }
892
+ applyRowCap(statement) {
893
+ const target = this.limitTarget(statement);
894
+ if (!target)
895
+ return;
896
+ const cap = this.maxRows();
897
+ const existing = target.limit?.limit;
898
+ // Never RAISE a limit the caller already set below the cap — a request for
899
+ // 1 row must not become 50. Only clamp down; preserve any OFFSET.
900
+ if (existing && existing.type === 'integer' && typeof existing.value === 'number' && existing.value <= cap) {
901
+ return;
902
+ }
903
+ target.limit = {
904
+ ...(target.limit ?? {}),
905
+ limit: { type: 'integer', value: cap },
906
+ };
907
+ }
908
+ limitTarget(statement) {
909
+ if (statement.type === 'select')
910
+ return statement;
911
+ if (statement.type === 'with' || statement.type === 'with recursive')
912
+ return this.limitTarget(statement.in);
913
+ return undefined;
914
+ }
915
+ }
916
+ export class ApiGovernance {
917
+ policy;
918
+ constructor(policy = {}) {
919
+ this.policy = policy;
920
+ }
921
+ allowsTool(toolName) {
922
+ const { allowTools, denyTools } = this.policy;
923
+ if (denyTools?.some(p => match(p, toolName)))
924
+ return false;
925
+ if (allowTools && allowTools.length > 0)
926
+ return allowTools.some(p => match(p, toolName));
927
+ return true;
928
+ }
929
+ redactResponse(data) {
930
+ if (!data)
931
+ return data;
932
+ const max = this.policy.maxRows ?? 100;
933
+ if (Array.isArray(data)) {
934
+ data = data.slice(0, max).map(item => this.maskPii(item));
935
+ }
936
+ else {
937
+ data = this.maskPii(data);
938
+ }
939
+ return data;
940
+ }
941
+ maskPii(obj) {
942
+ if (!obj)
943
+ return obj;
944
+ if (typeof obj === 'string')
945
+ return this.applyRegexMasks(obj);
946
+ if (Array.isArray(obj))
947
+ return obj.map(item => this.maskPii(item));
948
+ if (typeof obj === 'object') {
949
+ const out = {};
950
+ for (const [k, v] of Object.entries(obj)) {
951
+ if (typeof v === 'string') {
952
+ const lowerKey = k.toLowerCase();
953
+ if (lowerKey.includes('email') || lowerKey.includes('phone') || lowerKey.includes('tckn') || lowerKey.includes('card')) {
954
+ out[k] = '[MASKED_PII]';
955
+ }
956
+ else {
957
+ out[k] = this.applyRegexMasks(v);
958
+ }
959
+ }
960
+ else {
961
+ out[k] = this.maskPii(v);
962
+ }
963
+ }
964
+ return out;
965
+ }
966
+ return obj;
967
+ }
968
+ applyRegexMasks(text) {
969
+ let masked = text;
970
+ masked = masked.replace(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g, '[MASKED_PII]');
971
+ masked = masked.replace(/\b[1-9][0-9]{10}\b/g, '[MASKED_PII]');
972
+ masked = masked.replace(/(?:\+?\d{1,3}[\s-]?)?\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}\b/g, '[MASKED_PII]');
973
+ masked = masked.replace(/\b(?:\d[ -]*?){13,16}\b/g, '[MASKED_PII]');
974
+ return masked;
975
+ }
976
+ }
977
+ //# sourceMappingURL=governance.js.map