1 Star 0 Fork 6

ZoeDong/python-django

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
2d2c1d0c97832860fbd6597977e2aae17dd7e5b2.patch 2.12 KB
一键复制 编辑 原始数据 按行查看 历史
diff --git a/django/core/validators.py b/django/core/validators.py
index bd2122f..832697c 100644
--- a/django/core/validators.py
+++ b/django/core/validators.py
@@ -92,6 +92,7 @@ class URLValidator(RegexValidator):
r'\Z', re.IGNORECASE)
message = _('Enter a valid URL.')
schemes = ['http', 'https', 'ftp', 'ftps']
+ unsafe_chars = frozenset('\t\r\n')
def __init__(self, schemes=None, **kwargs):
super().__init__(**kwargs)
@@ -101,6 +102,8 @@ class URLValidator(RegexValidator):
def __call__(self, value):
if not isinstance(value, str):
raise ValidationError(self.message, code=self.code, params={'value': value})
+ if self.unsafe_chars.intersection(value):
+ raise ValidationError(self.message, code=self.code, params={'value': value})
# Check if the scheme is valid.
scheme = value.split('://')[0].lower()
if scheme not in self.schemes:
diff --git a/tests/validators/tests.py b/tests/validators/tests.py
index d6d013c..09d5c40 100644
--- a/tests/validators/tests.py
+++ b/tests/validators/tests.py
@@ -226,9 +226,15 @@ TEST_DATA = [
(URLValidator(), None, ValidationError),
(URLValidator(), 56, ValidationError),
(URLValidator(), 'no_scheme', ValidationError),
- # Trailing newlines not accepted
+ # Newlines and tabs are not accepted.
(URLValidator(), 'http://www.djangoproject.com/\n', ValidationError),
(URLValidator(), 'http://[::ffff:192.9.5.5]\n', ValidationError),
+ (URLValidator(), 'http://www.djangoproject.com/\r', ValidationError),
+ (URLValidator(), 'http://[::ffff:192.9.5.5]\r', ValidationError),
+ (URLValidator(), 'http://www.django\rproject.com/', ValidationError),
+ (URLValidator(), 'http://[::\rffff:192.9.5.5]', ValidationError),
+ (URLValidator(), 'http://\twww.djangoproject.com/', ValidationError),
+ (URLValidator(), 'http://\t[::ffff:192.9.5.5]', ValidationError),
# Trailing junk does not take forever to reject
(URLValidator(), 'http://www.asdasdasdasdsadfm.com.br ', ValidationError),
(URLValidator(), 'http://www.asdasdasdasdsadfm.com.br z', ValidationError),
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ZoeDong/python-django.git
git@gitee.com:ZoeDong/python-django.git
ZoeDong
python-django
python-django
master

搜索帮助