From b8983dcf57b46a4391891cecdfc856c9c1c8d363 Mon Sep 17 00:00:00 2001 From: Alexander Lötvall Date: Sat, 8 Jun 2024 12:14:46 +0200 Subject: [5.1.x] Fixed #35483 -- Added NUL (0x00) character validation to ModelChoiceFields. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Applied the ProhibitNullCharactersValidator to ModelChoiceField and ModelMultipleChoiceField. Co-authored-by: Viktor Paripás Co-authored-by: Vasyl Dizhak Co-authored-by: Arthur Vasconcelos Backport of 38ad710aba885ad26944ff5708ce1a02a446d2d3 from main. --- tests/model_forms/test_modelchoicefield.py | 8 +++++++- tests/model_forms/tests.py | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) (limited to 'tests/model_forms') diff --git a/tests/model_forms/test_modelchoicefield.py b/tests/model_forms/test_modelchoicefield.py index 19e9db69a0..83d801768a 100644 --- a/tests/model_forms/test_modelchoicefield.py +++ b/tests/model_forms/test_modelchoicefield.py @@ -7,7 +7,7 @@ from django.forms.widgets import CheckboxSelectMultiple from django.template import Context, Template from django.test import TestCase -from .models import Article, Author, Book, Category, Writer +from .models import Article, Author, Book, Category, ExplicitPK, Writer class ModelChoiceFieldTests(TestCase): @@ -79,6 +79,12 @@ class ModelChoiceFieldTests(TestCase): self.assertEqual(f.clean(self.c1.slug), self.c1) self.assertEqual(f.clean(self.c1), self.c1) + def test_model_choice_null_characters(self): + f = forms.ModelChoiceField(queryset=ExplicitPK.objects.all()) + msg = "Null characters are not allowed." + with self.assertRaisesMessage(ValidationError, msg): + f.clean("\x00something") + def test_choices(self): f = forms.ModelChoiceField( Category.objects.filter(pk=self.c1.id), required=False diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index 3f927cb053..c6e12e1aab 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -2227,6 +2227,15 @@ class ModelMultipleChoiceFieldTests(TestCase): f = forms.ModelMultipleChoiceField(queryset=Writer.objects.all()) self.assertNumQueries(1, f.clean, [p.pk for p in persons[1:11:2]]) + def test_model_multiple_choice_null_characters(self): + f = forms.ModelMultipleChoiceField(queryset=ExplicitPK.objects.all()) + msg = "Null characters are not allowed." + with self.assertRaisesMessage(ValidationError, msg): + f.clean(["\x00something"]) + + with self.assertRaisesMessage(ValidationError, msg): + f.clean(["valid", "\x00something"]) + def test_model_multiple_choice_run_validators(self): """ ModelMultipleChoiceField run given validators (#14144). -- cgit v1.3