feat: add bearer token auth check for MCP server

This commit is contained in:
pyr0ball 2026-07-04 07:08:21 -07:00
parent d2b85fc276
commit b2887753e9
2 changed files with 24 additions and 0 deletions

View file

@ -38,3 +38,18 @@ def test_client_raises_on_error_response():
client = DebugAgentClient(host="127.0.0.1", port=stub.port)
with pytest.raises(DebugAgentError, match="DR0-DR3"):
client.call("set_watchpoint", {"addr": "0x5000", "size": 4, "mode": "write"})
from tools.havoc_mcp_server import check_bearer_token
def test_check_bearer_token_accepts_matching_token():
assert check_bearer_token("Bearer secret123", expected="secret123") is True
def test_check_bearer_token_rejects_wrong_token():
assert check_bearer_token("Bearer wrong", expected="secret123") is False
def test_check_bearer_token_rejects_missing_header():
assert check_bearer_token(None, expected="secret123") is False
def test_check_bearer_token_rejects_malformed_header():
assert check_bearer_token("secret123", expected="secret123") is False # missing "Bearer " prefix

View file

@ -34,3 +34,12 @@ class DebugAgentClient:
if not response.get("ok"):
raise DebugAgentError(response.get("error", "unknown debug agent error"))
return response.get("result", {})
def check_bearer_token(header_value: str | None, expected: str) -> bool:
if header_value is None:
return False
prefix = "Bearer "
if not header_value.startswith(prefix):
return False
return header_value[len(prefix):] == expected