Skip to content

Commit c9df82a

Browse files
committed
security: fix SSRF vulnerability in URL path parameter replacement
- Add urllib.parse.quote to safely encode path parameters - Use safe='' to encode all special characters including '/', ':', '@' - Prevents attackers from injecting malicious hosts or internal service paths - Applies to both explicit PATH parameters and default path replacement logic
1 parent 0a14357 commit c9df82a

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

stackone_ai/models.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from enum import Enum
99
from functools import partial
1010
from typing import Annotated, Any, cast
11+
from urllib.parse import quote
1112

1213
import requests
1314
from langchain_core.tools import BaseTool
@@ -147,15 +148,19 @@ def _prepare_request_params(self, kwargs: JsonDict) -> tuple[str, JsonDict, Json
147148
param_location = self._execute_config.parameter_locations.get(key)
148149

149150
if param_location == ParameterLocation.PATH:
150-
url = url.replace(f"{{{key}}}", str(value))
151+
# Safely encode path parameters to prevent SSRF attacks
152+
encoded_value = quote(str(value), safe="")
153+
url = url.replace(f"{{{key}}}", encoded_value)
151154
elif param_location == ParameterLocation.QUERY:
152155
query_params[key] = value
153156
elif param_location in (ParameterLocation.BODY, ParameterLocation.FILE):
154157
body_params[key] = value
155158
else:
156159
# Default behavior
157160
if f"{{{key}}}" in url:
158-
url = url.replace(f"{{{key}}}", str(value))
161+
# Safely encode path parameters to prevent SSRF attacks
162+
encoded_value = quote(str(value), safe="")
163+
url = url.replace(f"{{{key}}}", encoded_value)
159164
elif self._execute_config.method in {"GET", "DELETE"}:
160165
query_params[key] = value
161166
else:

0 commit comments

Comments
 (0)