110 lines
4.5 KiB
Python
110 lines
4.5 KiB
Python
import asyncio
|
|
import re
|
|
from contextlib import asynccontextmanager
|
|
from dataclasses import replace
|
|
|
|
import pytest
|
|
|
|
from vuln_mcp.config import Settings
|
|
from vuln_mcp.proxy_list import fetch_proxy_list, refresh_proxy_list
|
|
|
|
|
|
@pytest.fixture
|
|
def anyio_backend():
|
|
return 'asyncio'
|
|
|
|
|
|
@asynccontextmanager
|
|
async def inventory(body, status='200 OK', extra=b''):
|
|
calls = []
|
|
|
|
async def serve(reader, writer):
|
|
calls.append(await reader.readuntil(b'\r\n\r\n'))
|
|
writer.write(f'HTTP/1.1 {status}\r\nContent-Length: {len(body)}\r\nConnection: close\r\n'.encode()
|
|
+ extra + b'\r\n' + body)
|
|
await writer.drain()
|
|
writer.close()
|
|
await writer.wait_closed()
|
|
|
|
async with await asyncio.start_server(serve, '127.0.0.1', 0) as server:
|
|
port = server.sockets[0].getsockname()[1]
|
|
yield f'http://127.0.0.1:{port}/v1/proxies?proxy_type=http&format=proxy-url-list-txt', calls
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_authenticated_inventory_request_preserves_order_and_query(monkeypatch):
|
|
async with inventory(b'\xef\xbb\xbfhttp://one:8000\r\n\r\nsocks5h://two:1080\n') as (url, calls):
|
|
monkeypatch.setenv('MCP_PROXY_LIST_URL', url)
|
|
monkeypatch.setenv('MCP_PROXY_LIST_TOKEN', 'fixture-token')
|
|
monkeypatch.setenv('MCP_PROXY_URLS', '[]')
|
|
monkeypatch.setenv('MCP_LIVE_ROUTE', 'proxy')
|
|
monkeypatch.setenv('HTTP_PROXY', 'http://127.0.0.1:1')
|
|
monkeypatch.setenv('NO_PROXY', '')
|
|
cfg = Settings.load()
|
|
assert 'fixture-token' not in repr(cfg)
|
|
direct = replace(cfg, live_route='direct')
|
|
assert await fetch_proxy_list(direct) is None and not calls
|
|
assert await fetch_proxy_list(cfg) == ('http://one:8000', 'socks5h://two:1080')
|
|
request, = calls
|
|
assert request.startswith(b'GET /v1/proxies?proxy_type=http&format=proxy-url-list-txt HTTP/1.1\r\n')
|
|
assert b'\r\nauthorization: bearer fixture-token\r\n' in request.lower()
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.parametrize('status,body', [('401 Unauthorized', b'sensitive-content'), ('200 OK', b''),
|
|
('200 OK', b'http://one\ninvalid sensitive-content')])
|
|
async def test_inventory_failure_disables_pool_not_privacy(tmp_path, caplog, status, body):
|
|
async with inventory(body, status) as (url, calls):
|
|
cfg = Settings(tmp_path, proxy_list_url=url, proxy_list_token='fixture-token')
|
|
assert await fetch_proxy_list(cfg) is None
|
|
assert len(calls) == 1
|
|
assert 'proxy_list unavailable' in caplog.text
|
|
assert 'fixture-token' not in caplog.text and 'sensitive-content' not in caplog.text
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_inventory_redirect_does_not_forward_token(tmp_path):
|
|
async with inventory(b'http://unexpected') as (target, received):
|
|
async with inventory(b'', '302 Found', f'Location: {target}\r\n'.encode()) as (url, _):
|
|
cfg = Settings(tmp_path, proxy_list_url=url, proxy_list_token='fixture-token')
|
|
assert await fetch_proxy_list(cfg) is None
|
|
assert received == []
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_periodic_refresh_keeps_last_success_then_recovers(tmp_path, monkeypatch):
|
|
results = iter([('http://first',), None, ('https://second',)])
|
|
delays, installed = [], []
|
|
|
|
async def tick(seconds):
|
|
delays.append(seconds)
|
|
if len(delays) == 4:
|
|
raise asyncio.CancelledError
|
|
|
|
async def fetch(_):
|
|
return next(results)
|
|
|
|
async def install(urls):
|
|
installed.append(urls)
|
|
|
|
monkeypatch.setattr('vuln_mcp.proxy_list.sleep', tick)
|
|
monkeypatch.setattr('vuln_mcp.proxy_list.fetch_proxy_list', fetch)
|
|
cfg = Settings(tmp_path, proxy_list_url='https://inventory.test/list', proxy_list_refresh_minutes=7)
|
|
for disabled in (replace(cfg, live_route='direct'), replace(cfg, proxy_list_url=None),
|
|
replace(cfg, proxy_list_refresh_minutes=0)):
|
|
await refresh_proxy_list(disabled, install)
|
|
assert not delays
|
|
with pytest.raises(asyncio.CancelledError):
|
|
await refresh_proxy_list(cfg, install)
|
|
assert delays == [420] * 4
|
|
assert installed == [('http://first',), ('https://second',)]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_only_the_trailing_limit_entries_are_kept(monkeypatch):
|
|
body = b''.join(f'http://user{i}:pass@10.0.0.1:8081\n'.encode() for i in range(5))
|
|
async with inventory(body) as (url, _calls):
|
|
cfg = Settings(None, proxy_list_url=url, proxy_list_token='fixture-token', proxy_list_limit=2)
|
|
kept = await fetch_proxy_list(cfg)
|
|
assert [re.search(r'user([0-9]+)', item)[1] for item in kept] == ['3', '4']
|