Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #17320 -- Added whitespace validation to the Site.domain field

  • Loading branch information...
commit c7294614795a3c0b3a872707bbea93733ef2077d 1 parent 99edbe0
Horst Gutmann zerok authored
Showing with 29 additions and 2 deletions.
  1. +18 −1 django/contrib/sites/models.py
  2. +11 −1 django/contrib/sites/tests.py
19 django/contrib/sites/models.py
View
@@ -1,12 +1,28 @@
+import string
+
from django.db import models
from django.db.models.signals import pre_save, pre_delete
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import python_2_unicode_compatible
+from django.core.exceptions import ValidationError
SITE_CACHE = {}
+def _simple_domain_name_validator(value):
+ """
+ Validates that the given value contains no whitespaces to prevent common
+ typos.
+ """
+ if not value:
+ return
+ checks = ((s in value) for s in string.whitespace)
+ if any(checks):
+ raise ValidationError(
+ _(u"The domain name cannot contain any spaces or tabs."))
+
+
class SiteManager(models.Manager):
def get_current(self):
@@ -37,7 +53,8 @@ def clear_cache(self):
@python_2_unicode_compatible
class Site(models.Model):
- domain = models.CharField(_('domain name'), max_length=100)
+ domain = models.CharField(_('domain name'), max_length=100,
+ validators=[_simple_domain_name_validator])
name = models.CharField(_('display name'), max_length=50)
objects = SiteManager()
12 django/contrib/sites/tests.py
View
@@ -2,7 +2,7 @@
from django.conf import settings
from django.contrib.sites.models import Site, RequestSite, get_current_site
-from django.core.exceptions import ObjectDoesNotExist
+from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.http import HttpRequest
from django.test import TestCase
from django.test.utils import override_settings
@@ -71,3 +71,13 @@ def test_get_current_site(self):
site = get_current_site(request)
self.assertTrue(isinstance(site, RequestSite))
self.assertEqual(site.name, "example.com")
+
+ def test_domain_name_with_whitespaces(self):
+ # Regression for #17320
+ # Domain names are not allowed contain whitespace characters
+ site = Site(name="test name", domain="test test")
+ self.assertRaises(ValidationError, site.full_clean)
+ site.domain = "test\ttest"
+ self.assertRaises(ValidationError, site.full_clean)
+ site.domain = "test\ntest"
+ self.assertRaises(ValidationError, site.full_clean)
Please sign in to comment.
Something went wrong with that request. Please try again.