From 19d12826837e723f5b4cea8735b3ea06d0567c12 Mon Sep 17 00:00:00 2001 From: Fabi <18670690+fabi125@users.noreply.github.com> Date: Tue, 11 Mar 2025 15:18:42 -0400 Subject: [PATCH] Fixes #18838: Correctly reject invalid falsy local context data (#18860) * Correctly reject invalid falsy local context data. * move tests --- netbox/extras/models/configs.py | 2 +- netbox/extras/tests/test_models.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/netbox/extras/models/configs.py b/netbox/extras/models/configs.py index 6b52d4c02..773f5a70e 100644 --- a/netbox/extras/models/configs.py +++ b/netbox/extras/models/configs.py @@ -200,7 +200,7 @@ class ConfigContextModel(models.Model): super().clean() # Verify that JSON data is provided as an object - if self.local_context_data and type(self.local_context_data) is not dict: + if self.local_context_data is not None and type(self.local_context_data) is not dict: raise ValidationError( {'local_context_data': _('JSON data must be in object form. Example:') + ' {"foo": 123}'} ) diff --git a/netbox/extras/tests/test_models.py b/netbox/extras/tests/test_models.py index c90390dd1..34882537d 100644 --- a/netbox/extras/tests/test_models.py +++ b/netbox/extras/tests/test_models.py @@ -1,3 +1,4 @@ +from django.forms import ValidationError from django.test import TestCase from core.models import ObjectType @@ -478,3 +479,30 @@ class ConfigContextTest(TestCase): annotated_queryset = Device.objects.filter(name=device.name).annotate_config_context_data() self.assertEqual(ConfigContext.objects.get_for_object(device).count(), 2) self.assertEqual(device.get_config_context(), annotated_queryset[0].get_config_context()) + + def test_valid_local_context_data(self): + device = Device.objects.first() + device.local_context_data = None + device.clean() + + device.local_context_data = {"foo": "bar"} + device.clean() + + def test_invalid_local_context_data(self): + device = Device.objects.first() + + device.local_context_data = "" + with self.assertRaises(ValidationError): + device.clean() + + device.local_context_data = 0 + with self.assertRaises(ValidationError): + device.clean() + + device.local_context_data = False + with self.assertRaises(ValidationError): + device.clean() + + device.local_context_data = 'foo' + with self.assertRaises(ValidationError): + device.clean()