Skip to content

Commit d1e7c20

Browse files
committed
fix: Fix possible SQL injection vulnerabilities
1 parent cec71e9 commit d1e7c20

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

backend/apps/db/db.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,15 @@ def get_tables(ds: CoreDatasource):
283283
with pymysql.connect(user=conf.username, passwd=conf.password, host=conf.host,
284284
port=conf.port, db=conf.database, connect_timeout=conf.timeout,
285285
read_timeout=conf.timeout) as conn, conn.cursor() as cursor:
286-
cursor.execute(sql, {"param": sql_param})
286+
cursor.execute(sql, (sql_param,))
287287
res = cursor.fetchall()
288288
res_list = [TableSchema(*item) for item in res]
289289
return res_list
290290
elif ds.type == 'redshift':
291291
with redshift_connector.connect(host=conf.host, port=conf.port, database=conf.database, user=conf.username,
292292
password=conf.password,
293293
timeout=conf.timeout) as conn, conn.cursor() as cursor:
294-
cursor.execute(sql, {"param": sql_param})
294+
cursor.execute(sql, (sql_param,))
295295
res = cursor.fetchall()
296296
res_list = [TableSchema(*item) for item in res]
297297
return res_list
@@ -323,15 +323,15 @@ def get_fields(ds: CoreDatasource, table_name: str = None):
323323
with pymysql.connect(user=conf.username, passwd=conf.password, host=conf.host,
324324
port=conf.port, db=conf.database, connect_timeout=conf.timeout,
325325
read_timeout=conf.timeout) as conn, conn.cursor() as cursor:
326-
cursor.execute(sql, {"param1": p1, "param2": p2})
326+
cursor.execute(sql, (p1, p2))
327327
res = cursor.fetchall()
328328
res_list = [ColumnSchema(*item) for item in res]
329329
return res_list
330330
elif ds.type == 'redshift':
331331
with redshift_connector.connect(host=conf.host, port=conf.port, database=conf.database, user=conf.username,
332332
password=conf.password,
333333
timeout=conf.timeout) as conn, conn.cursor() as cursor:
334-
cursor.execute(sql, {"param1": p1, "param2": p2})
334+
cursor.execute(sql, (p1, p2))
335335
res = cursor.fetchall()
336336
res_list = [ColumnSchema(*item) for item in res]
337337
return res_list

backend/apps/db/db_sql.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def get_version_sql(ds: CoreDatasource, conf: DatasourceConf):
3333

3434

3535
def get_table_sql(ds: CoreDatasource, conf: DatasourceConf, db_version: str = ''):
36-
if ds.type == "mysql" or ds.type == "doris":
36+
if ds.type == "mysql":
3737
return """
3838
SELECT
3939
TABLE_NAME,
@@ -128,8 +128,18 @@ def get_table_sql(ds: CoreDatasource, conf: DatasourceConf, db_version: str = ''
128128
pg_class
129129
WHERE
130130
relkind in ('r','p', 'f')
131-
AND relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = :param)
131+
AND relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = %s)
132132
""", conf.dbSchema
133+
elif ds.type == "doris":
134+
return """
135+
SELECT
136+
TABLE_NAME,
137+
TABLE_COMMENT
138+
FROM
139+
information_schema.TABLES
140+
WHERE
141+
TABLE_SCHEMA = %s
142+
""", conf.database
133143

134144

135145
def get_field_sql(ds: CoreDatasource, conf: DatasourceConf, table_name: str = None):
@@ -164,7 +174,7 @@ def get_field_sql(ds: CoreDatasource, conf: DatasourceConf, table_name: str = No
164174
"""
165175
sql2 = " AND C.TABLE_NAME = :param2" if table_name is not None and table_name != "" else ""
166176
return sql1 + sql2, conf.dbSchema, table_name
167-
elif ds.type == "pg" or ds.type == "excel" or ds.type == "redshift":
177+
elif ds.type == "pg" or ds.type == "excel":
168178
sql1 = """
169179
SELECT a.attname AS COLUMN_NAME,
170180
pg_catalog.format_type(a.atttypid, a.atttypmod) AS DATA_TYPE,
@@ -180,6 +190,22 @@ def get_field_sql(ds: CoreDatasource, conf: DatasourceConf, table_name: str = No
180190
"""
181191
sql2 = " AND c.relname = :param2" if table_name is not None and table_name != "" else ""
182192
return sql1 + sql2, conf.dbSchema, table_name
193+
elif ds.type == "redshift":
194+
sql1 = """
195+
SELECT a.attname AS COLUMN_NAME,
196+
pg_catalog.format_type(a.atttypid, a.atttypmod) AS DATA_TYPE,
197+
col_description(c.oid, a.attnum) AS COLUMN_COMMENT
198+
FROM pg_catalog.pg_attribute a
199+
JOIN
200+
pg_catalog.pg_class c ON a.attrelid = c.oid
201+
JOIN
202+
pg_catalog.pg_namespace n ON n.oid = c.relnamespace
203+
WHERE n.nspname = %s
204+
AND a.attnum > 0
205+
AND NOT a.attisdropped \
206+
"""
207+
sql2 = " AND c.relname = %s" if table_name is not None and table_name != "" else ""
208+
return sql1 + sql2, conf.dbSchema, table_name
183209
elif ds.type == "oracle":
184210
sql1 = """
185211
SELECT
@@ -234,3 +260,16 @@ def get_field_sql(ds: CoreDatasource, conf: DatasourceConf, table_name: str = No
234260
"""
235261
sql2 = " AND c.TABLE_NAME = :param2" if table_name is not None and table_name != "" else ""
236262
return sql1 + sql2, conf.dbSchema, table_name
263+
elif ds.type == "doris":
264+
sql1 = """
265+
SELECT
266+
COLUMN_NAME,
267+
DATA_TYPE,
268+
COLUMN_COMMENT
269+
FROM
270+
INFORMATION_SCHEMA.COLUMNS
271+
WHERE
272+
TABLE_SCHEMA = %s
273+
"""
274+
sql2 = " AND TABLE_NAME = %s" if table_name is not None and table_name != "" else ""
275+
return sql1 + sql2, conf.database, table_name

0 commit comments

Comments
 (0)