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60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
"""
SQL Injection Attack Tests

Tests SQL injection vulnerability prevention in AgentGovernanceService.
Verifies that parameterized queries are used and malicious inputs are escaped.

OWASP Category: A03:2021 - Injection
CWE: CWE-89 (SQL Injection)
"""

import pytest
from sqlalchemy.exc import IntegrityError, ProgrammingError
from sqlalchemy.orm import Session

from core.agent_governance_service import AgentGovernanceService
from core.models import AgentRegistry, AgentStatus


# ============================================================================
# Agent ID SQL Injection Tests
# ============================================================================


@pytest.mark.sql_injection
@pytest.mark.parametrize("malicious_id", [
    "'; DROP TABLE agents; --",
    "' OR '1'='1",
    "1' UNION SELECT * FROM users --",
    "'; INSERT INTO agents VALUES ('hacked', 'admin'); --",
    "1'; DELETE FROM episodes WHERE '1'='1' --",
    "' OR '1'='1' --",
    "admin'--",
    "admin'/*",
])
def test_agent_id_sql_injection_blocked(malicious_id, db_session: Session):
    """
    SECURITY: SQL injection via agent_id parameter.

    ATTACK: Attempt to inject SQL via agent_id string in can_perform_action().
    EXPECTED: Input escaped or parameterized, query returns "agent not found",
              no SQL injection executed.
    """
    service = AgentGovernanceService(db_session)

    result = service.can_perform_action(
        agent_id=malicious_id,
        action_type="stream_chat"
    )

    # Should not execute injected SQL
    assert result["allowed"] is False
    assert "not found" in result["reason"].lower()


@pytest.mark.sql_injection
def test_agent_id_union_query_blocked(db_session: Session):
    """
    SECURITY: UNION SELECT injection via agent_id.

    ATTACK: Attempt to extract data from other tables using UNION SELECT.
    EXPECTED: Query fails, "agent not found" error.
    """
    service = AgentGovernanceService(db_session)

    malicious_id = "1' UNION SELECT * FROM users --"
    result = service.can_perform_action(
        agent_id=malicious_id,
        action_type="stream_chat"
    )

    # Should not execute UNION
    assert result["allowed"] is False
    assert "not found" in result["reason"].lower()


@pytest.mark.sql_injection
def test_agent_id_drop_table_blocked(db_session: Session):
    """
    SECURITY: DROP TABLE injection via agent_id.

    ATTACK: Attempt to DROP agents table.
    EXPECTED: DROP TABLE not executed, agent not found error.
    """
    service = AgentGovernanceService(db_session)

    malicious_id = "'; DROP TABLE agents; --"
    result = service.can_perform_action(
        agent_id=malicious_id,
        action_type="stream_chat"
    )

    # Verify agent_registry table still exists by querying it
    agents = db_session.query(AgentRegistry).all()
    assert agents is not None  # Table exists

    # Should not have dropped table
    assert result["allowed"] is False
    assert "not found" in result["reason"].lower()


@pytest.mark.sql_injection
def test_agent_id_insert_blocked(db_session: Session):
    """
    SECURITY: INSERT injection via agent_id.

    ATTACK: Attempt to INSERT malicious agent record.
    EXPECTED: INSERT not executed, agent not found error.
    """
    service = AgentGovernanceService(db_session)

    initial_count = db_session.query(AgentRegistry).count()

    malicious_id = "'; INSERT INTO agents VALUES ('hacked', 'admin', 'AUTONOMOUS'); --"
    result = service.can_perform_action(
        agent_id=malicious_id,
        action_type="stream_chat"
    )

    # Verify no new agent was inserted
    final_count = db_session.query(AgentRegistry).count()
    assert initial_count == final_count

    assert result["allowed"] is False


# ============================================================================
# User Input SQL Injection Tests
# ============================================================================


@pytest.mark.sql_injection
@pytest.mark.parametrize("malicious_name", [
    "'; DROP TABLE users; --",
    "' OR '1'='1",
    "<script>alert('xss')</script>",
    "$(whoami)",
    "`cat /etc/passwd`",
])
def test_agent_name_sql_injection_blocked(malicious_name, db_session: Session):
    """
    SECURITY: SQL injection via agent name parameter.

    ATTACK: Attempt to inject SQL via name field in register_or_update_agent().
    EXPECTED: Input escaped or parameterized, agent created safely,
              malicious name stored as string not executed.
    """
    service = AgentGovernanceService(db_session)

    agent = service.register_or_update_agent(
        name=malicious_name,
        category="test",
        module_path="test.module",
        class_name="TestAgent"
    )

    # Agent should be created (input escaped)
    assert agent is not None
    # Malicious content should be stored as-is (not executed)
    assert agent.name == malicious_name


@pytest.mark.sql_injection
def test_description_field_sql_injection_blocked(db_session: Session):
    """
    SECURITY: SQL injection via description field.

    ATTACK: Attempt to inject SQL via description parameter.
    EXPECTED: Input sanitized, description stored as string.
    """
    service = AgentGovernanceService(db_session)

    malicious_desc = "'; DROP TABLE agents; --"

    agent = service.register_or_update_agent(
        name="Test Agent",
        category="test",
        module_path="test.module",
        class_name="TestAgent",
        description=malicious_desc
    )

    assert agent is not None
    assert agent.description == malicious_desc

    # Verify agent_registry table still exists
    agents = db_session.query(AgentRegistry).all()
    assert agents is not None


@pytest.mark.sql_injection
def test_module_path_sql_injection_blocked(db_session: Session):
    """
    SECURITY: SQL injection via module_path parameter.

    ATTACK: Attempt to inject SQL via module_path.
    EXPECTED: Input validated, module_path stored safely.
    """
    service = AgentGovernanceService(db_session)

    malicious_module = "'; DROP TABLE agents; --"

    # May raise validation error or store safely
    try:
        agent = service.register_or_update_agent(
            name="Test Agent",
            category="test",
            module_path=malicious_module,
            class_name="TestAgent"
        )
        assert agent is not None
        # If created, should be stored as string
        assert agent.module_path == malicious_module
    except (ValueError, AttributeError):
        # Or validation may reject it
        pass


# ============================================================================
# Database Query SQL Injection Tests
# ============================================================================


@pytest.mark.sql_injection
def test_filter_query_sql_injection_blocked(db_session: Session):
    """
    SECURITY: SQL injection in filter() clause.

    ATTACK: Attempt to inject SQL in db.query().filter() with malicious agent_id.
    EXPECTED: SQLAlchemy uses parameterized queries, injection blocked.
    """
    malicious_id = "1' OR '1'='1"

    # Query with malicious agent_id
    agent = db_session.query(AgentRegistry).filter(
        AgentRegistry.id == malicious_id
    ).first()

    # Should return None (no such agent)
    assert agent is None

    # Verify no SQL error leaked
    # (SQLAlchemy parameterizes queries, so this is safe)


@pytest.mark.sql_injection
def test_order_by_sql_injection_blocked(db_session: Session):
    """
    SECURITY: SQL injection in ORDER BY clause.

    ATTACK: Attempt to inject SQL in ORDER BY.
    EXPECTED: Injection blocked, query returns empty or error handled.
    """
    from sqlalchemy import text
    from sqlalchemy.exc import CompileError

    # Try to inject in ORDER BY
    malicious_order = "id; DROP TABLE agents; --"

    try:
        # SQLAlchemy should reject this (raises CompileError)
        agents = db_session.query(AgentRegistry).order_by(malicious_order).all()
        # If it somehow executes, verify table still exists
        assert agents is not None
    except (CompileError, ProgrammingError, AttributeError):
        # SQLAlchemy correctly rejects malicious input (expected behavior)
        pass

    # Verify agent_registry table still exists
    agents = db_session.query(AgentRegistry).all()
    assert agents is not None


@pytest.mark.sql_injection
def test_raw_query_sql_injection_blocked(db_session: Session):
    """
    SECURITY: SQL injection in raw SQL text() queries.

    ATTACK: Attempt to inject SQL using text() with malicious input.
    EXPECTED: Parameterization used, injection blocked.
    """
    from sqlalchemy import text

    malicious_id = "'; DROP TABLE agents; --"

    # Parameterized query (safe)
    result = db_session.execute(
        text("SELECT * FROM agent_registry WHERE id = :agent_id"),
        {"agent_id": malicious_id}
    ).fetchone()

    # Should return None (no such agent)
    assert result is None

    # Verify agent_registry table still exists
    agents = db_session.query(AgentRegistry).all()
    assert agents is not None


# ============================================================================
# Verification Tests
# ============================================================================


@pytest.mark.sql_injection
def test_parameterized_queries_used(db_session: Session):
    """
    SECURITY: Verify SQLAlchemy uses parameterized queries.

    CHECK: Ensure ORM methods use parameterized queries, not string concatenation.
    EXPECTED: SQLAlchemy automatically parameterizes, preventing injection.
    """
    service = AgentGovernanceService(db_session)

    # Create an agent
    agent = service.register_or_update_agent(
        name="Test Agent",
        category="test",
        module_path="test.module",
        class_name="TestAgent"
    )

    # Query using ORM (parameterized by default)
    queried = db_session.query(AgentRegistry).filter(
        AgentRegistry.id == agent.id
    ).first()

    assert queried is not None
    assert queried.id == agent.id


@pytest.mark.sql_injection
@pytest.mark.parametrize("special_char", [
    "'",
    '"',
    ";",
    "--",
    "/*",
    "*/",
    "\\",
    "\0",
])
def test_special_characters_escaped(special_char, db_session: Session):
    """
    SECURITY: Special characters properly escaped.

    CHECK: Quotes, backslashes, semicolons, comments don't break queries.
    EXPECTED: Special characters escaped or parameterized.
    """
    service = AgentGovernanceService(db_session)

    malicious_name = f"test{special_char}name"

    agent = service.register_or_update_agent(
        name=malicious_name,
        category="test",
        module_path="test.module",
        class_name="TestAgent"
    )

    assert agent is not None
    assert agent.name == malicious_name


@pytest.mark.sql_injection
def test_sql_error_not_exposed_to_user(db_session: Session):
    """
    SECURITY: SQL error messages don't leak database schema.

    CHECK: SQL errors don't reveal table structure, column names, etc.
    EXPECTED: Generic error message, no schema details.
    """
    service = AgentGovernanceService(db_session)

    # Try non-existent agent
    result = service.can_perform_action(
        agent_id="nonexistent-agent-123",
        action_type="stream_chat"
    )

    # Should return generic "not found" error
    assert result["allowed"] is False
    assert "not found" in result["reason"].lower()

    # Should NOT contain SQL details
    error_msg = str(result).lower()
    assert "syntax error" not in error_msg
    assert "table" not in error_msg or "not found" in result["reason"].lower()
    assert "column" not in error_msg
    assert "database" not in error_msg


# ============================================================================
# Batch SQL Injection Tests
# ============================================================================


@pytest.mark.sql_injection
@pytest.mark.parametrize("malicious_id", [
    "'; DROP TABLE agents; --",
    "' OR '1'='1",
    "1' UNION SELECT * FROM users --",
    "'; INSERT INTO agents VALUES ('hacked', 'admin'); --",
    "1'; DELETE FROM episodes WHERE '1'='1' --",
])
def test_batch_agent_id_sql_injection(malicious_id, db_session: Session):
    """
    SECURITY: Batch test SQL injection via agent_id.

    Tests all SQL injection payloads from conftest.
    """
    service = AgentGovernanceService(db_session)

    result = service.can_perform_action(
        agent_id=malicious_id,
        action_type="stream_chat"
    )

    assert result["allowed"] is False


@pytest.mark.sql_injection
@pytest.mark.parametrize("malicious_name", [
    "'; DROP TABLE users; --",
    "' OR '1'='1",
    "<script>alert('xss')</script>",
    "$(whoami)",
    "`cat /etc/passwd`",
])
def test_batch_agent_name_sql_injection(malicious_name, db_session: Session):
    """
    SECURITY: Batch test SQL injection via agent name.

    Tests all SQL injection payloads from conftest.
    """
    service = AgentGovernanceService(db_session)

    agent = service.register_or_update_agent(
        name=malicious_name,
        category="test",
        module_path="test.module",
        class_name="TestAgent"
    )

    assert agent is not None
    assert agent.name == malicious_name