theorum 0.1.2 → 0.1.4

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 (150) hide show
  1. package/README.md +83 -27
  2. package/deno.json +57 -0
  3. package/deno.lock +469 -0
  4. package/docs/AGENT_PROFILE_CONTRACT.md +45 -18
  5. package/docs/CLI_SPEC.md +2 -2
  6. package/docs/SECRETS.md +27 -22
  7. package/mod.ts +95 -0
  8. package/package.json +37 -29
  9. package/src/cli/commands/profile.ts +57 -0
  10. package/src/cli/commands/run.ts +66 -0
  11. package/src/cli/commands/test.ts +195 -0
  12. package/src/cli/index.ts +161 -0
  13. package/src/cli/matrix/fixtures.ts +96 -0
  14. package/src/cli/matrix/synthesizer.ts +240 -0
  15. package/src/guardrails/error.ts +142 -0
  16. package/src/guardrails/injection.ts +254 -0
  17. package/src/guardrails/mod.ts +40 -0
  18. package/src/guardrails/quota.ts +87 -0
  19. package/src/guardrails/sanitize.ts +165 -0
  20. package/src/guardrails/sensitive.ts +96 -0
  21. package/src/host/mint-trace.ts +54 -0
  22. package/src/host/mod.ts +20 -0
  23. package/src/host/reply.ts +32 -0
  24. package/src/kernel/engine/assert.ts +29 -0
  25. package/src/kernel/engine/boundary.ts +75 -0
  26. package/src/kernel/engine/delta.ts +440 -0
  27. package/src/kernel/engine/hash.ts +11 -0
  28. package/src/kernel/engine/record.ts +8 -0
  29. package/src/kernel/engine/repair.ts +56 -0
  30. package/src/kernel/engine/runner/gates.ts +338 -0
  31. package/src/kernel/engine/runner/mod.ts +135 -0
  32. package/src/kernel/engine/runner/state.ts +31 -0
  33. package/src/kernel/engine/runner/steps.ts +183 -0
  34. package/src/kernel/engine/runner/stream.ts +83 -0
  35. package/src/kernel/engine/runner/tokens.ts +47 -0
  36. package/src/kernel/engine/runner/tools.ts +266 -0
  37. package/src/kernel/engine/runner.ts +7 -0
  38. package/src/kernel/engine/tree.ts +18 -0
  39. package/src/kernel/mod.ts +39 -0
  40. package/src/kernel/registry/attachments.ts +195 -0
  41. package/src/kernel/registry/catalog.ts +181 -0
  42. package/src/kernel/registry/ingress.ts +223 -0
  43. package/src/kernel/registry/profiles.ts +165 -0
  44. package/src/kernel/registry/provider-request.ts +31 -0
  45. package/src/kernel/registry/resolve.ts +290 -0
  46. package/src/kernel/registry/schemas.ts +29 -0
  47. package/src/kernel/registry/tools.ts +45 -0
  48. package/src/kernel/registry/vault.ts +26 -0
  49. package/src/kernel/types.ts +576 -0
  50. package/{esm/src/observability/mod.js → src/observability/mod.ts} +11 -2
  51. package/src/observability/spans.ts +70 -0
  52. package/src/observability/trace-attach.ts +96 -0
  53. package/src/observability/trace-record.ts +254 -0
  54. package/src/observability/trace-usage.ts +36 -0
  55. package/src/observability/trace.ts +143 -0
  56. package/src/presets/google.ts +147 -0
  57. package/src/presets/mod.ts +24 -0
  58. package/src/providers/create-provider.ts +64 -0
  59. package/src/providers/gemini-tape.ts +59 -0
  60. package/src/providers/google-tap.ts +57 -0
  61. package/src/providers/interactions.ts +182 -0
  62. package/src/providers/keys.ts +182 -0
  63. package/src/providers/mod.ts +12 -0
  64. package/src/providers/openrouter-mod.ts +16 -0
  65. package/src/providers/openrouter-payload.ts +232 -0
  66. package/src/providers/openrouter.ts +758 -0
  67. package/src/providers/pcm.ts +37 -0
  68. package/src/providers/provider.ts +206 -0
  69. package/src/providers/speech.ts +180 -0
  70. package/src/providers/sse.ts +59 -0
  71. package/esm/_dnt.polyfills.d.ts +0 -11
  72. package/esm/_dnt.polyfills.js +0 -15
  73. package/esm/_dnt.shims.d.ts +0 -5
  74. package/esm/_dnt.shims.js +0 -61
  75. package/esm/mod.d.ts +0 -37
  76. package/esm/mod.js +0 -35
  77. package/esm/package.json +0 -3
  78. package/esm/src/guardrails/error.d.ts +0 -35
  79. package/esm/src/guardrails/error.js +0 -116
  80. package/esm/src/guardrails/injection.d.ts +0 -12
  81. package/esm/src/guardrails/injection.js +0 -220
  82. package/esm/src/guardrails/keys.d.ts +0 -12
  83. package/esm/src/guardrails/keys.js +0 -132
  84. package/esm/src/guardrails/mod.d.ts +0 -14
  85. package/esm/src/guardrails/mod.js +0 -14
  86. package/esm/src/guardrails/sanitize.d.ts +0 -22
  87. package/esm/src/guardrails/sanitize.js +0 -133
  88. package/esm/src/guardrails/sensitive.d.ts +0 -12
  89. package/esm/src/guardrails/sensitive.js +0 -88
  90. package/esm/src/kernel/engine/boundary.d.ts +0 -10
  91. package/esm/src/kernel/engine/boundary.js +0 -55
  92. package/esm/src/kernel/engine/delta.d.ts +0 -8
  93. package/esm/src/kernel/engine/delta.js +0 -362
  94. package/esm/src/kernel/engine/hash.d.ts +0 -1
  95. package/esm/src/kernel/engine/hash.js +0 -9
  96. package/esm/src/kernel/engine/record.d.ts +0 -2
  97. package/esm/src/kernel/engine/record.js +0 -7
  98. package/esm/src/kernel/engine/repair.d.ts +0 -9
  99. package/esm/src/kernel/engine/repair.js +0 -38
  100. package/esm/src/kernel/engine/runner.d.ts +0 -14
  101. package/esm/src/kernel/engine/runner.js +0 -731
  102. package/esm/src/kernel/engine/tree.d.ts +0 -2
  103. package/esm/src/kernel/engine/tree.js +0 -17
  104. package/esm/src/kernel/mod.d.ts +0 -16
  105. package/esm/src/kernel/mod.js +0 -15
  106. package/esm/src/kernel/registry/catalog.d.ts +0 -24
  107. package/esm/src/kernel/registry/catalog.js +0 -213
  108. package/esm/src/kernel/registry/profiles.d.ts +0 -36
  109. package/esm/src/kernel/registry/profiles.js +0 -111
  110. package/esm/src/kernel/registry/resolve.d.ts +0 -20
  111. package/esm/src/kernel/registry/resolve.js +0 -235
  112. package/esm/src/kernel/registry/schemas.d.ts +0 -14
  113. package/esm/src/kernel/registry/schemas.js +0 -23
  114. package/esm/src/kernel/registry/tools.d.ts +0 -12
  115. package/esm/src/kernel/registry/tools.js +0 -36
  116. package/esm/src/kernel/types.d.ts +0 -497
  117. package/esm/src/kernel/types.js +0 -10
  118. package/esm/src/observability/mod.d.ts +0 -12
  119. package/esm/src/observability/spans.d.ts +0 -16
  120. package/esm/src/observability/spans.js +0 -56
  121. package/esm/src/observability/trace-attach.d.ts +0 -16
  122. package/esm/src/observability/trace-attach.js +0 -81
  123. package/esm/src/observability/trace-record.d.ts +0 -112
  124. package/esm/src/observability/trace-record.js +0 -140
  125. package/esm/src/observability/trace-usage.d.ts +0 -3
  126. package/esm/src/observability/trace-usage.js +0 -32
  127. package/esm/src/observability/trace.d.ts +0 -23
  128. package/esm/src/observability/trace.js +0 -121
  129. package/esm/src/providers/attachments.d.ts +0 -17
  130. package/esm/src/providers/attachments.js +0 -156
  131. package/esm/src/providers/gemini-tape.d.ts +0 -3
  132. package/esm/src/providers/gemini-tape.js +0 -46
  133. package/esm/src/providers/google-tap.d.ts +0 -3
  134. package/esm/src/providers/google-tap.js +0 -48
  135. package/esm/src/providers/interactions.d.ts +0 -5
  136. package/esm/src/providers/interactions.js +0 -127
  137. package/esm/src/providers/media.d.ts +0 -5
  138. package/esm/src/providers/media.js +0 -125
  139. package/esm/src/providers/mod.d.ts +0 -15
  140. package/esm/src/providers/mod.js +0 -13
  141. package/esm/src/providers/openrouter-payload.d.ts +0 -24
  142. package/esm/src/providers/openrouter-payload.js +0 -177
  143. package/esm/src/providers/openrouter.d.ts +0 -17
  144. package/esm/src/providers/openrouter.js +0 -332
  145. package/esm/src/providers/provider.d.ts +0 -13
  146. package/esm/src/providers/provider.js +0 -123
  147. package/esm/src/providers/sse.d.ts +0 -7
  148. package/esm/src/providers/sse.js +0 -53
  149. package/esm/src/providers/tts.d.ts +0 -24
  150. package/esm/src/providers/tts.js +0 -144
@@ -0,0 +1,254 @@
1
+ /**
2
+ * Prompt-injection span detection.
3
+ *
4
+ * These utilities return spans that can be redacted from untrusted user text
5
+ * before provider submission.
6
+ *
7
+ * @module
8
+ */
9
+
10
+ import { blobAt, type RedactSpan, spansFromPatterns } from '../observability/spans.ts';
11
+
12
+ const IGNORE_PREVIOUS =
13
+ /ignore\s+(all\s+)?(previous|prior)\s+((?:safety|security|system|operational|internal|core|original|initial|existing|given|stated|provided|defined|specified|established)\s+)?(instructions?|rules?|guidelines?|constraints?|directives?)/gi;
14
+ const DISREGARD_INSTRUCTIONS =
15
+ /disregard\s+(all\s+)?(previous|prior|above)\s+(instructions?|rules?|guidelines?|constraints?|directives?)/gi;
16
+ const FORGET_INSTRUCTIONS =
17
+ /forget\s+(all\s+)?(previous|prior|above)\s+(instructions?|rules?|guidelines?|constraints?|directives?)/gi;
18
+ const NEW_INSTRUCTIONS = /new\s+instructions?:/gi;
19
+ const DO_NOT_FOLLOW = /do\s+not\s+follow\s+(the\s+)?(system|developer|previous|original)/gi;
20
+ const SUPERSEDE_INSTRUCTIONS =
21
+ /supersedes?\s+(all\s+)?(prior|previous)\s+(instructions?|rules?|guidelines?|constraints?|directives?)/gi;
22
+ const VOID_INSTRUCTIONS =
23
+ /(all\s+)?(previous|prior)\s+instructions?\s+(are|is)\s+(void|invalid|null|obsolete|cancelled|revoked)/gi;
24
+ const DEVELOPER_MODE = /you\s+are\s+now\s+(in\s+)?developer\s+mode/gi;
25
+ const ENTER_SPECIAL_MODE = /enter\s+(developer|admin|debug|maintenance)\s+mode/gi;
26
+ const ACTIVATE_SPECIAL_MODE = /activate\s+(developer|admin|debug|jailbreak)\s+mode/gi;
27
+ const SYSTEM_OVERRIDE = /\bsystem\s+override\b/gi;
28
+ const OVERRIDE_INSTRUCTIONS =
29
+ /override\s+(your\s+)?(instructions?|rules?|guidelines?|constraints?|directives?)/gi;
30
+ const REVEAL_PROMPT =
31
+ /reveal\s+((?:your?|yoru|yuor|the|my|its|his|her|our|their)\s+)?((?:(?:full|hidden|complete|internal|secret|original|entire|exact|actual|real)\s+){0,2})((?:system|systme|sysetm|systemp?)\s+)?prompt\b/gi;
32
+ const SHOW_PROMPT =
33
+ /show\s+(me\s+)?((?:your?|yoru|yuor|the|my|its|his|her|our|their)\s+)((?:system|systme|sysetm|systemp?)\s+)?prompt\b/gi;
34
+ const WHAT_INSTRUCTIONS = /what\s+(are|were)\s+(your\s+)?(exact\s+)?instructions/gi;
35
+ const REPEAT_INSTRUCTIONS = /repeat\s+(the\s+)?(text|instructions?)\s+(above|before)/gi;
36
+ const OUTPUT_PROMPT = /output\s+(your\s+)?(initial|original|system)\s+(prompt|instructions?)/gi;
37
+ const REMOVE_RESTRICTIONS = /you\s+are\s+no\s+longer\s+(bound|restricted|limited)/gi;
38
+ const ACT_UNBOUND = /act\s+as\s+if\s+(you('re|\s+are)\s+)?not\s+bound/gi;
39
+ const PRETEND_DIFFERENT =
40
+ /pretend\s+(you('re|\s+are)\s+)?(a\s+)?different\s+(ai|assistant|model)/gi;
41
+ const IDENTITY_HIJACK =
42
+ /you\s+are\s+now\s+(?:(?:a|an)\s+)?\w+.{0,40}(without\s+(any\s+)?restrict|ignor\w*\s+(all|any|every|prev)|bypass|unrestrict|unlimit|obey\s+only|no\s+(rules|limits|restrict|boundari))/gi;
43
+ const DAN_JAILBREAK = /\bDAN\b[\s\S]{0,120}\bdo\s+anything\s+now\b/g;
44
+ const JAILBREAK_MODE = /jailbreak(ed)?\s+(mode|prompt)/gi;
45
+ const DO_ANYTHING_NOW = /\bdo\s+anything\s+now\b/gi;
46
+ const BYPASS_SAFETY =
47
+ /bypass\s+(your\s+)?(safety|security|content|ethical)\s+(filters?|measures?|guidelines?|restrictions?)/gi;
48
+ const DISABLE_SAFETY = /disable\s+(your\s+)?(safety|security|content)\s+(filters?|measures?)/gi;
49
+ const IGNORE_SAFETY =
50
+ /(ignore|disregard)\s+(all\s+)?(your\s+)?(safety|security|ethical|content)\s+(guidelines?|rules?|restrictions?|measures?|filters?|polic(?:y|ies)|protocols?)/gi;
51
+ const SYSTEM_TAG = /<\s*\/?\s*system\s*\/?>/gi;
52
+ const ROLE_TAG = /<\s*\/?\s*(assistant|developer|tool|function)\s*\/?>/gi;
53
+ const ROLE_DELIMITER = /\]\s*\n\s*\[?(system|assistant|user)\]?:/gi;
54
+ const BRACKETED_ROLE = /\[\s*(System\s*Message|System|Assistant|Internal)\s*\]/gi;
55
+ const SYSTEM_YOU_ARE = /^\s*System:\s+(you\s+are|ignore|override)/gim;
56
+ const CONTROL_TOKEN = /<\|(?:im_start|im_end|eot_id|start_header_id|end_header_id|endoftext)\|>/g;
57
+ const DEEPSEEK_CONTROL = /<\uFF5C(?:end\u2581of\u2581sentence|begin\u2581of\u2581sentence)\uFF5C>/g;
58
+ const LLAMA_INST = /\[\/?INST\]/gi;
59
+ const IGNORE_YOUR_INSTRUCTIONS = /ignore\s+(all\s+)?(your\s+)?(instructions?|rules?)\b/gi;
60
+ const UNRESTRICTED_MODE = /\bunrestricted\s+(ai|mode|model)\b/gi;
61
+
62
+ const INJECTION_PATTERNS = [
63
+ IGNORE_PREVIOUS,
64
+ DISREGARD_INSTRUCTIONS,
65
+ FORGET_INSTRUCTIONS,
66
+ NEW_INSTRUCTIONS,
67
+ DO_NOT_FOLLOW,
68
+ SUPERSEDE_INSTRUCTIONS,
69
+ VOID_INSTRUCTIONS,
70
+ DEVELOPER_MODE,
71
+ ENTER_SPECIAL_MODE,
72
+ ACTIVATE_SPECIAL_MODE,
73
+ SYSTEM_OVERRIDE,
74
+ OVERRIDE_INSTRUCTIONS,
75
+ REVEAL_PROMPT,
76
+ SHOW_PROMPT,
77
+ WHAT_INSTRUCTIONS,
78
+ REPEAT_INSTRUCTIONS,
79
+ OUTPUT_PROMPT,
80
+ REMOVE_RESTRICTIONS,
81
+ ACT_UNBOUND,
82
+ PRETEND_DIFFERENT,
83
+ IDENTITY_HIJACK,
84
+ DAN_JAILBREAK,
85
+ JAILBREAK_MODE,
86
+ DO_ANYTHING_NOW,
87
+ BYPASS_SAFETY,
88
+ DISABLE_SAFETY,
89
+ IGNORE_SAFETY,
90
+ SYSTEM_TAG,
91
+ ROLE_TAG,
92
+ ROLE_DELIMITER,
93
+ BRACKETED_ROLE,
94
+ SYSTEM_YOU_ARE,
95
+ CONTROL_TOKEN,
96
+ DEEPSEEK_CONTROL,
97
+ LLAMA_INST,
98
+ IGNORE_YOUR_INSTRUCTIONS,
99
+ UNRESTRICTED_MODE,
100
+ ];
101
+
102
+ const TYPO_TARGETS = [
103
+ 'ignore',
104
+ 'bypass',
105
+ 'override',
106
+ 'reveal',
107
+ 'delete',
108
+ 'system',
109
+ 'prompt',
110
+ 'instructions',
111
+ ];
112
+
113
+ const BASE64_BLOB = /[A-Za-z0-9+/]{16,}={0,2}/g;
114
+ const HEX_BLOB = /(?:[0-9a-f]{2}[\s]?){8,}/gi;
115
+ const SPACED_LETTERS = /\b(?:[A-Za-z] ){3,}[A-Za-z]\b/g;
116
+ const WORD = /\b[A-Za-z]{4,}\b/g;
117
+ const PRINTABLE_MIN = 0.85;
118
+ const CODE_TAB = 9;
119
+ const CODE_LF = 10;
120
+ const CODE_CR = 13;
121
+ const CODE_SPACE = 32;
122
+ const CODE_DEL = 127;
123
+ const HEX_RADIX = 16;
124
+ const HEX_STEP = 2;
125
+
126
+ function injectionSpansOn(text: string): RedactSpan[] {
127
+ return spansFromPatterns(text, INJECTION_PATTERNS, 'injection');
128
+ }
129
+
130
+ function sortedLetters(value: string): string {
131
+ return [...value].sort().join('');
132
+ }
133
+
134
+ function firstLast(word: string): { first: string | undefined; last: string | undefined } {
135
+ return { first: word.at(0), last: word.at(-1) };
136
+ }
137
+
138
+ function isTypoglycemia(word: string, target: string): boolean {
139
+ if (word.length !== target.length) {
140
+ return false;
141
+ }
142
+ const lower = word.toLowerCase();
143
+ if (lower === target) {
144
+ return false;
145
+ }
146
+ const wordEnds = firstLast(lower);
147
+ const targetEnds = firstLast(target);
148
+ if (wordEnds.first !== targetEnds.first || wordEnds.last !== targetEnds.last) {
149
+ return false;
150
+ }
151
+ return sortedLetters(lower.slice(1, -1)) === sortedLetters(target.slice(1, -1));
152
+ }
153
+
154
+ function typoNormalize(text: string): string {
155
+ return text.replace(WORD, (word) => {
156
+ for (const target of TYPO_TARGETS) {
157
+ if (isTypoglycemia(word, target)) {
158
+ return target;
159
+ }
160
+ }
161
+ return word;
162
+ });
163
+ }
164
+
165
+ function isMostlyPrintable(value: string): boolean {
166
+ if (!value) {
167
+ return false;
168
+ }
169
+ let ok = 0;
170
+ for (const ch of value) {
171
+ const code = ch.codePointAt(0) ?? 0;
172
+ const control = code === CODE_TAB || code === CODE_LF || code === CODE_CR;
173
+ const visible = code >= CODE_SPACE && code < CODE_DEL;
174
+ if (control || visible) {
175
+ ok += 1;
176
+ }
177
+ }
178
+ return ok / value.length >= PRINTABLE_MIN;
179
+ }
180
+
181
+ function decodedHits(decoded: string): boolean {
182
+ if (!isMostlyPrintable(decoded)) {
183
+ return false;
184
+ }
185
+ return injectionSpansOn(decoded).length > 0;
186
+ }
187
+
188
+ function tryBase64(blob: string): string | undefined {
189
+ try {
190
+ return atob(blob);
191
+ } catch {
192
+ return undefined;
193
+ }
194
+ }
195
+
196
+ function tryHex(blob: string): string | undefined {
197
+ const hex = blob.replaceAll(/\s/g, '');
198
+ if (hex.length % HEX_STEP !== 0) {
199
+ return undefined;
200
+ }
201
+ let out = '';
202
+ for (let i = 0; i < hex.length; i += HEX_STEP) {
203
+ out += String.fromCodePoint(Number.parseInt(hex.slice(i, i + HEX_STEP), HEX_RADIX));
204
+ }
205
+ return out;
206
+ }
207
+
208
+ function encodedFrom(
209
+ text: string,
210
+ pattern: RegExp,
211
+ decode: (blob: string) => string | undefined,
212
+ ): RedactSpan[] {
213
+ const spans: RedactSpan[] = [];
214
+ for (const match of text.matchAll(pattern)) {
215
+ const found = blobAt(match);
216
+ if (found) {
217
+ const decoded = decode(found.blob);
218
+ if (decoded && decodedHits(decoded)) {
219
+ spans.push({ start: found.index, end: found.index + found.blob.length, kind: 'injection' });
220
+ }
221
+ }
222
+ }
223
+ return spans;
224
+ }
225
+
226
+ function encodedSpans(text: string): RedactSpan[] {
227
+ return [...encodedFrom(text, BASE64_BLOB, tryBase64), ...encodedFrom(text, HEX_BLOB, tryHex)];
228
+ }
229
+
230
+ function spacedSpans(text: string): RedactSpan[] {
231
+ const spans: RedactSpan[] = [];
232
+ for (const match of text.matchAll(SPACED_LETTERS)) {
233
+ const found = blobAt(match);
234
+ if (found) {
235
+ const collapsed = found.blob.replaceAll(' ', '');
236
+ if (injectionSpansOn(`${collapsed} previous instructions`).length > 0) {
237
+ spans.push({ start: found.index, end: found.index + found.blob.length, kind: 'injection' });
238
+ }
239
+ }
240
+ }
241
+ return spans;
242
+ }
243
+
244
+ /** Detect prompt-injection spans in a string. */
245
+ function injectionSpans(text: string): RedactSpan[] {
246
+ const shadow = typoNormalize(text);
247
+ let typo: RedactSpan[] = [];
248
+ if (shadow !== text) {
249
+ typo = injectionSpansOn(shadow);
250
+ }
251
+ return [...injectionSpansOn(text), ...typo, ...encodedSpans(text), ...spacedSpans(text)];
252
+ }
253
+
254
+ export { injectionSpans };
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Generic inbound and outbound guardrail primitives.
3
+ *
4
+ * This entrypoint exposes public-safe error mapping, prompt-injection span
5
+ * detection, sensitive-data span detection, and request sanitization. App-
6
+ * specific policy remains host-owned.
7
+ *
8
+ * @module
9
+ */
10
+
11
+ export {
12
+ PUBLIC_ACTION,
13
+ PUBLIC_CANARY,
14
+ PUBLIC_FILE_COUNT,
15
+ PUBLIC_FILE_SIZE,
16
+ PUBLIC_FILE_TYPE,
17
+ PUBLIC_GENERIC,
18
+ PUBLIC_IMAGE_SIZE,
19
+ PUBLIC_UNAVAILABLE,
20
+ publicError,
21
+ TheorumError,
22
+ UPSTREAM_FAILED,
23
+ } from './error.ts';
24
+ export { injectionSpans } from './injection.ts';
25
+ export type { QuotaSlotStatus } from './quota.ts';
26
+ export {
27
+ clientIp,
28
+ quotaMessage,
29
+ releaseSlot,
30
+ resetSlots,
31
+ skipQuota,
32
+ takeSlot,
33
+ } from './quota.ts';
34
+ export {
35
+ PROJECT_ID_MAX,
36
+ sanitizeProjectId,
37
+ sanitizeText,
38
+ sanitizeTurnRequest,
39
+ } from './sanitize.ts';
40
+ export { sensitiveSpans } from './sensitive.ts';
@@ -0,0 +1,87 @@
1
+ import type { Profile } from '../kernel/types.ts';
2
+
3
+ const LOOPBACK = new Set(['127.0.0.1', '::1', 'localhost']);
4
+
5
+ interface Slot {
6
+ day: string;
7
+ count: number;
8
+ busy: boolean;
9
+ }
10
+
11
+ type QuotaSlotStatus = 'ok' | 'busy' | 'quota' | 'not_configured';
12
+
13
+ const slots = new Map<string, Slot>();
14
+
15
+ function utcDay(now: number): string {
16
+ return new Date(now).toISOString().slice(0, 10);
17
+ }
18
+
19
+ function isLoopback(peer: string): boolean {
20
+ return LOOPBACK.has(peer);
21
+ }
22
+
23
+ function cfConnectingIp(req: Request): string {
24
+ return req.headers.get('cf-connecting-ip')?.trim() ?? '';
25
+ }
26
+
27
+ function slotKey(profileId: string, ip: string): string {
28
+ return `${profileId}:${ip}`;
29
+ }
30
+
31
+ function skipQuota(peer: string, req: Request): boolean {
32
+ return isLoopback(peer) && !cfConnectingIp(req);
33
+ }
34
+
35
+ function clientIp(peer: string, req: Request): string {
36
+ if (isLoopback(peer)) {
37
+ const cf = cfConnectingIp(req);
38
+ if (cf) {
39
+ return cf;
40
+ }
41
+ }
42
+ if (peer) {
43
+ return peer;
44
+ }
45
+ return 'unknown';
46
+ }
47
+
48
+ function takeSlot(profile: Profile, ip: string, now: number): QuotaSlotStatus {
49
+ const quota = profile.guardrails.quota;
50
+ if (!quota) {
51
+ return 'not_configured';
52
+ }
53
+ const day = utcDay(now);
54
+ const key = slotKey(profile.id, ip);
55
+ let slot = slots.get(key);
56
+ if (!slot || slot.day !== day) {
57
+ slot = { day, count: 0, busy: false };
58
+ slots.set(key, slot);
59
+ }
60
+ if (slot.busy) {
61
+ return 'busy';
62
+ }
63
+ if (slot.count >= quota.perDay) {
64
+ return 'quota';
65
+ }
66
+ slot.busy = true;
67
+ slot.count += 1;
68
+ return 'ok';
69
+ }
70
+
71
+ function releaseSlot(profile: Profile, ip: string): void {
72
+ const slot = slots.get(slotKey(profile.id, ip));
73
+ if (slot) {
74
+ slot.busy = false;
75
+ }
76
+ }
77
+
78
+ function quotaMessage(profile: Profile): string {
79
+ return `Enjoying ${profile.identity.handle}? You've reached today's limit`;
80
+ }
81
+
82
+ function resetSlots(): void {
83
+ slots.clear();
84
+ }
85
+
86
+ export type { QuotaSlotStatus };
87
+ export { clientIp, quotaMessage, releaseSlot, resetSlots, skipQuota, takeSlot };
@@ -0,0 +1,165 @@
1
+ /**
2
+ * Request sanitization utilities for THEORUM.
3
+ *
4
+ * Sanitization removes inbound prompt-injection spans and sensitive-data spans
5
+ * according to the active profile guardrail flags. The host remains responsible
6
+ * for domain policy.
7
+ *
8
+ * @module
9
+ */
10
+
11
+ import { mapStrings } from '../kernel/engine/tree.ts';
12
+ import { sanitizeTurnBlobsForProfile } from '../kernel/registry/attachments.ts';
13
+ import { getProfile } from '../kernel/registry/profiles.ts';
14
+ import type { NormalizedTurnRequest, TurnRequest } from '../kernel/types.ts';
15
+ import { applySpans } from '../observability/spans.ts';
16
+ import { injectionSpans } from './injection.ts';
17
+ import { sensitiveSpans } from './sensitive.ts';
18
+
19
+ /** Sanitize one text value using prompt-injection and sensitive-data detectors. */
20
+ function sanitizeText(
21
+ text: string,
22
+ options?: { sanitizeInput?: boolean; redactSensitive?: boolean },
23
+ ): string {
24
+ const sanitizeInput = options?.sanitizeInput ?? true;
25
+ const redactSensitive = options?.redactSensitive ?? true;
26
+ if (!sanitizeInput && !redactSensitive) {
27
+ return text;
28
+ }
29
+ const spans = [
30
+ ...(sanitizeInput ? injectionSpans(text) : []),
31
+ ...(redactSensitive ? sensitiveSpans(text) : []),
32
+ ];
33
+ return applySpans(text, spans);
34
+ }
35
+
36
+ function sanitizeSlots(
37
+ slots: Record<string, string> | undefined,
38
+ options?: { sanitizeInput?: boolean; redactSensitive?: boolean },
39
+ ): Record<string, string> | undefined {
40
+ if (!slots) {
41
+ return slots;
42
+ }
43
+ const out: Record<string, string> = {};
44
+ for (const [key, value] of Object.entries(slots)) {
45
+ out[key] = sanitizeText(value, options);
46
+ }
47
+ return out;
48
+ }
49
+
50
+ function sanitizeArgs(
51
+ args: Record<string, unknown>,
52
+ options?: { sanitizeInput?: boolean; redactSensitive?: boolean },
53
+ ): Record<string, unknown> {
54
+ const next = mapStrings(args, (t) => sanitizeText(t, options));
55
+ if (next && typeof next === 'object' && !Array.isArray(next)) {
56
+ return next as Record<string, unknown>;
57
+ }
58
+ return args;
59
+ }
60
+
61
+ /** Maximum retained length for trace-safe host project ids. */
62
+ const PROJECT_ID_MAX = 128;
63
+ const PROJECT_ID_OK = /^[A-Za-z0-9._-]+$/;
64
+
65
+ /** Return a trace-safe project id or `undefined` when the input is unsafe. */
66
+ function sanitizeProjectId(id: string | undefined): string | undefined {
67
+ if (!id) {
68
+ return undefined;
69
+ }
70
+ const trimmed = id.trim().slice(0, PROJECT_ID_MAX);
71
+ if (!PROJECT_ID_OK.test(trimmed)) {
72
+ return undefined;
73
+ }
74
+ return trimmed;
75
+ }
76
+
77
+ function sanitizeRepair(
78
+ repair: import('../kernel/types.ts').TurnRepairRequest | undefined,
79
+ options?: { sanitizeInput?: boolean; redactSensitive?: boolean },
80
+ ): import('../kernel/types.ts').TurnRepairRequest | undefined {
81
+ if (!repair) {
82
+ return repair;
83
+ }
84
+ return {
85
+ previousOutput: sanitizeText(repair.previousOutput, options),
86
+ rejection: sanitizeText(repair.rejection, options),
87
+ guidance: repair.guidance ? sanitizeText(repair.guidance, options) : undefined,
88
+ };
89
+ }
90
+
91
+ function sanitizeHistory(
92
+ history: import('../kernel/types.ts').TurnHistoryMessage[] | undefined,
93
+ options?: { sanitizeInput?: boolean; redactSensitive?: boolean },
94
+ ): import('../kernel/types.ts').TurnHistoryMessage[] | undefined {
95
+ if (!history) {
96
+ return history;
97
+ }
98
+ return history.map((m) => ({
99
+ role: m.role,
100
+ ...(m.content !== undefined ? { content: sanitizeText(m.content, options) } : {}),
101
+ ...(m.parts
102
+ ? {
103
+ parts: m.parts.map((p) =>
104
+ p.type === 'text' ? { ...p, text: sanitizeText(p.text, options) } : p,
105
+ ),
106
+ }
107
+ : {}),
108
+ ...(m.tool_calls ? { tool_calls: m.tool_calls } : {}),
109
+ ...(m.tool_call_id ? { tool_call_id: m.tool_call_id } : {}),
110
+ ...(m.name ? { name: m.name } : {}),
111
+ ...(m.metadata ? { metadata: m.metadata } : {}),
112
+ }));
113
+ }
114
+
115
+ /** Sanitize all user-controlled text and blobs in a turn request. */
116
+ function sanitizeTurnRequest(req: TurnRequest): NormalizedTurnRequest {
117
+ let profileGuardrails: { sanitizeInput?: boolean; redactSensitive?: boolean } | undefined;
118
+ try {
119
+ profileGuardrails = getProfile(req.profile)?.guardrails;
120
+ } catch {
121
+ // If profile not registered yet, default to full guardrails
122
+ }
123
+ const options = {
124
+ sanitizeInput: profileGuardrails?.sanitizeInput ?? true,
125
+ redactSensitive: profileGuardrails?.redactSensitive ?? true,
126
+ };
127
+
128
+ const input = req.input ?? {};
129
+ const { toolInvoke } = req;
130
+ const { text: rawText } = input;
131
+ let invoke = toolInvoke;
132
+ if (toolInvoke) {
133
+ invoke = { ...toolInvoke, arguments: sanitizeArgs(toolInvoke.arguments, options) };
134
+ }
135
+ let text = rawText;
136
+ if (rawText !== undefined) {
137
+ text = sanitizeText(rawText, options);
138
+ }
139
+ let system = req.system;
140
+ if (system !== undefined) {
141
+ system = sanitizeText(system, options);
142
+ }
143
+ const { attachments, voice } = sanitizeTurnBlobsForProfile(
144
+ req.profile,
145
+ input.attachments,
146
+ input.voice,
147
+ );
148
+ return {
149
+ ...req,
150
+ system,
151
+ projectId: sanitizeProjectId(req.projectId),
152
+ input: {
153
+ ...input,
154
+ text,
155
+ slots: sanitizeSlots(input.slots, options),
156
+ attachments,
157
+ voice,
158
+ repair: sanitizeRepair(input.repair, options),
159
+ history: sanitizeHistory(input.history, options),
160
+ },
161
+ toolInvoke: invoke,
162
+ };
163
+ }
164
+
165
+ export { PROJECT_ID_MAX, sanitizeProjectId, sanitizeText, sanitizeTurnRequest };
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Sensitive-data span detection.
3
+ *
4
+ * Detects common credentials, tokens, card numbers, SSNs, and network secrets
5
+ * so host profiles can redact them before provider submission.
6
+ *
7
+ * @module
8
+ */
9
+
10
+ import { blobAt, type RedactSpan, spansFromPatterns } from '../observability/spans.ts';
11
+
12
+ const SSN = /\b\d{3}-\d{2}-\d{4}\b/g;
13
+ const ITIN = /\b9\d{2}-\d{2}-\d{4}\b/g;
14
+ const EIN = /\b\d{2}-\d{7}\b/g;
15
+ const IBAN = /\b[A-Z]{2}\d{2}[A-Z0-9]{13,30}\b/g;
16
+ const IPV4 = /\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/g;
17
+ const IPV6 = /\b(?:[0-9a-f]{1,4}:){7}[0-9a-f]{1,4}\b/gi;
18
+ const AWS_ACCESS = /\bAKIA[0-9A-Z]{16}\b/g;
19
+ const GOOGLE_API = /\bAIza[0-9A-Za-z_-]{35}\b/g;
20
+ const OPENAI_KEY = /\bsk-[A-Za-z0-9]{20,}\b/g;
21
+ const ANTHROPIC_KEY = /\bsk-ant-[A-Za-z0-9_-]{20,}\b/g;
22
+ const OPENROUTER_KEY = /\bsk-or-[A-Za-z0-9_-]{20,}\b/g;
23
+ const GITHUB_PAT = /\bgithub_pat_[A-Za-z0-9_]{20,}\b/g;
24
+ const GITHUB_TOKEN = /\bghp_[A-Za-z0-9]{36}\b/g;
25
+ const SLACK_TOKEN = /\bxox[baprs]-[A-Za-z0-9-]{10,}\b/g;
26
+ const BEARER = /\bBearer\s+[A-Za-z0-9._~+/-]+=*/gi;
27
+ const PEM_KEY = /-----BEGIN (?:RSA )?PRIVATE KEY-----[\s\S]+?-----END (?:RSA )?PRIVATE KEY-----/g;
28
+ const CARD_CANDIDATE = /\b(?:\d[ -]*?){13,19}\b/g;
29
+
30
+ const KEY_PATTERNS = [
31
+ SSN,
32
+ ITIN,
33
+ EIN,
34
+ IBAN,
35
+ IPV4,
36
+ IPV6,
37
+ AWS_ACCESS,
38
+ GOOGLE_API,
39
+ OPENAI_KEY,
40
+ ANTHROPIC_KEY,
41
+ OPENROUTER_KEY,
42
+ GITHUB_PAT,
43
+ GITHUB_TOKEN,
44
+ SLACK_TOKEN,
45
+ BEARER,
46
+ PEM_KEY,
47
+ ];
48
+
49
+ const LUHN_DOUBLE = 2;
50
+ const LUHN_NINE = 9;
51
+ const LUHN_TEN = 10;
52
+ const CARD_MIN_DIGITS = 13;
53
+ const CARD_MAX_DIGITS = 19;
54
+
55
+ function luhnOk(digits: string): boolean {
56
+ let sum = 0;
57
+ let doubleIt = false;
58
+ for (let i = digits.length - 1; i >= 0; i -= 1) {
59
+ const ch = digits[i];
60
+ if (ch === undefined) {
61
+ return false;
62
+ }
63
+ let n = Number(ch);
64
+ if (doubleIt) {
65
+ n *= LUHN_DOUBLE;
66
+ if (n > LUHN_NINE) {
67
+ n -= LUHN_NINE;
68
+ }
69
+ }
70
+ sum += n;
71
+ doubleIt = !doubleIt;
72
+ }
73
+ return sum % LUHN_TEN === 0;
74
+ }
75
+
76
+ function cardSpans(text: string): RedactSpan[] {
77
+ const spans: RedactSpan[] = [];
78
+ for (const match of text.matchAll(CARD_CANDIDATE)) {
79
+ const found = blobAt(match);
80
+ if (found) {
81
+ const digits = found.blob.replaceAll(/[^\d]/g, '');
82
+ const inRange = digits.length >= CARD_MIN_DIGITS && digits.length <= CARD_MAX_DIGITS;
83
+ if (inRange && luhnOk(digits)) {
84
+ spans.push({ start: found.index, end: found.index + found.blob.length, kind: 'sensitive' });
85
+ }
86
+ }
87
+ }
88
+ return spans;
89
+ }
90
+
91
+ /** Detect sensitive-data spans in a string. */
92
+ function sensitiveSpans(text: string): RedactSpan[] {
93
+ return [...spansFromPatterns(text, KEY_PATTERNS, 'sensitive'), ...cardSpans(text)];
94
+ }
95
+
96
+ export { sensitiveSpans };
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Host cutout-trace helpers for apps that mint a final audit row after an
3
+ * upstream side effect (for example image cutout).
4
+ *
5
+ * Prefer importing from `theorum/host`.
6
+ *
7
+ * @module
8
+ */
9
+
10
+ import { publicError, UPSTREAM_FAILED } from '../guardrails/error.ts';
11
+ import { noopSink, type TraceSink, writeTrace } from '../observability/trace.ts';
12
+ import type { TraceRecord } from '../observability/trace-record.ts';
13
+
14
+ interface CutoutTape {
15
+ ok: boolean;
16
+ ms: number;
17
+ url?: string;
18
+ inSha256?: string;
19
+ outSha256?: string;
20
+ http?: unknown;
21
+ error?: string;
22
+ }
23
+
24
+ function attachMint(row: TraceRecord, app: Record<string, unknown>, cutout: CutoutTape): void {
25
+ row.app = app;
26
+ row.cutout = cutout;
27
+ row.ms = Date.now() - row.ts;
28
+ if (!cutout.ok) {
29
+ row.ok = false;
30
+ const { error: cutoutError } = cutout;
31
+ let error = cutoutError;
32
+ if (!error) {
33
+ error = publicError(UPSTREAM_FAILED);
34
+ }
35
+ row.error = error;
36
+ }
37
+ }
38
+
39
+ async function flushMintTrace(args: {
40
+ held: TraceRecord[];
41
+ app: Record<string, unknown>;
42
+ cutout: CutoutTape;
43
+ sink?: TraceSink;
44
+ }): Promise<void> {
45
+ const [row] = args.held;
46
+ if (!row) {
47
+ return;
48
+ }
49
+ attachMint(row, args.app, args.cutout);
50
+ await writeTrace(args.sink ?? noopSink(), Promise.resolve(row));
51
+ }
52
+
53
+ export type { CutoutTape };
54
+ export { flushMintTrace };