Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lang2sql/safety/layers/row_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from ...core.ports.safety import SafetyContext, SafetyDecision, Verdict

_LIMIT_RE = re.compile(r"\bLIMIT\s+\d+", re.IGNORECASE)
_LIMIT_RE = re.compile(r"\bLIMIT\s+[+-]?\s*\d+", re.IGNORECASE)
_FETCH_RE = re.compile(
r"\bFETCH\s+(?:FIRST|NEXT)\s+(?:\d+\s+)?ROWS?\s+(?:ONLY|WITH\s+TIES)", re.IGNORECASE
)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,22 @@ def test_case_08_huge_table_gets_limit():
decision = SafetyPipeline().evaluate("SELECT * FROM huge_table", ctx)
assert decision.verdict is Verdict.PASS
assert "LIMIT 1000" in decision.sql.upper()


def test_sqlite_signed_limit_is_not_duplicated():
import sqlite3

for suffix in ("LIMIT -1", "LIMIT +1", "LIMIT - 1", "LIMIT -1 OFFSET 1"):
sql = "SELECT 1 UNION ALL SELECT 2 " + suffix
decision = SafetyPipeline().evaluate(sql, SafetyContext(row_limit=1))
assert decision.verdict is Verdict.PASS
assert decision.sql == sql
with sqlite3.connect(":memory:") as db:
assert db.execute(decision.sql).fetchall() == db.execute(sql).fetchall()


def test_nested_negative_limit_does_not_disable_outer_limit():
decision = SafetyPipeline().evaluate(
"SELECT * FROM (SELECT 1 LIMIT -1)", SafetyContext(row_limit=5)
)
assert decision.sql.endswith("LIMIT 5")