119 lines
4.9 KiB
Python
119 lines
4.9 KiB
Python
import pytest
|
|
|
|
from vuln_mcp.config import Settings
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def clean_env(monkeypatch):
|
|
for name in ("MCP_ENV_FILE", "MCP_LIVE_ROUTE", "MCP_PROXY_URLS", "MCP_PROXY_LIST_URL", "MCP_PROXY_LIST_TOKEN", "MCP_HTTP_MAX_RETRIES", "MCP_DATA_DIR"):
|
|
monkeypatch.delenv(name, raising=False)
|
|
monkeypatch.delenv("MCP_PROXY_LIST_REFRESH_MINUTES", raising=False)
|
|
|
|
|
|
def test_env_file_only_when_explicit(tmp_path, monkeypatch):
|
|
monkeypatch.chdir(tmp_path)
|
|
config = tmp_path / ".env"
|
|
config.write_text(
|
|
'MCP_LIVE_ROUTE=direct\nMCP_HTTP_MAX_RETRIES=2\nMCP_PROXY_URLS=["socks5://user:abc%24@localhost:1080"]\n'
|
|
)
|
|
settings = Settings.load()
|
|
assert settings.live_route == "proxy" and settings.proxies == ()
|
|
monkeypatch.setenv("MCP_ENV_FILE", str(config))
|
|
assert Settings.load().live_route == "direct"
|
|
monkeypatch.setenv("MCP_LIVE_ROUTE", "proxy")
|
|
monkeypatch.setenv("MCP_HTTP_MAX_RETRIES", "0")
|
|
settings = Settings.load()
|
|
assert settings.live_route == "proxy"
|
|
assert settings.max_retries == 0 and settings.proxies == ("socks5://user:abc%24@localhost:1080",)
|
|
assert "abc" not in repr(settings)
|
|
|
|
|
|
@pytest.mark.parametrize("value", ["", "DIRECT", " direct", "private-token"])
|
|
def test_bad_live_route_is_rejected_without_echo(monkeypatch, value):
|
|
monkeypatch.setenv("MCP_LIVE_ROUTE", value)
|
|
with pytest.raises(ValueError, match="MCP_LIVE_ROUTE must be 'direct' or 'proxy'") as caught:
|
|
Settings.load()
|
|
assert "private-token" not in str(caught.value)
|
|
|
|
|
|
def test_direct_route_accepts_empty_proxy_pool(monkeypatch):
|
|
monkeypatch.setenv("MCP_LIVE_ROUTE", "direct")
|
|
settings = Settings.load()
|
|
assert settings.live_route == "direct" and settings.proxies == ()
|
|
|
|
|
|
def test_proxy_urls_accept_all_supported_schemes(monkeypatch):
|
|
monkeypatch.setenv(
|
|
"MCP_PROXY_URLS", '["http://one", "https://two/", "socks5://three", "socks5h://user:pwd%40@four:1081"]'
|
|
)
|
|
settings = Settings.load()
|
|
assert settings.proxies == ("http://one", "https://two/", "socks5://three", "socks5h://user:pwd%40@four:1081")
|
|
assert "pwd" not in repr(settings)
|
|
|
|
|
|
@pytest.mark.parametrize("value", [
|
|
'', '{}', '[4]', '["file:///tmp/x"]', '["http://abc:pwd@host:99999"]',
|
|
'["http://host:80", "http://HOST"]', '["http://host/p"]', '["https://host/?query"]',
|
|
'["socks5h://host:0"]', '["socks5h://user:pwd%XX@host"]', '["socks5h://host/#fragment"]',
|
|
'["socks5://host", "socks5h://HOST.:1080"]',
|
|
])
|
|
def test_bad_proxy_is_not_direct(value, monkeypatch):
|
|
monkeypatch.setenv("MCP_PROXY_URLS", value)
|
|
with pytest.raises(ValueError, match="Invalid MCP_PROXY_URLS") as caught:
|
|
Settings.load()
|
|
assert "pwd" not in str(caught.value)
|
|
|
|
|
|
def test_explicit_direct_still_validates_proxy_configuration(monkeypatch):
|
|
monkeypatch.setenv("MCP_LIVE_ROUTE", "direct")
|
|
monkeypatch.setenv("MCP_PROXY_URLS", "invalid")
|
|
with pytest.raises(ValueError, match="Invalid MCP_PROXY_URLS"):
|
|
Settings.load()
|
|
|
|
|
|
def test_configurable_inventory_interval(tmp_path, monkeypatch):
|
|
path = tmp_path / '.env'
|
|
path.write_text('MCP_PROXY_LIST_REFRESH_MINUTES=10\n')
|
|
monkeypatch.setenv('MCP_ENV_FILE', str(path))
|
|
assert Settings.load().proxy_list_refresh_minutes == 10
|
|
monkeypatch.setenv('MCP_PROXY_LIST_REFRESH_MINUTES', '0')
|
|
assert Settings.load().proxy_list_refresh_minutes == 0
|
|
|
|
|
|
@pytest.mark.parametrize('value', ['-1', '0.5', '1441', ''])
|
|
def test_inventory_interval_rejects_invalid_values(monkeypatch, value):
|
|
monkeypatch.setenv('MCP_PROXY_LIST_REFRESH_MINUTES', value)
|
|
with pytest.raises(ValueError, match='MCP_PROXY_LIST_REFRESH_MINUTES'):
|
|
Settings.load()
|
|
|
|
|
|
def test_accounts_on_one_gateway_are_distinct_routes(monkeypatch):
|
|
monkeypatch.setenv('MCP_PROXY_URLS', '["http://a:1@10.0.0.1:8081", "http://a:2@10.0.0.1:8081"]')
|
|
assert Settings.load().proxies == ('http://a:1@10.0.0.1:8081', 'http://a:2@10.0.0.1:8081')
|
|
|
|
|
|
def test_same_credentials_on_one_gateway_are_a_duplicate(monkeypatch):
|
|
monkeypatch.setenv('MCP_PROXY_URLS', '["http://a:1@10.0.0.1:8081", "http://a:1@10.0.0.1:8081"]')
|
|
with pytest.raises(ValueError):
|
|
Settings.load()
|
|
|
|
|
|
def test_proxy_list_limit_bounds(monkeypatch):
|
|
for value, ok in (('1', True), ('1024', True), ('0', False), ('1025', False), ('', False)):
|
|
monkeypatch.setenv('MCP_PROXY_LIST_LIMIT', value)
|
|
if ok:
|
|
assert Settings.load().proxy_list_limit == int(value)
|
|
else:
|
|
with pytest.raises(ValueError):
|
|
Settings.load()
|
|
|
|
|
|
def test_live_timeout_is_configurable_and_bounded(monkeypatch, tmp_path):
|
|
monkeypatch.setenv('MCP_LIVE_TIMEOUT_SECONDS', '4.5')
|
|
assert Settings.load().live_timeout == 4.5
|
|
assert Settings(tmp_path).live_timeout == 9.5
|
|
for value in ('0.5', '61', 'nope', ''):
|
|
monkeypatch.setenv('MCP_LIVE_TIMEOUT_SECONDS', value)
|
|
with pytest.raises(ValueError):
|
|
Settings.load()
|