Arthur 3 giorni fa
parent
commit
0b86cb436b

+ 7 - 9
netbox/extras/tests/test_forms.py

@@ -9,6 +9,7 @@ from core.choices import ManagedFileRootPathChoices
 from core.events import OBJECT_CREATED
 from core.models import DataSource, ObjectType
 from dcim.forms import SiteForm
+from dcim.forms.filtersets import PowerOutletFilterForm
 from dcim.models import Site
 from extras.choices import CustomFieldTypeChoices, EventRuleActionChoices
 from extras.forms import SavedFilterForm, TableConfigBulkEditForm, TableConfigForm
@@ -719,12 +720,9 @@ class TagFormTestCase(TestCase):
             str(form['color'])
         )
 
-        form = TagForm(instance=tag, data={
-            'name': 'Tag 1 renamed',
-            'slug': 'tag-1',
-            'color': 'e20074',
-            'weight': 1000,
-        })
-        self.assertTrue(form.is_valid(), form.errors)
-        tag = form.save()
-        self.assertEqual(tag.color, 'e20074')
+    def test_filter_form_ignores_invalid_color(self):
+        """
+        A non-hex color passed to a filter form must not be rendered as an option.
+        """
+        form = PowerOutletFilterForm(data={'color': 'ff0000;background:url(x)'})
+        self.assertNotIn('background:url', str(form['color']))

File diff suppressed because it is too large
+ 0 - 0
netbox/project-static/dist/netbox.js


File diff suppressed because it is too large
+ 0 - 0
netbox/project-static/dist/netbox.js.map


+ 3 - 1
netbox/project-static/src/select/static.ts

@@ -35,8 +35,10 @@ export function initStaticSelects(): void {
 // Initialize color selection fields
 export function initColorSelects(): void {
   function renderColor(item: TomOption, escape: typeof escape_html) {
+    // Only hex RGB values are safe to inject into the inline style
+    const color = /^[0-9a-f]{6}$/i.test(item.value) ? item.value : '';
     return `<div><span class="dropdown-item-indicator color-label" style="background-color: #${escape(
-      item.value,
+      color,
     )}"></span> ${escape(item.text)}</div>`;
   }
 

+ 5 - 4
netbox/utilities/forms/widgets/select.py

@@ -1,6 +1,7 @@
 from django import forms
 
 from netbox.choices import ColorChoices
+from utilities.validators import ColorValidator
 
 from ..utils import add_blank_choice
 
@@ -96,10 +97,10 @@ class ColorSelect(forms.Select):
 
     def optgroups(self, name, value, attrs=None):
         groups = super().optgroups(name, value, attrs)
-        # Render a custom (non-predefined) color as a selected option so it isn't lost on submission
-        known_values = {str(choice[0]) for choice in self.choices}
-        for color in value:
-            if color and color not in known_values:
+        # Render a valid custom (non-predefined) color as a selected option so it isn't lost on submission
+        color = value[0] if value else None
+        if color and ColorValidator.regex.fullmatch(color):
+            if not any(option['selected'] for _, options, _ in groups for option in options):
                 index = len(groups)
                 groups.append((None, [self.create_option(name, color, color, True, index)], index))
         return groups

+ 6 - 0
netbox/utilities/tests/test_forms.py

@@ -1172,3 +1172,9 @@ class ColorSelectTestCase(TestCase):
         widget = ColorSelect()
         html = widget.render('color', None)
         self.assertEqual(html.count('<option'), len(widget.choices))
+
+    def test_invalid_color_adds_no_option(self):
+        widget = ColorSelect()
+        html = widget.render('color', 'ff0000;background:url(x)')
+        self.assertNotIn('background:url', html)
+        self.assertEqual(html.count('<option'), len(widget.choices))

Some files were not shown because too many files changed in this diff