2026-02-26 02:07:54 -05:00
|
|
|
from fastapi.templating import Jinja2Templates
|
|
|
|
|
|
2026-03-08 12:50:44 -04:00
|
|
|
|
2026-02-26 02:07:54 -05:00
|
|
|
def test_agent_chat_msg_xss_prevention():
|
|
|
|
|
"""Verify XSS prevention in agent_chat_msg.html."""
|
|
|
|
|
templates = Jinja2Templates(directory="src/dashboard/templates")
|
|
|
|
|
payload = "<script>alert('xss')</script>"
|
2026-03-08 12:50:44 -04:00
|
|
|
|
2026-02-26 02:07:54 -05:00
|
|
|
class MockAgent:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.name = "TestAgent"
|
|
|
|
|
self.id = "test-agent"
|
2026-03-08 12:50:44 -04:00
|
|
|
|
|
|
|
|
response = templates.get_template("partials/agent_chat_msg.html").render(
|
|
|
|
|
{
|
|
|
|
|
"message": payload,
|
|
|
|
|
"response": payload,
|
|
|
|
|
"error": payload,
|
|
|
|
|
"agent": MockAgent(),
|
|
|
|
|
"timestamp": "12:00:00",
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-26 02:07:54 -05:00
|
|
|
# Check that payload is escaped
|
|
|
|
|
assert "<script>alert('xss')</script>" in response
|
|
|
|
|
assert payload not in response
|
|
|
|
|
|
2026-03-08 12:50:44 -04:00
|
|
|
|
2026-02-26 02:07:54 -05:00
|
|
|
def test_agent_panel_xss_prevention():
|
|
|
|
|
"""Verify XSS prevention in agent_panel.html."""
|
|
|
|
|
templates = Jinja2Templates(directory="src/dashboard/templates")
|
|
|
|
|
payload = "<script>alert('xss')</script>"
|
2026-03-08 12:50:44 -04:00
|
|
|
|
2026-02-26 02:07:54 -05:00
|
|
|
class MockAgent:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.name = payload
|
|
|
|
|
self.id = "test-agent"
|
|
|
|
|
self.status = "idle"
|
|
|
|
|
self.capabilities = payload
|
2026-03-08 12:50:44 -04:00
|
|
|
|
2026-02-26 02:07:54 -05:00
|
|
|
class MockTask:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.id = "task-1"
|
2026-03-08 12:50:44 -04:00
|
|
|
self.status = type("obj", (object,), {"value": "completed"})
|
2026-02-26 02:07:54 -05:00
|
|
|
self.created_at = "2026-02-26T12:00:00"
|
|
|
|
|
self.description = payload
|
|
|
|
|
self.result = payload
|
|
|
|
|
|
2026-03-08 12:50:44 -04:00
|
|
|
response = templates.get_template("partials/agent_panel.html").render(
|
|
|
|
|
{"agent": MockAgent(), "tasks": [MockTask()]}
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-26 02:07:54 -05:00
|
|
|
assert "<script>alert('xss')</script>" in response
|
|
|
|
|
assert payload not in response
|
|
|
|
|
|
2026-03-08 12:50:44 -04:00
|
|
|
|
2026-02-26 02:07:54 -05:00
|
|
|
def test_swarm_sidebar_xss_prevention():
|
|
|
|
|
"""Verify XSS prevention in swarm_agents_sidebar.html."""
|
|
|
|
|
templates = Jinja2Templates(directory="src/dashboard/templates")
|
|
|
|
|
payload = "<script>alert('xss')</script>"
|
2026-03-08 12:50:44 -04:00
|
|
|
|
2026-02-26 02:07:54 -05:00
|
|
|
class MockAgent:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.name = payload
|
|
|
|
|
self.id = "test-agent"
|
|
|
|
|
self.status = "idle"
|
|
|
|
|
self.capabilities = payload
|
|
|
|
|
self.last_seen = "2026-02-26T12:00:00"
|
|
|
|
|
|
2026-03-08 12:50:44 -04:00
|
|
|
response = templates.get_template("partials/swarm_agents_sidebar.html").render(
|
|
|
|
|
{"agents": [MockAgent()]}
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-26 02:07:54 -05:00
|
|
|
assert "<script>alert('xss')</script>" in response
|
|
|
|
|
assert payload not in response
|