Skip to content
This repository has been archived by the owner on Jan 31, 2019. It is now read-only.

Commit

Permalink
Don't allow private IP addresses (bug 638559)
Browse files Browse the repository at this point in the history
Throw a validation error if a private IP is used in the URL field.
  • Loading branch information
tofumatt committed Jun 3, 2011
1 parent bbe1e26 commit 8204c7d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions apps/feedback/forms.py
Expand Up @@ -12,6 +12,7 @@
from feedback.models import Opinion
from feedback.validators import (validate_swearwords, validate_no_html,
validate_no_email, validate_no_urls,
validate_no_private_ips,
ExtendedURLValidator)


Expand All @@ -24,6 +25,7 @@ def __init__(self, *args, **kwargs):
# Remove old URL validator, add ours instead.
self.validators = filter(lambda x: not isinstance(x, URLValidator),
self.validators)
self.validators.append(validate_no_private_ips)
self.validators.append(ExtendedURLValidator())

def to_python(self, value):
Expand Down
21 changes: 20 additions & 1 deletion apps/feedback/tests/test_validators.py
Expand Up @@ -2,7 +2,8 @@

import test_utils

from feedback.validators import validate_no_urls, ExtendedURLValidator
from feedback.validators import (validate_no_urls, validate_no_private_ips,
ExtendedURLValidator)


class ValidatorTests(test_utils.TestCase):
Expand All @@ -18,6 +19,24 @@ def test_chrome_url(self):
self.assertRaises(ValidationError, v, 'about:')
self.assertRaises(ValidationError, v, 'chrome:bogus')

def test_private_ips_not_allowed(self):
"""Make sure private IPs can't be submitted as URLs."""
patterns = (
('https://mozilla.com', False),
('http://tofumatt.com', False),
('youtube.com', False),
('0.0.0.0', True),
('http://127.0.0.1', True),
('HTTP://10.0.0.13', True),
('https://192.168.0.4', True),
)
for pattern in patterns:
if pattern[1]:
self.assertRaises(ValidationError, validate_no_private_ips,
pattern[0])
else:
validate_no_private_ips(pattern[0])

def test_url_in_text(self):
"""Find URLs in text."""
patterns = (
Expand Down
18 changes: 18 additions & 0 deletions apps/feedback/validators.py
Expand Up @@ -14,6 +14,17 @@
# Simple email regex to keep people from submitting personal data.
EMAIL_RE = re.compile(r'[^\s]+@[^\s]+\.[^\s]{2,6}')

# IPv4 Address verifier
PRIVATE_IP_RE = re.compile(
r'^(https?://)?' # http:// or https://
r'(?:0\.0\.0\.0)|'
r'(?:127\.0)|'
r'(?:10\.)|'
r'(?:172\.1[6-9]\.)|'
r'(?:172\.2[0-9]\.)|'
r'(?:172\.3[0-1]\.)|'
r'(?:192\.168\.)', re.IGNORECASE)

# Simple "possibly a URL" regex
URL_RE = re.compile(r'(://|www\.[^\s]|\.\w{2,}/)')

Expand Down Expand Up @@ -45,6 +56,13 @@ def validate_no_email(str):
'Thanks!'))


def validate_no_private_ips(str):
"""Disallow private IPv4 IPs from being submitted."""
if PRIVATE_IP_RE.search(str):
raise ValidationError(
_('URLs with IP addresses must contain public IP addresses.'))


def validate_no_urls(str):
"""Disallow text possibly containing a URL."""
if URL_RE.search(str):
Expand Down

0 comments on commit 8204c7d

Please sign in to comment.