Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk): refuse a Python request path without a leading slash

Yudhi Armyndharis88c02251d639
diff --git a/sdk/python/openwa/_http.py b/sdk/python/openwa/_http.py
index 09ffb7a91..317ff4c88 100644
--- a/sdk/python/openwa/_http.py
+++ b/sdk/python/openwa/_http.py
@@ -35,7 +35,15 @@ def quote_segment(segment: Any) -> str:
def build_url(base_url: str, path: str, query: Mapping[str, Any] | None = None) -> str:
- """Build a URL, serializing query params and skipping ``None`` values."""
+ """Build a URL, serializing query params and skipping ``None`` values.
+
+ Raises :class:`ValueError` for a path that does not begin with ``/``, as the
+ JavaScript, Go and Java clients do: an absolute URL such as
+ ``https://example.net/x`` would replace the client's base host and carry the
+ API key there.
+ """
+ if not path.startswith("/"):
+ raise ValueError(f"OpenWA: path must begin with '/': {path!r}")
url = f"{base_url.rstrip('/')}{path}"
if not query:
return url
diff --git a/sdk/python/tests/test_sdk.py b/sdk/python/tests/test_sdk.py
index cad05284e..634b14e61 100644
--- a/sdk/python/tests/test_sdk.py
+++ b/sdk/python/tests/test_sdk.py
@@ -152,6 +152,16 @@ class TestClientCore:
assert build_url("http://x", "/api/search", {"q": "%41"}).endswith("q=%2541")
assert build_url("http://x", "/api/search", {"q": "50%"}).endswith("q=50%25")
+ @pytest.mark.parametrize("path", ["https://evil.example/x", ".evil.example/x", "@evil.example/x"])
+ def test_build_url_refuses_a_path_without_a_leading_slash(self, path):
+ # An absolute URL would replace the base host and carry the API key there.
+ with pytest.raises(ValueError):
+ build_url("http://x", path)
+ backend = MockBackend()
+ with pytest.raises(ValueError):
+ make_client(backend).request("GET", path)
+ assert backend.calls == []
+
def test_build_url_omits_none_valued_params(self):
# The behaviour a caller depends on: a None never reaches the wire as the literal "None".
# Tested here because build_url is what implements it — the typed resource signatures