Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Reject a semicolon in a SQL query instead of silently cutting the statement (#1388)

PerryLink55fadb4c568e
diff --git a/src/python/txtai/database/sql/base.py b/src/python/txtai/database/sql/base.py
index 70186539..6a3f30ed 100644
--- a/src/python/txtai/database/sql/base.py
+++ b/src/python/txtai/database/sql/base.py
@@ -5,6 +5,7 @@ SQL module
from io import StringIO
from shlex import shlex
+from .error import SQLError
from .expression import Expression
@@ -42,8 +43,11 @@ class SQL:
clauses = None
if self.issql(query):
- # Ignore multiple statements
- query = query.split(";")[0]
+ # Multiple statements are not supported
+ components = [x for x in query.split(";") if x.strip()]
+ if len(components) > 1:
+ raise SQLError("Multiple SQL statements are not supported. Pass values that contain a semicolon as a bind parameter.")
+ query = components[0]
# Tokenize query
tokens, positions = self.tokenize(query)
diff --git a/test/python/testdatabase/testsql.py b/test/python/testdatabase/testsql.py
index f9de9163..73bac065 100644
--- a/test/python/testdatabase/testsql.py
+++ b/test/python/testdatabase/testsql.py
@@ -143,6 +143,17 @@ class TestSQL(unittest.TestCase):
with self.assertRaises(SQLError):
self.db.search("select a b c from txtai where id match id")
+ def testMultipleStatements(self):
+ """
+ Test only the first statement is run
+ """
+
+ with self.assertRaises(SQLError):
+ self.db.search("select * from txtai ; delete from sections")
+
+ self.assertSql("select", ";select id from txtai", "s.id")
+ self.assertSql("select", "select id from txtai;", "s.id")
+
def testBracket(self):
"""
Test bracket expressions