|
|
@@ -1692,6 +1692,38 @@ class CustomFieldAPITestCase(APITestCase):
|
|
|
response = self.client.patch(url, data, format='json', **self.header)
|
|
|
self.assertHttpStatus(response, status.HTTP_200_OK)
|
|
|
|
|
|
+ def test_url_scheme_validation(self):
|
|
|
+ """
|
|
|
+ Test that URL custom field values must use a scheme permitted by ALLOWED_URL_SCHEMES (fixes
|
|
|
+ #22640), and that a schemeless value is normalized to an absolute URL (assume_scheme='https'),
|
|
|
+ consistent with the UI.
|
|
|
+ """
|
|
|
+ site2 = Site.objects.get(name='Site 2')
|
|
|
+ url = reverse('dcim-api:site-detail', kwargs={'pk': site2.pk})
|
|
|
+ self.add_permissions('dcim.change_site')
|
|
|
+
|
|
|
+ # A dangerous scheme (e.g. javascript:) must be rejected
|
|
|
+ data = {'custom_fields': {'url_field': 'javascript:alert(1)'}}
|
|
|
+ response = self.client.patch(url, data, format='json', **self.header)
|
|
|
+ self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
|
|
|
+
|
|
|
+ # A well-formed URL using a scheme outside ALLOWED_URL_SCHEMES must be rejected
|
|
|
+ data = {'custom_fields': {'url_field': 'gopher://example.com'}}
|
|
|
+ response = self.client.patch(url, data, format='json', **self.header)
|
|
|
+ self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
|
|
|
+
|
|
|
+ # An allowed scheme must be accepted
|
|
|
+ data = {'custom_fields': {'url_field': 'https://example.com'}}
|
|
|
+ response = self.client.patch(url, data, format='json', **self.header)
|
|
|
+ self.assertHttpStatus(response, status.HTTP_200_OK)
|
|
|
+
|
|
|
+ # A schemeless value must be accepted and normalized to https, matching the UI
|
|
|
+ data = {'custom_fields': {'url_field': 'example.com'}}
|
|
|
+ response = self.client.patch(url, data, format='json', **self.header)
|
|
|
+ self.assertHttpStatus(response, status.HTTP_200_OK)
|
|
|
+ site2.refresh_from_db()
|
|
|
+ self.assertEqual(site2.custom_field_data['url_field'], 'https://example.com')
|
|
|
+
|
|
|
def test_json_schema_validation(self):
|
|
|
site2 = Site.objects.get(name='Site 2')
|
|
|
url = reverse('dcim-api:site-detail', kwargs={'pk': site2.pk})
|