mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-29 09:30:07 +03:00
CharField: Detect and prohibit surrogate characters
This commit is contained in:
parent
8001087e9e
commit
4607c8a866
|
@ -35,6 +35,7 @@ from rest_framework.exceptions import ErrorDetail, ValidationError
|
||||||
from rest_framework.settings import api_settings
|
from rest_framework.settings import api_settings
|
||||||
from rest_framework.utils import html, humanize_datetime, json, representation
|
from rest_framework.utils import html, humanize_datetime, json, representation
|
||||||
from rest_framework.utils.formatting import lazy_format
|
from rest_framework.utils.formatting import lazy_format
|
||||||
|
from rest_framework.validators import ProhibitSurrogateCharactersValidator
|
||||||
|
|
||||||
|
|
||||||
class empty:
|
class empty:
|
||||||
|
@ -784,6 +785,7 @@ class CharField(Field):
|
||||||
# ProhibitNullCharactersValidator is None on Django < 2.0
|
# ProhibitNullCharactersValidator is None on Django < 2.0
|
||||||
if ProhibitNullCharactersValidator is not None:
|
if ProhibitNullCharactersValidator is not None:
|
||||||
self.validators.append(ProhibitNullCharactersValidator())
|
self.validators.append(ProhibitNullCharactersValidator())
|
||||||
|
self.validators.append(ProhibitSurrogateCharactersValidator())
|
||||||
|
|
||||||
def run_validation(self, data=empty):
|
def run_validation(self, data=empty):
|
||||||
# Test for the empty string here so that it does not get validated,
|
# Test for the empty string here so that it does not get validated,
|
||||||
|
|
|
@ -174,6 +174,17 @@ class UniqueTogetherValidator:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ProhibitSurrogateCharactersValidator:
|
||||||
|
message = _('Surrogate characters are not allowed: U+{code_point:X}.')
|
||||||
|
code = 'surrogate_characters_not_allowed'
|
||||||
|
|
||||||
|
def __call__(self, value):
|
||||||
|
for surrogate_character in (ch for ch in str(value)
|
||||||
|
if 0xD800 <= ord(ch) <= 0xDFFF):
|
||||||
|
message = self.message.format(code_point=ord(surrogate_character))
|
||||||
|
raise ValidationError(message, code=self.code)
|
||||||
|
|
||||||
|
|
||||||
class BaseUniqueForValidator:
|
class BaseUniqueForValidator:
|
||||||
message = None
|
message = None
|
||||||
missing_message = _('This field is required.')
|
missing_message = _('This field is required.')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user