Skip to content

Commit 39f2203

Browse files
jackieyaXiaJunjie2020
authored andcommitted
fix: prevent SQL injection in uploadExcel via psycopg2.sql.Identifier
1 parent ff98514 commit 39f2203

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

backend/apps/datasource/api/datasource.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import traceback
66
import uuid
77
from io import StringIO
8+
from psycopg2 import sql
89
from typing import List
910
from urllib.parse import quote
1011

@@ -382,10 +383,10 @@ def insert_pg(df, tableName, engine):
382383
# output.seek(0)
383384

384385
# pg copy
385-
cursor.copy_expert(
386-
sql=f"""COPY "{tableName}" FROM STDIN WITH CSV DELIMITER E'\t'""",
387-
file=output
386+
query = sql.SQL("COPY {} FROM STDIN WITH CSV DELIMITER E'\t'").format(
387+
sql.Identifier(tableName)
388388
)
389+
cursor.copy_expert(sql=query.as_string(cursor.connection), file=output)
389390
conn.commit()
390391
except Exception as e:
391392
traceback.print_exc()

0 commit comments

Comments
 (0)