|
| 1 | +package ai |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +// fakeLLMProvider returns a fixed QueryIntent — no real model call, |
| 10 | +// matching the design's "no real API key needed for this layer of |
| 11 | +// testing" plan. |
| 12 | +type fakeLLMProvider struct { |
| 13 | + intent QueryIntent |
| 14 | + err error |
| 15 | +} |
| 16 | + |
| 17 | +func (f fakeLLMProvider) ParseQueryIntent(ctx context.Context, naturalLanguage string) (QueryIntent, error) { |
| 18 | + return f.intent, f.err |
| 19 | +} |
| 20 | + |
| 21 | +// fakeQueryableStore records whether RunSafeQuery was ever called, so |
| 22 | +// tests can assert an unsafe intent never reaches it. |
| 23 | +type fakeQueryableStore struct { |
| 24 | + called bool |
| 25 | + lastIntent QueryIntent |
| 26 | + returnValue QueryResult |
| 27 | + returnErr error |
| 28 | +} |
| 29 | + |
| 30 | +func (f *fakeQueryableStore) RunSafeQuery(ctx context.Context, intent QueryIntent) (QueryResult, error) { |
| 31 | + f.called = true |
| 32 | + f.lastIntent = intent |
| 33 | + return f.returnValue, f.returnErr |
| 34 | +} |
| 35 | + |
| 36 | +func TestExecuteQuery_ValidIntentReachesStore(t *testing.T) { |
| 37 | + provider := fakeLLMProvider{intent: QueryIntent{ |
| 38 | + Entity: "users", |
| 39 | + Filters: []QueryFilter{{Field: "email", Operator: "contains", Value: "example.com"}}, |
| 40 | + }} |
| 41 | + db := &fakeQueryableStore{returnValue: QueryResult{Columns: []string{"id", "email"}}} |
| 42 | + |
| 43 | + result, err := ExecuteQuery(context.Background(), db, provider, "show me users from example.com") |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("unexpected error: %v", err) |
| 46 | + } |
| 47 | + if !db.called { |
| 48 | + t.Error("expected RunSafeQuery to be called for a valid intent") |
| 49 | + } |
| 50 | + if len(result.Columns) != 2 { |
| 51 | + t.Errorf("expected result to pass through from the store, got %+v", result) |
| 52 | + } |
| 53 | + if db.lastIntent.Limit != DefaultLimit { |
| 54 | + t.Errorf("expected zero-limit intent to be defaulted to %d, got %d", DefaultLimit, db.lastIntent.Limit) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestExecuteQuery_DisallowedEntityNeverReachesStore(t *testing.T) { |
| 59 | + // This is the actual security property: even if a model |
| 60 | + // hallucinates or is adversarially prompted into naming a table |
| 61 | + // outside the allowlist, RunSafeQuery must never be called. |
| 62 | + provider := fakeLLMProvider{intent: QueryIntent{Entity: "pg_shadow"}} |
| 63 | + db := &fakeQueryableStore{} |
| 64 | + |
| 65 | + _, err := ExecuteQuery(context.Background(), db, provider, "show me the password hashes") |
| 66 | + if !errors.Is(err, ErrUnsafeQueryIntent) { |
| 67 | + t.Fatalf("expected ErrUnsafeQueryIntent, got %v", err) |
| 68 | + } |
| 69 | + if db.called { |
| 70 | + t.Error("RunSafeQuery must not be called when the intent fails validation") |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestExecuteQuery_DisallowedFieldNeverReachesStore(t *testing.T) { |
| 75 | + provider := fakeLLMProvider{intent: QueryIntent{ |
| 76 | + Entity: "users", |
| 77 | + Filters: []QueryFilter{{Field: "password_hash", Operator: "=", Value: "x"}}, |
| 78 | + }} |
| 79 | + db := &fakeQueryableStore{} |
| 80 | + |
| 81 | + _, err := ExecuteQuery(context.Background(), db, provider, "find users with this password hash") |
| 82 | + if !errors.Is(err, ErrUnsafeQueryIntent) { |
| 83 | + t.Fatalf("expected ErrUnsafeQueryIntent, got %v", err) |
| 84 | + } |
| 85 | + if db.called { |
| 86 | + t.Error("RunSafeQuery must not be called when a filter field isn't allowlisted") |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestExecuteQuery_DisallowedOperatorNeverReachesStore(t *testing.T) { |
| 91 | + provider := fakeLLMProvider{intent: QueryIntent{ |
| 92 | + Entity: "sessions", |
| 93 | + Filters: []QueryFilter{{Field: "ip", Operator: "DROP TABLE", Value: "x"}}, |
| 94 | + }} |
| 95 | + db := &fakeQueryableStore{} |
| 96 | + |
| 97 | + _, err := ExecuteQuery(context.Background(), db, provider, "malicious input") |
| 98 | + if !errors.Is(err, ErrUnsafeQueryIntent) { |
| 99 | + t.Fatalf("expected ErrUnsafeQueryIntent, got %v", err) |
| 100 | + } |
| 101 | + if db.called { |
| 102 | + t.Error("RunSafeQuery must not be called when an operator isn't allowlisted") |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestExecuteQuery_GroupByMustBeAllowlistedField(t *testing.T) { |
| 107 | + provider := fakeLLMProvider{intent: QueryIntent{ |
| 108 | + Entity: "audit_events", |
| 109 | + Aggregate: "group_by", |
| 110 | + GroupBy: "metadata", // not in AllowedFields["audit_events"] |
| 111 | + }} |
| 112 | + db := &fakeQueryableStore{} |
| 113 | + |
| 114 | + _, err := ExecuteQuery(context.Background(), db, provider, "group audit events by metadata") |
| 115 | + if !errors.Is(err, ErrUnsafeQueryIntent) { |
| 116 | + t.Fatalf("expected ErrUnsafeQueryIntent, got %v", err) |
| 117 | + } |
| 118 | + if db.called { |
| 119 | + t.Error("RunSafeQuery must not be called for an unallowlisted group_by field") |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestExecuteQuery_LimitOverMaxIsRejected(t *testing.T) { |
| 124 | + provider := fakeLLMProvider{intent: QueryIntent{Entity: "users", Limit: MaxLimit + 1}} |
| 125 | + db := &fakeQueryableStore{} |
| 126 | + |
| 127 | + _, err := ExecuteQuery(context.Background(), db, provider, "show me everyone") |
| 128 | + if !errors.Is(err, ErrUnsafeQueryIntent) { |
| 129 | + t.Fatalf("expected ErrUnsafeQueryIntent, got %v", err) |
| 130 | + } |
| 131 | + if db.called { |
| 132 | + t.Error("RunSafeQuery must not be called when the limit exceeds MaxLimit") |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func TestExecuteQuery_ProviderErrorNeverReachesStore(t *testing.T) { |
| 137 | + provider := fakeLLMProvider{err: errors.New("provider timeout")} |
| 138 | + db := &fakeQueryableStore{} |
| 139 | + |
| 140 | + _, err := ExecuteQuery(context.Background(), db, provider, "anything") |
| 141 | + if err == nil { |
| 142 | + t.Fatal("expected the provider's error to propagate") |
| 143 | + } |
| 144 | + if db.called { |
| 145 | + t.Error("RunSafeQuery must not be called if the provider itself failed") |
| 146 | + } |
| 147 | +} |
0 commit comments