Skip to content

Conversation

EugeneJinXin
Copy link
Contributor

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

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

The string
api.openai.com
may be at an arbitrary position in the sanitized URL.

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 parses base_url and checks if the hostname 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 via urlparse, and then the hostname property is tested accordingly.
Suggested changeset 1
python/tests/integration_tests/wrappers/test_openai.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/python/tests/integration_tests/wrappers/test_openai.py b/python/tests/integration_tests/wrappers/test_openai.py
--- a/python/tests/integration_tests/wrappers/test_openai.py
+++ b/python/tests/integration_tests/wrappers/test_openai.py
@@ -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")
EOF
@@ -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")
Copilot is powered by AI and may make mistakes. Always verify output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants