|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""AgentStudio base URL resolution. |
| 3 | +
|
| 4 | +``workspace`` and ``region`` are interpolated into the MaaS host |
| 5 | +template ``https://{workspace}.{region}.maas.aliyuncs.com``. Both reach |
| 6 | +:func:`_resolve_base_url` from a public ``Client`` keyword argument or |
| 7 | +from the ``DASHSCOPE_WORKSPACE`` env var, so both are validated as DNS |
| 8 | +labels: an unvalidated ``"a@evil.com/#"`` makes httpx resolve the host |
| 9 | +to ``evil.com`` while the transport still attaches |
| 10 | +``Authorization: Bearer <api_key>``. |
| 11 | +""" |
| 12 | + |
| 13 | +import httpx |
| 14 | +import pytest |
| 15 | + |
| 16 | +from dashscope.agentstudio.client import _resolve_base_url |
| 17 | + |
| 18 | +MAAS_SUFFIX = ".maas.aliyuncs.com" |
| 19 | + |
| 20 | +# Values that break out of the host component when interpolated raw. |
| 21 | +HOST_BREAKING = [ |
| 22 | + "a@evil.com/#", |
| 23 | + "evil.com/x", |
| 24 | + "evil.com:443", |
| 25 | + "evil.com?x", |
| 26 | + "evil.com#frag", |
| 27 | + "white space", |
| 28 | + "abc\n", |
| 29 | +] |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture(autouse=True) |
| 33 | +def _clean_env(monkeypatch): |
| 34 | + """Keep ambient env vars from changing resolution precedence.""" |
| 35 | + for var in [ |
| 36 | + "DASHSCOPE_WORKSPACE", |
| 37 | + "DASHSCOPE_AGENTSTUDIO_URL", |
| 38 | + "AGENTSTUDIO_URL", |
| 39 | + ]: |
| 40 | + monkeypatch.delenv(var, raising=False) |
| 41 | + |
| 42 | + |
| 43 | +def test_builds_url_from_workspace_and_region(): |
| 44 | + url = _resolve_base_url(None, "ws-demo", "cn-beijing") |
| 45 | + assert url == ( |
| 46 | + "https://ws-demo.cn-beijing.maas.aliyuncs.com/api/v1/agentstudio" |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def test_region_defaults_when_omitted(): |
| 51 | + url = _resolve_base_url(None, "ws-demo", None) |
| 52 | + assert httpx.URL(url).host == f"ws-demo.cn-beijing{MAAS_SUFFIX}" |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize("bad", HOST_BREAKING) |
| 56 | +def test_rejects_host_breaking_workspace(bad): |
| 57 | + with pytest.raises(ValueError, match="workspace_id"): |
| 58 | + _resolve_base_url(None, bad, "cn-beijing") |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.parametrize("bad", HOST_BREAKING + ["-dash", "a" * 64]) |
| 62 | +def test_rejects_host_breaking_region(bad): |
| 63 | + with pytest.raises(ValueError, match="region"): |
| 64 | + _resolve_base_url(None, "ws-demo", bad) |
| 65 | + |
| 66 | + |
| 67 | +def test_uppercase_region_still_resolves(): |
| 68 | + """Compat guard: httpx lowercases the host, so case must not be rejected.""" |
| 69 | + url = _resolve_base_url(None, "ws-demo", "CN-Beijing") |
| 70 | + assert httpx.URL(url).host == f"ws-demo.cn-beijing{MAAS_SUFFIX}" |
| 71 | + |
| 72 | + |
| 73 | +def test_rejects_workspace_from_env(monkeypatch): |
| 74 | + """The env-sourced workspace is validated on the same path.""" |
| 75 | + monkeypatch.setenv("DASHSCOPE_WORKSPACE", "a@evil.com/#") |
| 76 | + with pytest.raises(ValueError, match="workspace_id"): |
| 77 | + _resolve_base_url(None, None, "cn-beijing") |
| 78 | + |
| 79 | + |
| 80 | +def test_resolved_host_stays_under_maas_domain(): |
| 81 | + """Positive control for the property the checks above protect.""" |
| 82 | + url = _resolve_base_url(None, "llm-abc_def", "ap-southeast-1") |
| 83 | + assert httpx.URL(url).host.endswith(MAAS_SUFFIX) |
| 84 | + |
| 85 | + |
| 86 | +def test_missing_workspace_error_is_actionable(): |
| 87 | + with pytest.raises(ValueError, match="workspace is required"): |
| 88 | + _resolve_base_url(None, None, "cn-beijing") |
| 89 | + |
| 90 | + |
| 91 | +def test_explicit_base_url_overrides_without_validation(): |
| 92 | + """A deliberate full-URL override is trusted, per documented priority.""" |
| 93 | + assert _resolve_base_url("https://my.host/api", None, None) == ( |
| 94 | + "https://my.host/api" |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +def test_env_base_url_overrides_without_validation(monkeypatch): |
| 99 | + monkeypatch.setenv("DASHSCOPE_AGENTSTUDIO_URL", "https://my.host/api") |
| 100 | + assert _resolve_base_url(None, None, None) == "https://my.host/api" |
| 101 | + |
| 102 | + |
| 103 | +def test_sync_client_rejects_injected_workspace(): |
| 104 | + """The attack surface is the public constructor, not just the helper.""" |
| 105 | + from dashscope.agentstudio import Client |
| 106 | + |
| 107 | + with pytest.raises(ValueError, match="workspace_id"): |
| 108 | + Client(api_key="test-key", workspace="a@evil.com/#") |
| 109 | + |
| 110 | + |
| 111 | +def test_async_client_rejects_injected_region(): |
| 112 | + from dashscope.agentstudio import AsyncClient |
| 113 | + |
| 114 | + with pytest.raises(ValueError, match="region"): |
| 115 | + AsyncClient( |
| 116 | + api_key="test-key", |
| 117 | + workspace="ws-demo", |
| 118 | + region="x@evil.com/#", |
| 119 | + ) |
0 commit comments