|
7 | 7 | from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine |
8 | 8 |
|
9 | 9 | from agentkit.tools.builtin.sql_readonly import ( |
| 10 | + MAX_CELL_CHARS, |
10 | 11 | execute_sql_query, |
| 12 | + format_rows_as_markdown, |
11 | 13 | sql_readonly, |
12 | 14 | validate_sql_query, |
13 | 15 | ) |
@@ -158,3 +160,67 @@ async def test_sql_tool_registry_integration( |
158 | 160 | bad_result = await registry.execute(bad_call) |
159 | 161 | assert bad_result.ok is False |
160 | 162 | assert "Only SELECT" in (bad_result.error or "") |
| 163 | + |
| 164 | + |
| 165 | +def test_markdown_escapes_pipe_and_backslash_per_cell() -> None: |
| 166 | + """A pipe in a cell value cannot forge additional columns, and rows stay intact.""" |
| 167 | + output = format_rows_as_markdown(["customer"], [("Al|ice",), ("a\\b",), ("Bob",)]) |
| 168 | + |
| 169 | + assert output.splitlines() == [ |
| 170 | + "| customer |", |
| 171 | + "| --- |", |
| 172 | + "| Al\\|ice |", |
| 173 | + "| a\\\\b |", |
| 174 | + "| Bob |", |
| 175 | + ] |
| 176 | + |
| 177 | + |
| 178 | +def test_markdown_escapes_newlines_and_control_characters() -> None: |
| 179 | + """A newline in a cell value cannot forge additional rows.""" |
| 180 | + output = format_rows_as_markdown(["text"], [("line1\nline2\r\nline3\x07end\x7f",)]) |
| 181 | + |
| 182 | + assert output.splitlines() == [ |
| 183 | + "| text |", |
| 184 | + "| --- |", |
| 185 | + "| line1\\nline2\\r\\nline3\\u0007end\\u007f |", |
| 186 | + ] |
| 187 | + |
| 188 | + |
| 189 | +def test_markdown_null_and_unchanged_basics() -> None: |
| 190 | + output = format_rows_as_markdown(["a", "b"], [(None, 150.0)]) |
| 191 | + |
| 192 | + assert output.splitlines() == ["| a | b |", "| --- | --- |", "| NULL | 150.0 |"] |
| 193 | + |
| 194 | + |
| 195 | +def test_markdown_truncates_oversized_cell() -> None: |
| 196 | + output = format_rows_as_markdown(["c"], [("x" * 5_000,)]) |
| 197 | + lines = output.splitlines() |
| 198 | + |
| 199 | + # One data row, one bounded cell, marker appended inside the cell. |
| 200 | + assert len(lines) == 3 |
| 201 | + assert lines[-1].endswith("[Truncated] |") |
| 202 | + assert len(lines[-1]) <= MAX_CELL_CHARS + len("[Truncated]") + 4 |
| 203 | + |
| 204 | + |
| 205 | +def test_markdown_escapes_column_names_too() -> None: |
| 206 | + output = format_rows_as_markdown(["name|extra"], [("v",)]) |
| 207 | + |
| 208 | + assert output.splitlines()[0] == "| name\\|extra |" |
| 209 | + |
| 210 | + |
| 211 | +@pytest.mark.asyncio |
| 212 | +async def test_execute_sql_query_escapes_adversarial_cells(demo_db_engine: AsyncEngine) -> None: |
| 213 | + async with demo_db_engine.begin() as conn: |
| 214 | + await conn.execute( |
| 215 | + text("INSERT INTO orders (customer, amount) VALUES ('Al|ice\nEvil', 7.5)") |
| 216 | + ) |
| 217 | + |
| 218 | + output = await execute_sql_query( |
| 219 | + "SELECT customer, amount FROM orders ORDER BY id", engine=demo_db_engine |
| 220 | + ) |
| 221 | + lines = [line for line in output.strip().split("\n") if line.strip()] |
| 222 | + |
| 223 | + # Header + divider + 4 data rows: the injected pipe and newline are escapes within |
| 224 | + # one cell, not structure that adds a line or column. |
| 225 | + assert len(lines) == 6 |
| 226 | + assert "Al\\|ice\\nEvil" in output |
0 commit comments