Skip to content

Commit 273171b

Browse files
committed
fix: Kingbase SQL inject
1 parent fe142f9 commit 273171b

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

backend/apps/db/db.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ def get_tables(ds: CoreDatasource):
534534
return res_list
535535
elif equals_ignore_case(ds.type, 'kingbase'):
536536
with get_driver_connection(ds) as conn, conn.cursor() as cursor:
537-
cursor.execute(sql.format(sql_param))
537+
cursor.execute(sql, (sql_param,))
538538
res = cursor.fetchall()
539539
res_list = [TableSchema(*item) for item in res]
540540
return res_list
@@ -584,7 +584,8 @@ def get_fields(ds: CoreDatasource, table_name: str = None):
584584
return res_list
585585
elif equals_ignore_case(ds.type, 'kingbase'):
586586
with get_driver_pool(ds).connection() as conn, conn.cursor() as cursor:
587-
cursor.execute(sql.format(p1, p2))
587+
# cursor.execute(sql.format(p1, p2))
588+
cursor.execute(sql, (p1, p2))
588589
res = cursor.fetchall()
589590
res_list = [ColumnSchema(*item) for item in res]
590591
return res_list

backend/apps/db/db_sql.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def get_table_sql(ds: CoreDatasource, conf: DatasourceConf, db_version: str = ''
7373
AND c.relkind IN ('r', 'v', 'p', 'm')
7474
AND c.relname NOT LIKE 'pg_%'
7575
AND c.relname NOT LIKE 'sql_%'
76-
ORDER BY c.relname \
76+
ORDER BY c.relname
7777
""", conf.dbSchema
7878
elif equals_ignore_case(ds.type, "oracle"):
7979
return """
@@ -154,11 +154,11 @@ def get_table_sql(ds: CoreDatasource, conf: DatasourceConf, db_version: str = ''
154154
pg_namespace n ON n.oid = c.relnamespace
155155
LEFT JOIN
156156
pg_description d ON d.objoid = c.oid AND d.objsubid = 0
157-
WHERE n.nspname = '{0}'
157+
WHERE n.nspname = %s
158158
AND c.relkind IN ('r', 'v', 'p', 'm')
159-
AND c.relname NOT LIKE 'pg_%'
160-
AND c.relname NOT LIKE 'sql_%'
161-
ORDER BY c.relname \
159+
AND c.relname NOT LIKE 'pg_%%'
160+
AND c.relname NOT LIKE 'sql_%%'
161+
ORDER BY c.relname
162162
""", conf.dbSchema
163163
elif equals_ignore_case(ds.type, "es"):
164164
return "", None
@@ -212,7 +212,7 @@ def get_field_sql(ds: CoreDatasource, conf: DatasourceConf, table_name: str = No
212212
pg_catalog.pg_namespace n ON n.oid = c.relnamespace
213213
WHERE n.nspname = :param1
214214
AND a.attnum > 0
215-
AND NOT a.attisdropped \
215+
AND NOT a.attisdropped
216216
"""
217217
sql2 = " AND c.relname = :param2" if table_name is not None and table_name != "" else ""
218218
return sql1 + sql2, conf.dbSchema, table_name
@@ -228,7 +228,7 @@ def get_field_sql(ds: CoreDatasource, conf: DatasourceConf, table_name: str = No
228228
pg_catalog.pg_namespace n ON n.oid = c.relnamespace
229229
WHERE n.nspname = %s
230230
AND a.attnum > 0
231-
AND NOT a.attisdropped \
231+
AND NOT a.attisdropped
232232
"""
233233
sql2 = " AND c.relname = %s" if table_name is not None and table_name != "" else ""
234234
return sql1 + sql2, conf.dbSchema, table_name
@@ -308,11 +308,11 @@ def get_field_sql(ds: CoreDatasource, conf: DatasourceConf, table_name: str = No
308308
pg_catalog.pg_class c ON a.attrelid = c.oid
309309
JOIN
310310
pg_catalog.pg_namespace n ON n.oid = c.relnamespace
311-
WHERE n.nspname = '{0}'
311+
WHERE n.nspname = %s
312312
AND a.attnum > 0
313-
AND NOT a.attisdropped \
313+
AND NOT a.attisdropped
314314
"""
315-
sql2 = " AND c.relname = '{1}'" if table_name is not None and table_name != "" else ""
315+
sql2 = " AND c.relname = %s" if table_name is not None and table_name != "" else ""
316316
return sql1 + sql2, conf.dbSchema, table_name
317317
elif equals_ignore_case(ds.type, "es"):
318318
return "", None, None

0 commit comments

Comments
 (0)