Skip to content

Test security

test_sql_safety_guardrail_allows_select()

Verifies that benign SQL queries are permitted.

Scenario: User asks for a simple data extraction (SELECT).

Expectation: The validator returns None (indicating no error/violation).

Source code in api/tests/unit/test_security.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def test_sql_safety_guardrail_allows_select():
    """
    Verifies that benign SQL queries are permitted.

    **Scenario:**
    User asks for a simple data extraction (SELECT).

    **Expectation:**
    The validator returns `None` (indicating no error/violation).
    """
    valid_query = "SELECT * FROM srag_analytics WHERE age > 60"
    result = validate_sql_safety({"sql_query": valid_query})
    assert result is None

test_sql_safety_guardrail_blocks_delete_injection()

Verifies detection of SQL Injection attempts using chained commands.

Scenario: The input starts with a valid SELECT but appends a malicious DELETE command after a semicolon (;).

Expectation: The validator detects the hidden DELETE keyword and blocks the execution.

Source code in api/tests/unit/test_security.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def test_sql_safety_guardrail_blocks_delete_injection():
    """
    Verifies detection of SQL Injection attempts using chained commands.

    **Scenario:**
    The input starts with a valid SELECT but appends a malicious DELETE command
    after a semicolon (`;`).

    **Expectation:**
    The validator detects the hidden `DELETE` keyword and blocks the execution.
    """
    injection_query = "SELECT * FROM srag_analytics; DELETE FROM srag_analytics;"
    result = validate_sql_safety({"sql_query": injection_query})

    assert result is not None
    assert "Security Violation" in result
    assert "DELETE" in result

test_sql_safety_guardrail_blocks_drop()

Verifies that destructive DROP commands are blocked.

Scenario: User (or hallucinating LLM) attempts to delete the main table.

Expectation: The validator returns a string containing "Security Violation".

Source code in api/tests/unit/test_security.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def test_sql_safety_guardrail_blocks_drop():
    """
    Verifies that destructive `DROP` commands are blocked.

    **Scenario:**
    User (or hallucinating LLM) attempts to delete the main table.

    **Expectation:**
    The validator returns a string containing "Security Violation".
    """
    malicious_query = "DROP TABLE srag_analytics"
    result = validate_sql_safety({"sql_query": malicious_query})

    assert result is not None
    assert "Security Violation" in result
    assert "DROP" in result