sqlshield 0.0.1__py3-none-any.whl → 0.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of sqlshield might be problematic. Click here for more details.

sqlshield/shield.py CHANGED
@@ -1,26 +1,39 @@
1
1
  from sqlglot import parse_one, exp
2
- from pprint import pprint
3
2
 
4
3
  class Session():
5
4
  def __init__(self, db, params):
6
5
  self.database = db
7
6
  self.params = params
8
-
7
+ def is_type(tree, _type):
8
+ if len(list(tree.find_all(_type))) > 0:
9
+ return True
10
+ return False
9
11
  def generateNativeSQL(self, aSql):
12
+
13
+ tree = parse_one(aSql)
14
+ if type(tree) != exp.Select:
15
+ raise Exception('As of now, only select statements are supported.', aSql)
16
+ blocked = [exp.DML, exp.DDL, exp.Command, exp.Commit, exp.Drop, exp.TruncateTable]
17
+ for _type in blocked:
18
+ if Session.is_type(tree, _type):
19
+ raise Exception('The SQL contains either DML or DDL', aSql)
10
20
  # prepare_table_map
11
21
  tbl_map = {}
12
22
  for tbl in self.database.tables:
13
- tbl_map[tbl.pub_name] = tbl
23
+ tbl_map[tbl.pub_name.lower()] = tbl
14
24
 
15
25
  print("Input: ", aSql)
16
26
  def transformer(node):
17
27
  if isinstance(node, exp.Table):
18
- tbl_name = node.this.this
28
+ tbl_name_c = node.this.this
29
+ tbl_name = tbl_name_c.lower()
30
+ if tbl_name not in tbl_map:
31
+ raise Exception('No such table found.', tbl_name_c)
19
32
  mtable = tbl_map[tbl_name]
20
33
  msql = mtable.get_alias_sql(self.params)
21
34
  return parse_one(msql)
22
35
  return node
23
- tree = parse_one(aSql)
36
+
24
37
  new_tree = tree.transform(transformer)
25
38
  return new_tree.sql()
26
39
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sqlshield
3
- Version: 0.0.1
3
+ Version: 0.0.2
4
4
  Summary: A Shield for your LLM generated SQL Queries. It provides an application level control for securing the database from SQL generated by LLM.
5
5
  Author-email: Sandeep Giri <sandeepgiri@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/terno-ai/llm-sql-shield
@@ -13,40 +13,42 @@ Description-Content-Type: text/markdown
13
13
  License-File: LICENSE
14
14
 
15
15
  # LLM SQL Shield
16
+ A Shield for your database to neutralize the SQL queries generated by Large Language Model (LLM)
16
17
 
17
- `llm-sql-shield` is A Shield for your LLM generated SQL Queries.
18
+ ## Background
18
19
 
19
- # Background
20
- Large Language Models (LLM) prove to be extraordinally good when it comes to SQL query generation. This has lead to a revolution in generating SQL using large language models.
20
+ Large Language Models (LLM) have proven to be extraordinarily effective in generating SQL queries. This has revolutionized SQL generation using large language models.
21
21
 
22
- But there is a huge challenge, there is threat to database. The queries generted by LLMs can access the data that they aren't supposed to. No matter how you constraint the prompt is, it is always possible to jailbreak. Given the nature of LLMs, I don't think they can ever be controlled deterministically.
22
+ However, there is a significant challenge: a threat to the database. The queries generated by LLMs can access data that they aren't supposed to. No matter how constrained the prompt is, it is always possible to jailbreak. Given the nature of LLMs, they can never be controlled deterministically.
23
23
 
24
- We can secure our database but it is too dificult to achieve it:
24
+ Securing the database is difficult because:
25
25
 
26
- 1. Databases are usually controlled and management by a different team. Therefore, any changes to the databases would take time.
27
- 2. Databases are usually accessed and modified by multiple different services. Therefore, making any change to database is dificult.
28
- 3. Database security is designed for handful of roles and users. Achieving the security for thousand of users is dificult.
29
- 4. Row based security can be achieved by predefined views but if we have to have dynamic view or parameterised views, it is not possible in databases as of now.
30
- 5. LLMs require the table names and column names to be self explainatory. To achieve the same, in database, you will have to clone the database and make modifications to it but then making the data sync in these tables becomes dificult task.
26
+ 1. Databases are usually controlled and managed by a different team. Therefore, any changes to the databases would take time.
27
+ 2. Databases are usually accessed and modified by multiple different services. Therefore, making any change to the database is difficult.
28
+ 3. Database security is designed for a handful of roles and users. Achieving security for thousands of users is difficult.
29
+ 4. Row-based security can be achieved by predefined views, but if we have to have dynamic views or parameterized views, it is not possible in databases as of now.
30
+ 5. LLMs require the table names and column names to be self-explanatory. To achieve the same in a database, you will have to clone the database and make modifications to it, but then making the data sync in these tables becomes a difficult task.
31
+
32
+
33
+ ## LLM SQL Shield - Features
31
34
 
32
- # LLM SQL Shield - Features
33
35
  With SQL Shield, you can:
34
36
 
35
- 1. Limit the tables that you expose to LLM.
36
- 2. You can also rename the tables as per your wish by setting proper `pub_name`
37
- 3. You can limit the columns from each table.
38
- 4. You can rename the columns to make them more meaningful by setting proper `pub_name`
39
- 5. You can limit the rows that can be accessed by providing `filters`.
37
+ 1. Limit the tables that you expose to LLM.
38
+ 2. Rename the tables as per your wish by setting proper `pub_name`.
39
+ 3. Limit the columns from each table.
40
+ 4. Rename the columns to make them more meaningful by setting proper `pub_name`.
41
+ 5. Limit the rows that can be accessed by providing `filters`.
40
42
  6. The `filters` can have variables that you can fill in at the time of query generation and execution.
41
43
 
42
- # Install
44
+ ## Install
43
45
  ```
44
46
  pip install sqlshield
45
47
  pip install openai
46
48
  pip install sqlalchemy
47
49
 
48
50
  ```
49
- # Example
51
+ ## Example - Tutorial
50
52
 
51
53
  Here is an example
52
54
 
@@ -63,7 +65,7 @@ import os
63
65
  # TODO: Specify correct OpenAI key
64
66
  os.environ["OPENAI_API_KEY"] = 'sk-XXXXXXXXX'
65
67
 
66
- # TODO: You can download this SQLite3 DB file: https://github.com/terno-ai/llm-sql-shield/raw/main/doc/db/chinook.db
68
+ # TODO: You can download this SQLite3 DB file: https://github.com/terno-ai/llm-sql-shield/raw/main/tests/chinook.db
67
69
  # And save it in your current directory
68
70
  # Connect to DB
69
71
  engine = sqlalchemy.create_engine('sqlite:///chinook.db')
@@ -0,0 +1,8 @@
1
+ sqlshield/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ sqlshield/models.py,sha256=6iL-J_zAmjSB-sotyYSPf7Cw-CKRgDdnu38XOnFyGQs,4012
3
+ sqlshield/shield.py,sha256=Npj6xyQTXQTSu6VBDEx-S0UWNGnCI_WT6fTY-1Ukihs,1436
4
+ sqlshield-0.0.2.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
5
+ sqlshield-0.0.2.dist-info/METADATA,sha256=rGD_mQki-NzuupfjpvPVySpTbs4aG8bogswnYP23iTE,4473
6
+ sqlshield-0.0.2.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
7
+ sqlshield-0.0.2.dist-info/top_level.txt,sha256=sMIFlsz4TsFN2Y4jGG_bNkgSeRopF0t0iBSjbFfgIMs,10
8
+ sqlshield-0.0.2.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- sqlshield/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- sqlshield/models.py,sha256=6iL-J_zAmjSB-sotyYSPf7Cw-CKRgDdnu38XOnFyGQs,4012
3
- sqlshield/shield.py,sha256=3JmkjFmBW3TadcumDnbt86OyP83hUzQuMd-dDwWxZo0,777
4
- sqlshield-0.0.1.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
5
- sqlshield-0.0.1.dist-info/METADATA,sha256=FIMQ36Axe-FOsJLfGo497N9n59MmYY9lbwbXHbgzqXk,4493
6
- sqlshield-0.0.1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
7
- sqlshield-0.0.1.dist-info/top_level.txt,sha256=sMIFlsz4TsFN2Y4jGG_bNkgSeRopF0t0iBSjbFfgIMs,10
8
- sqlshield-0.0.1.dist-info/RECORD,,