Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit c7294614795a3c0b3a872707bbea93733ef2077d 1 parent 99edbe0
Horst Gutmann authored February 24, 2013
19  django/contrib/sites/models.py
... ...
@@ -1,12 +1,28 @@
  1
+import string
  2
+
1 3
 from django.db import models
2 4
 from django.db.models.signals import pre_save, pre_delete
3 5
 from django.utils.translation import ugettext_lazy as _
4 6
 from django.utils.encoding import python_2_unicode_compatible
  7
+from django.core.exceptions import ValidationError
5 8
 
6 9
 
7 10
 SITE_CACHE = {}
8 11
 
9 12
 
  13
+def _simple_domain_name_validator(value):
  14
+    """
  15
+    Validates that the given value contains no whitespaces to prevent common
  16
+    typos.
  17
+    """
  18
+    if not value:
  19
+        return
  20
+    checks = ((s in value) for s in string.whitespace)
  21
+    if any(checks):
  22
+        raise ValidationError(
  23
+            _(u"The domain name cannot contain any spaces or tabs."))
  24
+
  25
+
10 26
 class SiteManager(models.Manager):
11 27
 
12 28
     def get_current(self):
@@ -37,7 +53,8 @@ def clear_cache(self):
37 53
 @python_2_unicode_compatible
38 54
 class Site(models.Model):
39 55
 
40  
-    domain = models.CharField(_('domain name'), max_length=100)
  56
+    domain = models.CharField(_('domain name'), max_length=100,
  57
+        validators=[_simple_domain_name_validator])
41 58
     name = models.CharField(_('display name'), max_length=50)
42 59
     objects = SiteManager()
43 60
 
12  django/contrib/sites/tests.py
@@ -2,7 +2,7 @@
2 2
 
3 3
 from django.conf import settings
4 4
 from django.contrib.sites.models import Site, RequestSite, get_current_site
5  
-from django.core.exceptions import ObjectDoesNotExist
  5
+from django.core.exceptions import ObjectDoesNotExist, ValidationError
6 6
 from django.http import HttpRequest
7 7
 from django.test import TestCase
8 8
 from django.test.utils import override_settings
@@ -71,3 +71,13 @@ def test_get_current_site(self):
71 71
         site = get_current_site(request)
72 72
         self.assertTrue(isinstance(site, RequestSite))
73 73
         self.assertEqual(site.name, "example.com")
  74
+
  75
+    def test_domain_name_with_whitespaces(self):
  76
+        # Regression for #17320
  77
+        # Domain names are not allowed contain whitespace characters
  78
+        site = Site(name="test name", domain="test test")
  79
+        self.assertRaises(ValidationError, site.full_clean)
  80
+        site.domain = "test\ttest"
  81
+        self.assertRaises(ValidationError, site.full_clean)
  82
+        site.domain = "test\ntest"
  83
+        self.assertRaises(ValidationError, site.full_clean)

0 notes on commit c729461

Please sign in to comment.
Something went wrong with that request. Please try again.