Skip to content

Commit ff98514

Browse files
committed
fix: Fix SSRF causing arbitrary file read vulnerability
1 parent 79f7830 commit ff98514

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

backend/apps/db/constant.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Date: 2025/7/16
33

44
from enum import Enum
5+
from typing import List
56

67
from common.utils.utils import equals_ignore_case
78

@@ -15,26 +16,28 @@ def __init__(self, type_name):
1516

1617

1718
class DB(Enum):
18-
excel = ('excel', 'Excel/CSV', '"', '"', ConnectType.sqlalchemy, 'PostgreSQL')
19-
redshift = ('redshift', 'AWS Redshift', '"', '"', ConnectType.py_driver, 'AWS_Redshift')
20-
ck = ('ck', 'ClickHouse', '"', '"', ConnectType.sqlalchemy, 'ClickHouse')
21-
dm = ('dm', '达梦', '"', '"', ConnectType.py_driver, 'DM')
22-
doris = ('doris', 'Apache Doris', '`', '`', ConnectType.py_driver, 'Doris')
23-
es = ('es', 'Elasticsearch', '"', '"', ConnectType.py_driver, 'Elasticsearch')
24-
kingbase = ('kingbase', 'Kingbase', '"', '"', ConnectType.py_driver, 'Kingbase')
25-
sqlServer = ('sqlServer', 'Microsoft SQL Server', '[', ']', ConnectType.sqlalchemy, 'Microsoft_SQL_Server')
26-
mysql = ('mysql', 'MySQL', '`', '`', ConnectType.sqlalchemy, 'MySQL')
27-
oracle = ('oracle', 'Oracle', '"', '"', ConnectType.sqlalchemy, 'Oracle')
28-
pg = ('pg', 'PostgreSQL', '"', '"', ConnectType.sqlalchemy, 'PostgreSQL')
29-
starrocks = ('starrocks', 'StarRocks', '`', '`', ConnectType.py_driver, 'StarRocks')
30-
31-
def __init__(self, type, db_name, prefix, suffix, connect_type: ConnectType, template_name: str):
19+
excel = ('excel', 'Excel/CSV', '"', '"', ConnectType.sqlalchemy, 'PostgreSQL', [])
20+
redshift = ('redshift', 'AWS Redshift', '"', '"', ConnectType.py_driver, 'AWS_Redshift', [])
21+
ck = ('ck', 'ClickHouse', '"', '"', ConnectType.sqlalchemy, 'ClickHouse', [])
22+
dm = ('dm', '达梦', '"', '"', ConnectType.py_driver, 'DM', [])
23+
doris = ('doris', 'Apache Doris', '`', '`', ConnectType.py_driver, 'Doris', [])
24+
es = ('es', 'Elasticsearch', '"', '"', ConnectType.py_driver, 'Elasticsearch', [])
25+
kingbase = ('kingbase', 'Kingbase', '"', '"', ConnectType.py_driver, 'Kingbase', [])
26+
sqlServer = ('sqlServer', 'Microsoft SQL Server', '[', ']', ConnectType.sqlalchemy, 'Microsoft_SQL_Server', [])
27+
mysql = ('mysql', 'MySQL', '`', '`', ConnectType.sqlalchemy, 'MySQL', ['local_infile'])
28+
oracle = ('oracle', 'Oracle', '"', '"', ConnectType.sqlalchemy, 'Oracle', [])
29+
pg = ('pg', 'PostgreSQL', '"', '"', ConnectType.sqlalchemy, 'PostgreSQL', [])
30+
starrocks = ('starrocks', 'StarRocks', '`', '`', ConnectType.py_driver, 'StarRocks', [])
31+
32+
def __init__(self, type, db_name, prefix, suffix, connect_type: ConnectType, template_name: str,
33+
illegalParams: List[str]):
3234
self.type = type
3335
self.db_name = db_name
3436
self.prefix = prefix
3537
self.suffix = suffix
3638
self.connect_type = connect_type
3739
self.template_name = template_name
40+
self.illegalParams = illegalParams
3841

3942
@classmethod
4043
def get_db(cls, type, default_if_none=False):

backend/apps/db/db.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import urllib.parse
66
from datetime import datetime, date, time, timedelta
77
from decimal import Decimal
8-
from typing import Optional
8+
from typing import Optional, List
99

1010
import oracledb
1111
import psycopg2
@@ -57,6 +57,7 @@ def get_uri(ds: CoreDatasource) -> str:
5757
def get_uri_from_config(type: str, conf: DatasourceConf) -> str:
5858
db_url: str
5959
if equals_ignore_case(type, "mysql"):
60+
checkParams(conf.extraJdbc, DB.mysql.illegalParams)
6061
if conf.extraJdbc is not None and conf.extraJdbc != '':
6162
db_url = f"mysql+pymysql://{urllib.parse.quote(conf.username)}:{urllib.parse.quote(conf.password)}@{conf.host}:{conf.port}/{conf.database}?{conf.extraJdbc}"
6263
else:
@@ -682,3 +683,11 @@ def check_sql_read(sql: str, ds: CoreDatasource | AssistantOutDsSchema):
682683

683684
except Exception as e:
684685
raise ValueError(f"Parse SQL Error: {e}")
686+
687+
688+
def checkParams(extraParams: str, illegalParams: List[str]):
689+
kvs = extraParams.split('&')
690+
for kv in kvs:
691+
k, v = kv.split('=')
692+
if k in illegalParams:
693+
raise HTTPException(status_code=500, detail=f'Illegal Parameter: {k}')

0 commit comments

Comments
 (0)