@highflame/policy 2.0.8 → 2.0.9

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 (126) hide show
  1. package/dist/actions.gen.d.ts +0 -1
  2. package/dist/actions.gen.js +0 -1
  3. package/dist/annotations.d.ts +0 -1
  4. package/dist/annotations.js +0 -1
  5. package/dist/builder.d.ts +0 -1
  6. package/dist/builder.js +0 -1
  7. package/dist/context.gen.d.ts +0 -1
  8. package/dist/context.gen.js +0 -1
  9. package/dist/engine.d.ts +0 -1
  10. package/dist/engine.js +0 -1
  11. package/dist/entities.gen.d.ts +0 -1
  12. package/dist/entities.gen.js +0 -1
  13. package/dist/entity-metadata-types.gen.d.ts +0 -1
  14. package/dist/entity-metadata-types.gen.js +0 -1
  15. package/dist/errors.d.ts +0 -1
  16. package/dist/errors.js +0 -1
  17. package/dist/index.d.ts +0 -1
  18. package/dist/index.js +0 -1
  19. package/dist/overwatch-context.gen.d.ts +0 -1
  20. package/dist/overwatch-context.gen.js +0 -1
  21. package/dist/overwatch-defaults.gen.d.ts +0 -1
  22. package/dist/overwatch-defaults.gen.js +0 -1
  23. package/dist/overwatch-entities.gen.d.ts +0 -1
  24. package/dist/overwatch-entities.gen.js +0 -1
  25. package/dist/palisade-context.gen.d.ts +0 -1
  26. package/dist/palisade-context.gen.js +0 -1
  27. package/dist/palisade-entities.gen.d.ts +0 -1
  28. package/dist/palisade-entities.gen.js +0 -1
  29. package/dist/parser.d.ts +0 -1
  30. package/dist/parser.js +0 -1
  31. package/dist/schema.gen.d.ts +0 -1
  32. package/dist/schema.gen.js +0 -1
  33. package/dist/schemas.d.ts +0 -1
  34. package/dist/schemas.js +0 -1
  35. package/dist/service-schemas.gen.d.ts +0 -1
  36. package/dist/service-schemas.gen.js +0 -1
  37. package/dist/types.d.ts +0 -1
  38. package/dist/types.js +0 -1
  39. package/package.json +1 -2
  40. package/dist/actions.gen.d.ts.map +0 -1
  41. package/dist/actions.gen.js.map +0 -1
  42. package/dist/annotations.d.ts.map +0 -1
  43. package/dist/annotations.js.map +0 -1
  44. package/dist/builder.d.ts.map +0 -1
  45. package/dist/builder.js.map +0 -1
  46. package/dist/context.gen.d.ts.map +0 -1
  47. package/dist/context.gen.js.map +0 -1
  48. package/dist/engine.d.ts.map +0 -1
  49. package/dist/engine.js.map +0 -1
  50. package/dist/engine.test.d.ts +0 -8
  51. package/dist/engine.test.d.ts.map +0 -1
  52. package/dist/engine.test.js +0 -190
  53. package/dist/engine.test.js.map +0 -1
  54. package/dist/entities.gen.d.ts.map +0 -1
  55. package/dist/entities.gen.js.map +0 -1
  56. package/dist/entity-metadata-types.gen.d.ts.map +0 -1
  57. package/dist/entity-metadata-types.gen.js.map +0 -1
  58. package/dist/errors.d.ts.map +0 -1
  59. package/dist/errors.js.map +0 -1
  60. package/dist/index.d.ts.map +0 -1
  61. package/dist/index.js.map +0 -1
  62. package/dist/overwatch-context.gen.d.ts.map +0 -1
  63. package/dist/overwatch-context.gen.js.map +0 -1
  64. package/dist/overwatch-defaults.gen.d.ts.map +0 -1
  65. package/dist/overwatch-defaults.gen.js.map +0 -1
  66. package/dist/overwatch-defaults.test.d.ts +0 -8
  67. package/dist/overwatch-defaults.test.d.ts.map +0 -1
  68. package/dist/overwatch-defaults.test.js +0 -145
  69. package/dist/overwatch-defaults.test.js.map +0 -1
  70. package/dist/overwatch-entities.gen.d.ts.map +0 -1
  71. package/dist/overwatch-entities.gen.js.map +0 -1
  72. package/dist/overwatch-rebac.test.d.ts +0 -25
  73. package/dist/overwatch-rebac.test.d.ts.map +0 -1
  74. package/dist/overwatch-rebac.test.js +0 -301
  75. package/dist/overwatch-rebac.test.js.map +0 -1
  76. package/dist/palisade-context.gen.d.ts.map +0 -1
  77. package/dist/palisade-context.gen.js.map +0 -1
  78. package/dist/palisade-entities.gen.d.ts.map +0 -1
  79. package/dist/palisade-entities.gen.js.map +0 -1
  80. package/dist/parser.d.ts.map +0 -1
  81. package/dist/parser.js.map +0 -1
  82. package/dist/parser.test.d.ts +0 -8
  83. package/dist/parser.test.d.ts.map +0 -1
  84. package/dist/parser.test.js +0 -212
  85. package/dist/parser.test.js.map +0 -1
  86. package/dist/schema.gen.d.ts.map +0 -1
  87. package/dist/schema.gen.js.map +0 -1
  88. package/dist/schemas.d.ts.map +0 -1
  89. package/dist/schemas.js.map +0 -1
  90. package/dist/schemas.test.d.ts +0 -8
  91. package/dist/schemas.test.d.ts.map +0 -1
  92. package/dist/schemas.test.js +0 -407
  93. package/dist/schemas.test.js.map +0 -1
  94. package/dist/service-schemas.gen.d.ts.map +0 -1
  95. package/dist/service-schemas.gen.js.map +0 -1
  96. package/dist/studio-ui.test.d.ts +0 -8
  97. package/dist/studio-ui.test.d.ts.map +0 -1
  98. package/dist/studio-ui.test.js +0 -687
  99. package/dist/studio-ui.test.js.map +0 -1
  100. package/dist/types.d.ts.map +0 -1
  101. package/dist/types.js.map +0 -1
  102. package/src/actions.gen.ts +0 -57
  103. package/src/annotations.ts +0 -243
  104. package/src/builder.ts +0 -799
  105. package/src/context.gen.ts +0 -10
  106. package/src/engine.test.ts +0 -370
  107. package/src/engine.ts +0 -497
  108. package/src/entities.gen.ts +0 -65
  109. package/src/entity-metadata-types.gen.ts +0 -19
  110. package/src/errors.ts +0 -195
  111. package/src/index.ts +0 -62
  112. package/src/overwatch-context.gen.ts +0 -45
  113. package/src/overwatch-defaults.gen.ts +0 -1255
  114. package/src/overwatch-defaults.test.ts +0 -176
  115. package/src/overwatch-entities.gen.ts +0 -41
  116. package/src/overwatch-rebac.test.ts +0 -346
  117. package/src/palisade-context.gen.ts +0 -28
  118. package/src/palisade-entities.gen.ts +0 -49
  119. package/src/parser.test.ts +0 -251
  120. package/src/parser.ts +0 -579
  121. package/src/schema.gen.ts +0 -134
  122. package/src/schemas.test.ts +0 -477
  123. package/src/schemas.ts +0 -91
  124. package/src/service-schemas.gen.ts +0 -608
  125. package/src/studio-ui.test.ts +0 -813
  126. package/src/types.ts +0 -66
@@ -1,813 +0,0 @@
1
- /**
2
- * Studio UI Integration Tests
3
- *
4
- * These tests simulate exactly how the Studio UI (Overwatch admin dashboard)
5
- * will use the @highflame/policy npm package.
6
- */
7
-
8
- import { describe, it, expect } from 'vitest';
9
-
10
- // Browser-safe imports (simulating '@highflame/policy/types')
11
- import {
12
- EntityType,
13
- ActionType,
14
- PolicyBuilder,
15
- OVERWATCH_SCHEMA,
16
- PALISADE_SCHEMA,
17
- OVERWATCH_CONTEXT,
18
- PALISADE_CONTEXT,
19
- OverwatchContextKey,
20
- PalisadeContextKey,
21
- // Entity metadata for UI dropdowns
22
- OVERWATCH_ENTITIES,
23
- OVERWATCH_ACTION_ENTITIES,
24
- PALISADE_ENTITIES,
25
- PALISADE_ACTION_ENTITIES,
26
- type ServiceContext,
27
- type ActionContext,
28
- type ServiceEntityMetadata,
29
- type ActionEntityMetadata,
30
- } from './types.js';
31
-
32
- // Node.js only imports (for API routes)
33
- import {
34
- PolicyEngine,
35
- PolicyValidator,
36
- newEntityUID,
37
- newEntity,
38
- } from './index.js';
39
-
40
- describe('Studio UI Integration Tests', () => {
41
- /**
42
- * Test: PolicyBuilder action formatting
43
- *
44
- * Tests that simple action names are properly formatted to Cedar syntax.
45
- * This is the "normal approach" for non-namespaced schemas.
46
- */
47
- it('should format simple action names to Cedar syntax', () => {
48
- // Using simple action name (normal approach)
49
- const policy = PolicyBuilder.permit()
50
- .principalType('User')
51
- .action('call_tool')
52
- .resourceType('Tool')
53
- .build();
54
-
55
- const cedarText = policy.toCedar();
56
-
57
- // Simple actions should be wrapped as Action::"..."
58
- expect(cedarText).toContain('action == Action::"call_tool"');
59
- expect(cedarText).not.toContain('Action::"Action::'); // No double-wrapping
60
- });
61
-
62
- /**
63
- * Test 1: Schema and Context Loading
64
- *
65
- * Studio UI needs to load schemas for validation and context metadata
66
- * for populating form dropdowns.
67
- */
68
- it('should load schemas and context metadata for form builders', () => {
69
- // Schemas should be strings
70
- expect(typeof OVERWATCH_SCHEMA).toBe('string');
71
- expect(OVERWATCH_SCHEMA).toContain('namespace Overwatch');
72
- expect(typeof PALISADE_SCHEMA).toBe('string');
73
- expect(PALISADE_SCHEMA).toContain('namespace Palisade');
74
-
75
- // Context metadata should have action definitions
76
- expect(OVERWATCH_CONTEXT.service).toBe('overwatch');
77
- expect(OVERWATCH_CONTEXT.actions.length).toBeGreaterThan(0);
78
-
79
- // Find call_tool action and verify context attributes
80
- const callToolAction = OVERWATCH_CONTEXT.actions.find(
81
- (a: ActionContext) => a.name === 'call_tool'
82
- );
83
- expect(callToolAction).toBeDefined();
84
- expect(callToolAction!.context_attributes.length).toBeGreaterThan(0);
85
-
86
- // Context keys should be available for type-safe form building
87
- expect(OverwatchContextKey.ToolName).toBe('tool_name');
88
- expect(OverwatchContextKey.ThreatCount).toBe('threat_count');
89
- expect(PalisadeContextKey.Severity).toBe('severity');
90
- expect(PalisadeContextKey.Environment).toBe('environment');
91
- });
92
-
93
- /**
94
- * Test 2: Full Round-Trip - Create Policy → Validate → Evaluate
95
- *
96
- * This simulates the complete flow:
97
- * 1. User creates a policy using PolicyBuilder in the UI
98
- * 2. API validates the policy against the schema
99
- * 3. Runtime evaluates the policy
100
- */
101
- it('should complete full policy lifecycle for Overwatch', () => {
102
- // Step 1: User creates policy in form UI
103
- const policy = PolicyBuilder.permit()
104
- .principalType('Overwatch::User')
105
- .action('Overwatch::Action::"call_tool"')
106
- .resourceType('Overwatch::Tool')
107
- .whenRaw('context.threat_count < 5')
108
- .build();
109
-
110
- const cedarText = policy.toCedar();
111
- expect(cedarText).toContain('permit');
112
- expect(cedarText).toContain('Overwatch::User');
113
-
114
- // Step 2: API validates policy against schema
115
- const validator = new PolicyValidator(OVERWATCH_SCHEMA);
116
- const validationResult = validator.validate(cedarText);
117
- expect(validationResult.valid).toBe(true);
118
-
119
- // Step 3: Runtime loads and evaluates policy
120
- const engine = new PolicyEngine({ schema: OVERWATCH_SCHEMA });
121
- engine.loadPolicy(cedarText);
122
-
123
- const entities = [
124
- newEntity('Overwatch::User', 'mcp_client', { user_type: 'external', email: 'test@example.com' }),
125
- newEntity('Overwatch::Tool', 'shell', { tool_name: 'shell', risk_level: 'high' }),
126
- ];
127
-
128
- // Full context as Guardian will provide at runtime
129
- const baseContext = {
130
- content: 'ls -la',
131
- source: 'claudecode',
132
- event: 'PreToolUse',
133
- user_email: 'test@example.com',
134
- tool_name: 'shell',
135
- mcp_server: 'filesystem',
136
- mcp_tool: 'shell',
137
- path: '/workspace',
138
- cwd: '/workspace',
139
- workspace_root: '/workspace',
140
- highest_severity: 'low',
141
- threat_categories: [],
142
-
143
- yara_threats: [],
144
- max_threat_severity: 1,
145
- contains_secrets: false,
146
- response_content: '',
147
- };
148
-
149
- // Should allow when threat_count < 5
150
- const allowDecision = engine.evaluate({
151
- principal: newEntityUID('Overwatch::User', 'mcp_client'),
152
- action: 'Overwatch::Action::"call_tool"',
153
- resource: newEntityUID('Overwatch::Tool', 'shell'),
154
- context: { ...baseContext, threat_count: 3 },
155
- entities,
156
- });
157
- expect(allowDecision.effect).toBe('Allow');
158
-
159
- // Should deny when threat_count >= 5 (no matching permit)
160
- const denyDecision = engine.evaluate({
161
- principal: newEntityUID('Overwatch::User', 'mcp_client'),
162
- action: 'Overwatch::Action::"call_tool"',
163
- resource: newEntityUID('Overwatch::Tool', 'shell'),
164
- context: { ...baseContext, threat_count: 10 },
165
- entities,
166
- });
167
- expect(denyDecision.effect).toBe('Deny');
168
- });
169
-
170
- /**
171
- * Test 3: Full Round-Trip for Palisade
172
- *
173
- * Same flow but for Palisade ML security policies.
174
- */
175
- it('should complete full policy lifecycle for Palisade', () => {
176
- // Step 1: Create a forbid policy for CRITICAL findings
177
- const policy = PolicyBuilder.forbid()
178
- .principalType('Palisade::Scanner')
179
- .action('Palisade::Action::"load_model"')
180
- .resourceType('Palisade::Artifact')
181
- .whenRaw('context.severity == "CRITICAL"')
182
- .build();
183
-
184
- const cedarText = policy.toCedar();
185
-
186
- // Step 2: Validate
187
- const validator = new PolicyValidator(PALISADE_SCHEMA);
188
- expect(validator.validate(cedarText).valid).toBe(true);
189
-
190
- // Step 3: Evaluate
191
- const engine = new PolicyEngine({ schema: PALISADE_SCHEMA });
192
- engine.loadPolicy(cedarText);
193
-
194
- const entities = [
195
- newEntity('Palisade::Scanner', 'palisade', { scanner_type: 'ml' }),
196
- newEntity('Palisade::Artifact', 'model.pkl', {
197
- artifact_format: 'pickle',
198
- path: '/model.pkl',
199
- signed: false,
200
- signer: 'unsigned',
201
- }),
202
- ];
203
-
204
- // Should deny CRITICAL findings
205
- const decision = engine.evaluate({
206
- principal: newEntityUID('Palisade::Scanner', 'palisade'),
207
- action: 'Palisade::Action::"load_model"',
208
- resource: newEntityUID('Palisade::Artifact', 'model.pkl'),
209
- context: { severity: 'CRITICAL' },
210
- entities,
211
- });
212
- expect(decision.effect).toBe('Deny');
213
- });
214
-
215
- /**
216
- * Test 4: Entity Metadata for UI Dropdowns - Overwatch
217
- *
218
- * Studio UI needs to know which entity types can be principals,
219
- * which can be resources, and what actions are available.
220
- * This data is extracted from Cedar schema appliesTo blocks.
221
- */
222
- it('should provide correct entity metadata for Overwatch UI dropdowns', () => {
223
- // Verify ServiceEntityMetadata structure
224
- expect(OVERWATCH_ENTITIES.principals).toBeDefined();
225
- expect(OVERWATCH_ENTITIES.resources).toBeDefined();
226
- expect(OVERWATCH_ENTITIES.actions).toBeDefined();
227
-
228
- // Overwatch principals should include User and Agent
229
- expect(OVERWATCH_ENTITIES.principals).toContain('Agent');
230
- expect(OVERWATCH_ENTITIES.principals).toContain('User');
231
- expect(OVERWATCH_ENTITIES.principals).toHaveLength(2);
232
-
233
- // Overwatch resources should include all resource types
234
- expect(OVERWATCH_ENTITIES.resources).toContain('FilePath');
235
- expect(OVERWATCH_ENTITIES.resources).toContain('LlmPrompt');
236
- expect(OVERWATCH_ENTITIES.resources).toContain('Server');
237
- expect(OVERWATCH_ENTITIES.resources).toContain('Tool');
238
- expect(OVERWATCH_ENTITIES.resources).toHaveLength(4);
239
-
240
- // Overwatch actions should match schema
241
- expect(OVERWATCH_ENTITIES.actions).toContain('call_tool');
242
- expect(OVERWATCH_ENTITIES.actions).toContain('connect_server');
243
- expect(OVERWATCH_ENTITIES.actions).toContain('process_prompt');
244
- expect(OVERWATCH_ENTITIES.actions).toContain('read_file');
245
- expect(OVERWATCH_ENTITIES.actions).toContain('write_file');
246
- expect(OVERWATCH_ENTITIES.actions).toHaveLength(5);
247
- });
248
-
249
- /**
250
- * Test 5: Per-Action Entity Mapping - Overwatch
251
- *
252
- * Studio UI needs to filter dropdowns based on selected action.
253
- * Each action has specific valid principals and resources.
254
- */
255
- it('should provide per-action entity mapping for Overwatch', () => {
256
- // call_tool action should have correct principals and resources
257
- const callTool = OVERWATCH_ACTION_ENTITIES['call_tool'];
258
- expect(callTool).toBeDefined();
259
- expect(callTool.principals).toContain('User');
260
- expect(callTool.principals).toContain('Agent');
261
- expect(callTool.resources).toContain('Tool');
262
- expect(callTool.resources).toContain('FilePath');
263
-
264
- // connect_server action should only apply to Server resource
265
- const connectServer = OVERWATCH_ACTION_ENTITIES['connect_server'];
266
- expect(connectServer).toBeDefined();
267
- expect(connectServer.principals).toContain('User');
268
- expect(connectServer.principals).toContain('Agent');
269
- expect(connectServer.resources).toContain('Server');
270
- expect(connectServer.resources).not.toContain('Tool');
271
-
272
- // process_prompt action should only apply to LlmPrompt resource
273
- const processPrompt = OVERWATCH_ACTION_ENTITIES['process_prompt'];
274
- expect(processPrompt).toBeDefined();
275
- expect(processPrompt.resources).toContain('LlmPrompt');
276
- expect(processPrompt.resources).not.toContain('Tool');
277
-
278
- // read_file and write_file should apply to FilePath resource
279
- const readFile = OVERWATCH_ACTION_ENTITIES['read_file'];
280
- const writeFile = OVERWATCH_ACTION_ENTITIES['write_file'];
281
- expect(readFile.resources).toContain('FilePath');
282
- expect(writeFile.resources).toContain('FilePath');
283
- });
284
-
285
- /**
286
- * Test 6: Entity Metadata for Palisade
287
- *
288
- * Verify Palisade service also has correct entity metadata.
289
- */
290
- it('should provide correct entity metadata for Palisade UI dropdowns', () => {
291
- // Palisade has Scanner as principal
292
- expect(PALISADE_ENTITIES.principals).toContain('Scanner');
293
-
294
- // Palisade resources include Artifact and Package
295
- expect(PALISADE_ENTITIES.resources).toContain('Artifact');
296
- expect(PALISADE_ENTITIES.resources).toContain('Package');
297
-
298
- // Palisade actions
299
- expect(PALISADE_ENTITIES.actions).toContain('load_model');
300
- expect(PALISADE_ENTITIES.actions).toContain('scan_artifact');
301
- expect(PALISADE_ENTITIES.actions).toContain('quarantine_artifact');
302
-
303
- // Per-action mapping - load_model applies to Artifact
304
- const loadModel = PALISADE_ACTION_ENTITIES['load_model'];
305
- expect(loadModel).toBeDefined();
306
- expect(loadModel.principals).toContain('Scanner');
307
- expect(loadModel.resources).toContain('Artifact');
308
-
309
- // scan_package applies to Package resource
310
- const scanPackage = PALISADE_ACTION_ENTITIES['scan_package'];
311
- expect(scanPackage).toBeDefined();
312
- expect(scanPackage.resources).toContain('Package');
313
- });
314
- });
315
-
316
- /**
317
- * Cedar Policy Annotations Integration Tests
318
- *
319
- * These tests verify the full round-trip of Cedar policy annotations:
320
- * PolicyRule → Cedar text → parse back → verify annotations preserved
321
- */
322
- // Import annotation functions for tests
323
- import {
324
- ruleToCedar,
325
- rulesToCedar,
326
- parseCedarToRules,
327
- generateRuleId,
328
- isValidAnnotationKey,
329
- type PolicyRule,
330
- type PolicySeverity,
331
- type PolicyEffect,
332
- type ConditionOperator,
333
- } from './index.js';
334
-
335
- describe('Cedar Annotations Integration Tests', () => {
336
-
337
- /**
338
- * Test 1: Basic Annotations Round-Trip
339
- *
340
- * Create a PolicyRule with all standard annotations, convert to Cedar,
341
- * parse back, and verify all annotations are preserved.
342
- */
343
- it('should preserve annotations through Cedar round-trip', () => {
344
- // Step 1: Create a PolicyRule with full annotations
345
- const originalRule: PolicyRule = {
346
- annotations: {
347
- id: 'rule-001',
348
- name: 'Block high-threat tool calls',
349
- description: 'Forbids tool calls when threat count exceeds threshold',
350
- severity: 'high' as PolicySeverity,
351
- tags: ['security', 'baseline', 'v2'],
352
- },
353
- effect: 'forbid' as PolicyEffect,
354
- principal: { type: 'Overwatch::User' },
355
- action: 'Overwatch::Action::"call_tool"',
356
- resource: { type: 'Overwatch::Tool' },
357
- conditions: [{ field: 'threat_count', operator: 'gt' as ConditionOperator, value: 5 }],
358
- enabled: true,
359
- order: 0,
360
- };
361
-
362
- // Step 2: Convert to Cedar text
363
- const cedarText = ruleToCedar(originalRule);
364
-
365
- // Verify Cedar text contains annotations in proper syntax
366
- expect(cedarText).toContain('@id("rule-001")');
367
- expect(cedarText).toContain('@name("Block high-threat tool calls")');
368
- expect(cedarText).toContain('@description("Forbids tool calls when threat count exceeds threshold")');
369
- expect(cedarText).toContain('@severity("high")');
370
- expect(cedarText).toContain('@tags("security,baseline,v2")');
371
- expect(cedarText).toContain('forbid');
372
- expect(cedarText).toContain('context.threat_count > 5');
373
-
374
- // Step 3: Validate the generated Cedar against Overwatch schema
375
- const validator = new PolicyValidator(OVERWATCH_SCHEMA);
376
- const validationResult = validator.validate(cedarText);
377
- expect(validationResult.valid).toBe(true);
378
-
379
- // Step 4: Parse back to PolicyRule
380
- const parseResult = parseCedarToRules(cedarText);
381
- expect(parseResult.errors).toHaveLength(0);
382
- expect(parseResult.rules).toHaveLength(1);
383
-
384
- const parsedRule = parseResult.rules[0];
385
-
386
- // Step 5: Verify all annotations are preserved
387
- expect(parsedRule.annotations.id).toBe('rule-001');
388
- expect(parsedRule.annotations.name).toBe('Block high-threat tool calls');
389
- expect(parsedRule.annotations.description).toBe(
390
- 'Forbids tool calls when threat count exceeds threshold'
391
- );
392
- expect(parsedRule.annotations.severity).toBe('high');
393
- expect(parsedRule.annotations.tags).toEqual(['security', 'baseline', 'v2']);
394
- });
395
-
396
- /**
397
- * Test 2: Custom Annotations Round-Trip
398
- *
399
- * Verifies that custom user-defined annotations are preserved.
400
- */
401
- it('should preserve custom annotations through Cedar round-trip', () => {
402
- const originalRule: PolicyRule = {
403
- annotations: {
404
- id: 'compliance-rule',
405
- name: 'SOC2 Compliance Policy',
406
- severity: 'critical' as PolicySeverity,
407
- },
408
- customAnnotations: {
409
- compliance: 'SOC2',
410
- ticket: 'SEC-1234',
411
- owner: 'security-team',
412
- review_date: '2024-06-01',
413
- },
414
- effect: 'forbid' as PolicyEffect,
415
- principal: null,
416
- action: 'Overwatch::Action::"call_tool"',
417
- resource: null,
418
- conditions: [],
419
- rawCondition: 'context.contains_secrets == true',
420
- enabled: true,
421
- order: 0,
422
- };
423
-
424
- // Convert to Cedar
425
- const cedarText = ruleToCedar(originalRule);
426
-
427
- // Verify custom annotations in Cedar text (alphabetically ordered)
428
- expect(cedarText).toContain('@compliance("SOC2")');
429
- expect(cedarText).toContain('@owner("security-team")');
430
- expect(cedarText).toContain('@review_date("2024-06-01")');
431
- expect(cedarText).toContain('@ticket("SEC-1234")');
432
-
433
- // Validate Cedar
434
- const validator = new PolicyValidator(OVERWATCH_SCHEMA);
435
- expect(validator.validate(cedarText).valid).toBe(true);
436
-
437
- // Parse back
438
- const parseResult = parseCedarToRules(cedarText);
439
- expect(parseResult.errors).toHaveLength(0);
440
- const parsedRule = parseResult.rules[0];
441
-
442
- // Verify custom annotations preserved
443
- expect(parsedRule.customAnnotations).toBeDefined();
444
- expect(parsedRule.customAnnotations?.compliance).toBe('SOC2');
445
- expect(parsedRule.customAnnotations?.ticket).toBe('SEC-1234');
446
- expect(parsedRule.customAnnotations?.owner).toBe('security-team');
447
- expect(parsedRule.customAnnotations?.review_date).toBe('2024-06-01');
448
- });
449
-
450
- /**
451
- * Test 3: Multiple Rules with rulesToCedar
452
- *
453
- * Verifies that multiple rules are correctly converted and ordered.
454
- */
455
- it('should handle multiple rules with correct ordering', () => {
456
- const rules: PolicyRule[] = [
457
- {
458
- annotations: { id: 'rule-c', name: 'Third Rule' },
459
- effect: 'permit' as PolicyEffect,
460
- principal: null,
461
- action: 'Overwatch::Action::"read_file"',
462
- resource: null,
463
- conditions: [],
464
- enabled: true,
465
- order: 2,
466
- },
467
- {
468
- annotations: { id: 'rule-a', name: 'First Rule', severity: 'critical' as PolicySeverity },
469
- effect: 'forbid' as PolicyEffect,
470
- principal: null,
471
- action: 'Overwatch::Action::"call_tool"',
472
- resource: null,
473
- conditions: [],
474
- rawCondition: 'context.threat_count > 0',
475
- enabled: true,
476
- order: 0,
477
- },
478
- {
479
- annotations: { id: 'rule-b', name: 'Second Rule (Disabled)' },
480
- effect: 'forbid' as PolicyEffect,
481
- principal: null,
482
- action: 'Overwatch::Action::"write_file"',
483
- resource: null,
484
- conditions: [],
485
- enabled: false, // Disabled rule
486
- order: 1,
487
- },
488
- ];
489
-
490
- // Convert to Cedar (enabled only, sorted by order)
491
- const cedarText = rulesToCedar(rules);
492
-
493
- // Verify order (rule-a order=0 should come before rule-c order=2)
494
- const ruleAIndex = cedarText.indexOf('@id("rule-a")');
495
- const ruleCIndex = cedarText.indexOf('@id("rule-c")');
496
- expect(ruleAIndex).toBeLessThan(ruleCIndex);
497
-
498
- // Disabled rule should NOT be included
499
- expect(cedarText).not.toContain('@id("rule-b")');
500
-
501
- // Validate the combined policy text
502
- const validator = new PolicyValidator(OVERWATCH_SCHEMA);
503
- expect(validator.validate(cedarText).valid).toBe(true);
504
-
505
- // Parse back and verify
506
- const parseResult = parseCedarToRules(cedarText);
507
- expect(parseResult.errors).toHaveLength(0);
508
- expect(parseResult.rules).toHaveLength(2); // Only enabled rules
509
-
510
- // Verify parsed rules
511
- const ruleIds = parseResult.rules.map((r: any) => r.annotations.id);
512
- expect(ruleIds).toContain('rule-a');
513
- expect(ruleIds).toContain('rule-c');
514
- expect(ruleIds).not.toContain('rule-b');
515
- });
516
-
517
- /**
518
- * Test 4: Disabled Rules as Comments
519
- *
520
- * Verifies that includeDisabled=true adds disabled rules as comments.
521
- */
522
- it('should include disabled rules as comments when requested', () => {
523
- const rules: PolicyRule[] = [
524
- {
525
- annotations: { id: 'active-rule', name: 'Active Rule' },
526
- effect: 'permit' as PolicyEffect,
527
- principal: null,
528
- action: 'Overwatch::Action::"call_tool"',
529
- resource: null,
530
- conditions: [],
531
- enabled: true,
532
- order: 0,
533
- },
534
- {
535
- annotations: { id: 'disabled-rule', name: 'Disabled Rule' },
536
- effect: 'forbid' as PolicyEffect,
537
- principal: null,
538
- action: 'Overwatch::Action::"call_tool"',
539
- resource: null,
540
- conditions: [],
541
- enabled: false,
542
- order: 1,
543
- },
544
- ];
545
-
546
- // Convert with includeDisabled=true
547
- const cedarText = rulesToCedar(rules, true);
548
-
549
- // Active rule should be normal
550
- expect(cedarText).toContain('@id("active-rule")');
551
- expect(cedarText).not.toMatch(/\/\/ \[DISABLED\].*@id\("active-rule"\)/);
552
-
553
- // Disabled rule should be commented
554
- expect(cedarText).toContain('// [DISABLED] @id("disabled-rule")');
555
- expect(cedarText).toContain('// [DISABLED] @name("Disabled Rule")');
556
- expect(cedarText).toContain('// [DISABLED] forbid');
557
- });
558
-
559
- /**
560
- * Test 5: Annotation Value Escaping
561
- *
562
- * Verifies that special characters in annotation values are properly escaped.
563
- */
564
- it('should properly escape special characters in annotation values', () => {
565
- const rule: PolicyRule = {
566
- annotations: {
567
- id: 'escape-test',
568
- name: 'Rule with "quotes" and \\backslashes',
569
- description: 'Multi-line text works too',
570
- },
571
- effect: 'permit' as PolicyEffect,
572
- principal: null,
573
- action: 'Overwatch::Action::"call_tool"',
574
- resource: null,
575
- conditions: [],
576
- enabled: true,
577
- order: 0,
578
- };
579
-
580
- const cedarText = ruleToCedar(rule);
581
-
582
- // Quotes and backslashes should be escaped
583
- expect(cedarText).toContain('Rule with \\"quotes\\" and \\\\backslashes');
584
-
585
- // Parse back and verify values are unescaped
586
- const parseResult = parseCedarToRules(cedarText);
587
- expect(parseResult.errors).toHaveLength(0);
588
-
589
- const parsedRule = parseResult.rules[0];
590
- expect(parsedRule.annotations.name).toBe('Rule with "quotes" and \\backslashes');
591
- });
592
-
593
- /**
594
- * Test 6: Annotation Key Validation
595
- *
596
- * Verifies that only valid annotation keys are accepted.
597
- */
598
- it('should validate annotation keys correctly', () => {
599
- // Valid custom keys
600
- expect(isValidAnnotationKey('compliance')).toBe(true);
601
- expect(isValidAnnotationKey('ticket_number')).toBe(true);
602
- expect(isValidAnnotationKey('_internal')).toBe(true);
603
- expect(isValidAnnotationKey('myKey123')).toBe(true);
604
-
605
- // Invalid - predefined keys (handled separately)
606
- expect(isValidAnnotationKey('id')).toBe(false);
607
- expect(isValidAnnotationKey('name')).toBe(false);
608
- expect(isValidAnnotationKey('severity')).toBe(false);
609
- expect(isValidAnnotationKey('tags')).toBe(false);
610
-
611
- // Invalid - bad format
612
- expect(isValidAnnotationKey('123abc')).toBe(false); // Starts with number
613
- expect(isValidAnnotationKey('has-dash')).toBe(false); // Contains dash
614
- expect(isValidAnnotationKey('has.dot')).toBe(false); // Contains dot
615
- expect(isValidAnnotationKey('')).toBe(false); // Empty
616
- });
617
-
618
- /**
619
- * Test 7: Generated Rule ID
620
- *
621
- * Verifies that rule IDs are auto-generated when not provided.
622
- */
623
- it('should auto-generate rule IDs when not provided', () => {
624
- const id1 = generateRuleId();
625
- const id2 = generateRuleId();
626
-
627
- // Should be valid UUIDs
628
- const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
629
- expect(id1).toMatch(uuidRegex);
630
- expect(id2).toMatch(uuidRegex);
631
-
632
- // Should be unique
633
- expect(id1).not.toBe(id2);
634
- });
635
-
636
- /**
637
- * Test 8: Palisade Schema Compatibility
638
- *
639
- * Verifies annotations work correctly with Palisade schema.
640
- */
641
- it('should work correctly with Palisade schema', () => {
642
- const rule: PolicyRule = {
643
- annotations: {
644
- id: 'palisade-rule-001',
645
- name: 'Block Critical ML Findings',
646
- description: 'Prevents loading models with critical security findings',
647
- severity: 'critical' as PolicySeverity,
648
- tags: ['ml-security', 'compliance'],
649
- },
650
- customAnnotations: {
651
- framework: 'pytorch',
652
- scan_type: 'static',
653
- },
654
- effect: 'forbid' as PolicyEffect,
655
- principal: { type: 'Palisade::Scanner' },
656
- action: 'Palisade::Action::"load_model"',
657
- resource: { type: 'Palisade::Artifact' },
658
- conditions: [],
659
- rawCondition: 'context.severity == "CRITICAL"',
660
- enabled: true,
661
- order: 0,
662
- };
663
-
664
- const cedarText = ruleToCedar(rule);
665
-
666
- // Validate against Palisade schema
667
- const validator = new PolicyValidator(PALISADE_SCHEMA);
668
- expect(validator.validate(cedarText).valid).toBe(true);
669
-
670
- // Parse and verify
671
- const parseResult = parseCedarToRules(cedarText);
672
- expect(parseResult.errors).toHaveLength(0);
673
-
674
- const parsedRule = parseResult.rules[0];
675
- expect(parsedRule.annotations.id).toBe('palisade-rule-001');
676
- expect(parsedRule.annotations.severity).toBe('critical');
677
- expect(parsedRule.customAnnotations?.framework).toBe('pytorch');
678
- });
679
-
680
- /**
681
- * Test 9: Full Policy Lifecycle with Annotations
682
- *
683
- * Simulates the complete Studio UI workflow:
684
- * 1. Create rules via UI form
685
- * 2. Convert to Cedar for storage/evaluation
686
- * 3. Validate against schema
687
- * 4. Load into engine
688
- * 5. Evaluate requests
689
- * 6. Parse back for editing
690
- */
691
- it('should support full policy lifecycle with annotations', () => {
692
- // Step 1: Create rules via UI form (simulated)
693
- const rules: PolicyRule[] = [
694
- {
695
- annotations: {
696
- id: 'allow-safe-tools',
697
- name: 'Allow Safe Tools',
698
- description: 'Permits tool calls with no detected threats',
699
- severity: 'low' as PolicySeverity,
700
- },
701
- effect: 'permit' as PolicyEffect,
702
- principal: { type: 'Overwatch::User' },
703
- action: 'Overwatch::Action::"call_tool"',
704
- resource: { type: 'Overwatch::Tool' },
705
- conditions: [],
706
- rawCondition: 'context.threat_count == 0',
707
- enabled: true,
708
- order: 0,
709
- },
710
- {
711
- annotations: {
712
- id: 'block-high-threats',
713
- name: 'Block High Threats',
714
- severity: 'critical' as PolicySeverity,
715
- tags: ['security'],
716
- },
717
- effect: 'forbid' as PolicyEffect,
718
- principal: null,
719
- action: 'Overwatch::Action::"call_tool"',
720
- resource: null,
721
- conditions: [{ field: 'threat_count', operator: 'gt' as ConditionOperator, value: 0 }],
722
- enabled: true,
723
- order: 1,
724
- },
725
- ];
726
-
727
- // Step 2: Convert to Cedar for storage
728
- const cedarText = rulesToCedar(rules);
729
-
730
- // Step 3: Validate
731
- const validator = new PolicyValidator(OVERWATCH_SCHEMA);
732
- expect(validator.validate(cedarText).valid).toBe(true);
733
-
734
- // Step 4: Load into engine
735
- const engine = new PolicyEngine({ schema: OVERWATCH_SCHEMA });
736
- engine.loadPolicy(cedarText);
737
-
738
- // Step 5: Evaluate - safe request (0 threats) should be allowed
739
- const entities = [
740
- newEntity('Overwatch::User', 'mcp_client', { user_type: 'external', email: 'test@example.com' }),
741
- newEntity('Overwatch::Tool', 'shell', { tool_name: 'shell', risk_level: 'low' }),
742
- ];
743
-
744
- const safeRequest = engine.evaluate({
745
- principal: newEntityUID('Overwatch::User', 'mcp_client'),
746
- action: 'Overwatch::Action::"call_tool"',
747
- resource: newEntityUID('Overwatch::Tool', 'shell'),
748
- context: {
749
- content: 'ls',
750
- source: 'claudecode',
751
- event: 'PreToolUse',
752
- user_email: 'test@example.com',
753
- tool_name: 'shell',
754
- threat_count: 0,
755
- highest_severity: 'low',
756
- threat_categories: [],
757
-
758
- yara_threats: [],
759
- max_threat_severity: 0,
760
- contains_secrets: false,
761
- mcp_server: '',
762
- mcp_tool: '',
763
- path: '',
764
- cwd: '',
765
- workspace_root: '',
766
- response_content: '',
767
- },
768
- entities,
769
- });
770
- expect(safeRequest.effect).toBe('Allow');
771
-
772
- // Risky request (threats detected) should be denied
773
- const riskyRequest = engine.evaluate({
774
- principal: newEntityUID('Overwatch::User', 'mcp_client'),
775
- action: 'Overwatch::Action::"call_tool"',
776
- resource: newEntityUID('Overwatch::Tool', 'shell'),
777
- context: {
778
- content: 'rm -rf /',
779
- source: 'claudecode',
780
- event: 'PreToolUse',
781
- user_email: 'test@example.com',
782
- tool_name: 'shell',
783
- threat_count: 3,
784
- highest_severity: 'critical',
785
- threat_categories: ['destructive'],
786
-
787
- yara_threats: [],
788
- max_threat_severity: 4,
789
- contains_secrets: false,
790
- mcp_server: '',
791
- mcp_tool: '',
792
- path: '',
793
- cwd: '',
794
- workspace_root: '',
795
- response_content: '',
796
- },
797
- entities,
798
- });
799
- expect(riskyRequest.effect).toBe('Deny');
800
-
801
- // Step 6: Parse back for editing
802
- const parseResult = parseCedarToRules(cedarText);
803
- expect(parseResult.errors).toHaveLength(0);
804
- expect(parseResult.rules).toHaveLength(2);
805
-
806
- // Verify all rules and annotations are preserved for editing
807
- const allowRule = parseResult.rules.find((r: any) => r.annotations.id === 'allow-safe-tools');
808
- const blockRule = parseResult.rules.find((r: any) => r.annotations.id === 'block-high-threats');
809
- expect(allowRule?.annotations.name).toBe('Allow Safe Tools');
810
- expect(blockRule?.annotations.severity).toBe('critical');
811
- expect(blockRule?.annotations.tags).toEqual(['security']);
812
- });
813
- });