Skip to content

Commit 3e2399b

Browse files
committed
security: fix auth SQLi and add request timeouts in x2text-service
1 parent b312f03 commit 3e2399b

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

x2text-service/app/authentication_middleware.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99

1010
def authentication_middleware(func: Any) -> Any:
11+
"""Decorator to enforce bearer token authentication on flask routes."""
1112
def wrapper(*args: Any, **kwargs: Any) -> Any:
1213
token = AuthenticationMiddleware.get_token_from_auth_header(request)
1314
# Check if bearer token exists and validate it
@@ -23,13 +24,14 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
2324
class AuthenticationMiddleware:
2425
@classmethod
2526
def validate_bearer_token(cls, token: str | None) -> bool:
27+
"""Validate the provided bearer token against the database."""
2628
try:
2729
if token is None:
2830
current_app.logger.error("Authentication failed. Empty bearer token")
2931
return False
3032
platform_key_table = f'"{Env.DB_SCHEMA}".{DBTable.PLATFORM_KEY}'
31-
query = f"SELECT * FROM {platform_key_table} WHERE key = '{token}'"
32-
cursor = be_db.execute_sql(query)
33+
query = f"SELECT * FROM {platform_key_table} WHERE key = %s"
34+
cursor = be_db.execute_sql(query, (token,))
3335
result_row = cursor.fetchone()
3436
cursor.close()
3537
if not result_row or len(result_row) == 0:
@@ -62,6 +64,7 @@ def validate_bearer_token(cls, token: str | None) -> bool:
6264

6365
@classmethod
6466
def get_token_from_auth_header(cls, request: Request) -> str | None:
67+
"""Extract the bearer token from the Authorization header."""
6568
try:
6669
bearer_token = request.headers.get("Authorization")
6770
if not bearer_token:
@@ -99,6 +102,7 @@ def get_organization_from_bearer_token(cls, token: str) -> tuple[int | None, str
99102

100103
@classmethod
101104
def execute_query(cls, query: str, params: tuple = ()) -> Any:
105+
"""Execute a SQL query and return the first result."""
102106
cursor = be_db.execute_sql(query, params)
103107
result_row = cursor.fetchone()
104108
cursor.close()

x2text-service/app/controllers/controller.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626

2727
@basic.route("/health", methods=["GET"])
2828
def health() -> str:
29+
"""Check the health status of the service."""
2930
logging.info("Checking health from : %s", request.remote_addr)
3031
return "OK"
3132

3233

3334
@basic.route("/test-connection", methods=["POST"])
3435
@authentication_middleware
3536
def test_connection() -> Any:
37+
"""Test the connection to the Unstructured API."""
3638
logging.info("Received a test connection request from %s", request.remote_addr)
3739
form_data = dict(request.form)
3840
unstructured_api_key = X2TextUtil.get_value_for_key(UNSTRUCTURED_API_KEY, form_data)
@@ -54,7 +56,7 @@ def test_connection() -> Any:
5456
headers=headers,
5557
data=None,
5658
files=files,
57-
timeout=None,
59+
timeout=60,
5860
)
5961

6062
if response.status_code == 400:
@@ -76,6 +78,7 @@ def test_connection() -> Any:
7678
@basic.route("/process", methods=["POST"])
7779
@authentication_middleware
7880
def process() -> Any:
81+
"""Process a document for text extraction."""
7982
logging.info("Received a doc processing request from %s", request.remote_addr)
8083
form_data = dict(request.form)
8184
url = X2TextUtil.get_value_for_key(UNSTRUCTURED_URL, form_data)
@@ -122,7 +125,7 @@ def process() -> Any:
122125
headers=headers,
123126
data=payload,
124127
files=files,
125-
timeout=None,
128+
timeout=60,
126129
)
127130
if response.ok:
128131
json_response = response.json()

0 commit comments

Comments
 (0)