-
Notifications
You must be signed in to change notification settings - Fork 160
feat: add log of base_url #2058
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
for event in [*data.get("post", []), *data.get("patch", [])]: | ||
metadata = event.get("extra", {}).get("metadata", {}) | ||
if base_url := metadata.get("openai_base_url"): | ||
assert "api.openai.com" in base_url |
Check failure
Code scanning / CodeQL
Incomplete URL substring sanitization High test
api.openai.com
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 7 days ago
To fix the problem, the code should extract the host part from base_url
using urllib.parse.urlparse
, then check if the hostname matches or ends with the expected domain. This ensures malicious domains such as "evil-api.openai.com.attacker.com"
do not get accepted, and prevents accidental substring matches in arbitrary positions.
Specifically:
- Import
urllib.parse.urlparse
. - Replace the substring check
if "api.openai.com" in base_url:
with logic that parsesbase_url
and checks if thehostname
is exactly"api.openai.com"
(or, if subdomains are desired, ends with.api.openai.com
). Since the original code wants to check for presence of"api.openai.com"
, matching exactly is the clearest option.
Required changes:
- Add the import line.
- Change the assertion block so that
base_url
is parsed viaurlparse
, and then the hostname property is tested accordingly.
-
Copy modified lines R843-R844
@@ -840,7 +840,8 @@ | ||
for event in [*data.get("post", []), *data.get("patch", [])]: | ||
metadata = event.get("extra", {}).get("metadata", {}) | ||
if base_url := metadata.get("openai_base_url"): | ||
assert "api.openai.com" in base_url | ||
from urllib.parse import urlparse | ||
assert urlparse(base_url).hostname == "api.openai.com" | ||
return | ||
|
||
raise AssertionError("openai_base_url not found in metadata") |
Worth adding this openai.base_url since it's important metadata, helping usage analytics of openAI format-compliant providers such as gemini
idea credits to jake https://langchain.slack.com/archives/C08RJTF3QS2/p1758664251472349?thread_ts=1758663268.667749&cid=C08RJTF3QS2