|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { visualTreeToCel, parseCelToVisualTree, createEmptyCondition } from './cel-visual-parser'; |
| 3 | + |
| 4 | +describe('cel-visual-parser', () => { |
| 5 | + describe('CEL string escaping', () => { |
| 6 | + it('should escape double quotes in condition values', () => { |
| 7 | + const condition = { |
| 8 | + ...createEmptyCondition(), |
| 9 | + field: 'email' as const, |
| 10 | + operator: 'contains' as const, |
| 11 | + value: 'test"value', |
| 12 | + }; |
| 13 | + |
| 14 | + const cel = visualTreeToCel(condition); |
| 15 | + // Should escape the quote |
| 16 | + expect(cel).toBe('email.contains("test\\"value")'); |
| 17 | + // Should NOT contain unescaped quote that would break CEL |
| 18 | + expect(cel).not.toMatch(/contains\("test"value"\)/); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should escape backslashes in condition values', () => { |
| 22 | + const condition = { |
| 23 | + ...createEmptyCondition(), |
| 24 | + field: 'email' as const, |
| 25 | + operator: 'contains' as const, |
| 26 | + value: 'test\\value', |
| 27 | + }; |
| 28 | + |
| 29 | + const cel = visualTreeToCel(condition); |
| 30 | + // Should escape the backslash |
| 31 | + expect(cel).toBe('email.contains("test\\\\value")'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should escape both quotes and backslashes together', () => { |
| 35 | + const condition = { |
| 36 | + ...createEmptyCondition(), |
| 37 | + field: 'email' as const, |
| 38 | + operator: 'equals' as const, |
| 39 | + value: 'test\\"value', |
| 40 | + }; |
| 41 | + |
| 42 | + const cel = visualTreeToCel(condition); |
| 43 | + // Backslash escaped first, then quote |
| 44 | + expect(cel).toBe('email == "test\\\\\\"value"'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should prevent CEL injection via malicious value', () => { |
| 48 | + // Attacker tries: test" || true || " |
| 49 | + // Without escaping this becomes: email == "test" || true || "" |
| 50 | + // Which would always be true due to || true |
| 51 | + const condition = { |
| 52 | + ...createEmptyCondition(), |
| 53 | + field: 'email' as const, |
| 54 | + operator: 'equals' as const, |
| 55 | + value: 'test" || true || "', |
| 56 | + }; |
| 57 | + |
| 58 | + const cel = visualTreeToCel(condition); |
| 59 | + // Should escape quotes, preventing injection |
| 60 | + expect(cel).toBe('email == "test\\" || true || \\""'); |
| 61 | + // Should NOT allow the injection pattern |
| 62 | + expect(cel).not.toContain('" || true || "'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should escape values in all operator types', () => { |
| 66 | + const maliciousValue = 'inject"attack'; |
| 67 | + |
| 68 | + // Test equals |
| 69 | + expect(visualTreeToCel({ |
| 70 | + ...createEmptyCondition(), |
| 71 | + field: 'email' as const, |
| 72 | + operator: 'equals' as const, |
| 73 | + value: maliciousValue, |
| 74 | + })).toContain('\\"'); |
| 75 | + |
| 76 | + // Test not_equals |
| 77 | + expect(visualTreeToCel({ |
| 78 | + ...createEmptyCondition(), |
| 79 | + field: 'email' as const, |
| 80 | + operator: 'not_equals' as const, |
| 81 | + value: maliciousValue, |
| 82 | + })).toContain('\\"'); |
| 83 | + |
| 84 | + // Test matches |
| 85 | + expect(visualTreeToCel({ |
| 86 | + ...createEmptyCondition(), |
| 87 | + field: 'email' as const, |
| 88 | + operator: 'matches' as const, |
| 89 | + value: maliciousValue, |
| 90 | + })).toContain('\\"'); |
| 91 | + |
| 92 | + // Test ends_with |
| 93 | + expect(visualTreeToCel({ |
| 94 | + ...createEmptyCondition(), |
| 95 | + field: 'email' as const, |
| 96 | + operator: 'ends_with' as const, |
| 97 | + value: maliciousValue, |
| 98 | + })).toContain('\\"'); |
| 99 | + |
| 100 | + // Test starts_with |
| 101 | + expect(visualTreeToCel({ |
| 102 | + ...createEmptyCondition(), |
| 103 | + field: 'email' as const, |
| 104 | + operator: 'starts_with' as const, |
| 105 | + value: maliciousValue, |
| 106 | + })).toContain('\\"'); |
| 107 | + |
| 108 | + // Test contains |
| 109 | + expect(visualTreeToCel({ |
| 110 | + ...createEmptyCondition(), |
| 111 | + field: 'email' as const, |
| 112 | + operator: 'contains' as const, |
| 113 | + value: maliciousValue, |
| 114 | + })).toContain('\\"'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should escape values in in_list operator', () => { |
| 118 | + const condition = { |
| 119 | + ...createEmptyCondition(), |
| 120 | + field: 'emailDomain' as const, |
| 121 | + operator: 'in_list' as const, |
| 122 | + value: ['safe.com', 'inject"attack.com', 'also\\bad.com'], |
| 123 | + }; |
| 124 | + |
| 125 | + const cel = visualTreeToCel(condition); |
| 126 | + expect(cel).toContain('inject\\"attack.com'); |
| 127 | + expect(cel).toContain('also\\\\bad.com'); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('CEL to visual tree parsing', () => { |
| 132 | + it('should parse simple equality condition', () => { |
| 133 | + const result = parseCelToVisualTree('email == "test@example.com"'); |
| 134 | + expect(result).toBeDefined(); |
| 135 | + if (result?.type === 'condition') { |
| 136 | + expect(result.field).toBe('email'); |
| 137 | + expect(result.operator).toBe('equals'); |
| 138 | + expect(result.value).toBe('test@example.com'); |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + it('should parse endsWith condition', () => { |
| 143 | + const result = parseCelToVisualTree('email.endsWith("@gmail.com")'); |
| 144 | + expect(result).toBeDefined(); |
| 145 | + if (result?.type === 'condition') { |
| 146 | + expect(result.field).toBe('email'); |
| 147 | + expect(result.operator).toBe('ends_with'); |
| 148 | + expect(result.value).toBe('@gmail.com'); |
| 149 | + } |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments