|
11 | 11 | unregister_dashboard_chart, |
12 | 12 | unregister_dashboard_template, |
13 | 13 | ) |
| 14 | +from openwisp_utils.admin_theme.dashboard import get_dashboard_context |
14 | 15 |
|
15 | | -from ..models import Project |
| 16 | +from ..models import Operator, Project |
16 | 17 | from . import AdminTestMixin |
| 18 | +from .utils import MockRequest, MockUser |
17 | 19 |
|
18 | 20 |
|
19 | 21 | class TestDashboardSchema(UnitTestCase): |
@@ -178,3 +180,26 @@ def test_dashboard_disabled(self): |
178 | 180 | with self.subTest('Test "Dashboard" is absent from menu items'): |
179 | 181 | response = self.client.get(reverse('admin:index')) |
180 | 182 | self.assertNotContains(response, 'Dashboard') |
| 183 | + |
| 184 | + def test_get_dashboard_context_html_escape(self): |
| 185 | + # craft malicious DB value which will be shown in labels |
| 186 | + project = Project.objects.create(name='<script>alert(1)</script>') |
| 187 | + Operator.objects.create(project=project, first_name='xss', last_name='xss') |
| 188 | + # prepare mock request and get context |
| 189 | + mocked_user = MockUser(is_superuser=True) |
| 190 | + mocked_request = MockRequest(user=mocked_user) |
| 191 | + context = get_dashboard_context(mocked_request) |
| 192 | + # ensure DB value is escaped |
| 193 | + self.assertEqual( |
| 194 | + context['dashboard_charts'][0]['query_params']['labels'][0], |
| 195 | + '<script>alert(1)</script>', |
| 196 | + ) |
| 197 | + # ensure configured labels are escaped |
| 198 | + self.assertEqual( |
| 199 | + context['dashboard_charts'][1]['labels']['with_operator__sum'], |
| 200 | + '<strong>Projects with operators</strong>', |
| 201 | + ) |
| 202 | + self.assertEqual( |
| 203 | + context['dashboard_charts'][1]['query_params']['labels'][0], |
| 204 | + '<strong>Projects with operators</strong>', |
| 205 | + ) |
0 commit comments