|
29 | 29 |
|
30 | 30 | from cds.modules.deposit.api import Video |
31 | 31 | from cds.modules.records.serializers.drupal import VideoDrupal |
| 32 | +from cds.modules.records.serializers.json import CDSJSONSerializer |
| 33 | +from cds.modules.records.api import CDSRecord |
| 34 | +from unittest.mock import Mock |
32 | 35 | from cds.modules.records.serializers.smil import Smil |
33 | 36 | from cds.modules.records.serializers.vtt import VTT |
34 | 37 |
|
@@ -149,3 +152,39 @@ def test_drupal_serializer(video_record_metadata, deposit_metadata): |
149 | 152 | data = serializer.format()["entries"][0]["entry"] |
150 | 153 | data = {k: data[k] for k in data if k in expected} |
151 | 154 | assert data == expected |
| 155 | + |
| 156 | + |
| 157 | +def test_cds_json_serializer_sanitization(video_record_metadata): |
| 158 | + """Test HTML sanitization in CDSJSONSerializer.""" |
| 159 | + record = CDSRecord.create(video_record_metadata) |
| 160 | + |
| 161 | + # Add malicious HTML |
| 162 | + record['description'] = '<script>alert("xss")</script>Safe content <b>bold</b>' |
| 163 | + record['title']['title'] = 'Test <script>alert("title")</script> Title <b>bold</b>' |
| 164 | + |
| 165 | + # Test the serializer |
| 166 | + serializer = CDSJSONSerializer() |
| 167 | + |
| 168 | + # Create a mock PID (required by the serializer) |
| 169 | + mock_pid = Mock() |
| 170 | + mock_pid.pid_value = '1' |
| 171 | + |
| 172 | + # Test preprocess_record method |
| 173 | + result = serializer.preprocess_record(mock_pid, record) |
| 174 | + |
| 175 | + # Check sanitization |
| 176 | + description = result['metadata']['description'] |
| 177 | + assert '<script>' not in description |
| 178 | + assert '</script>' not in description |
| 179 | + assert 'Safe content' in description |
| 180 | + # Keep safe HTML tags like <b> |
| 181 | + assert '<b>bold</b>' in description |
| 182 | + |
| 183 | + # Remove everything in title |
| 184 | + title = result['metadata']['title']['title'] |
| 185 | + assert '<script>' not in title |
| 186 | + assert '</script>' not in title |
| 187 | + assert 'Test' in title and 'Title' in title |
| 188 | + assert '<b>' not in title |
| 189 | + |
| 190 | + |
0 commit comments