From 7e83ddc96837339286f698f8f17f57f80e70b2b6 Mon Sep 17 00:00:00 2001 From: Seth Foster Date: Mon, 28 Oct 2019 13:47:01 -0400 Subject: [PATCH] Fix URLField to allow numbers in top level domain Add a custom regex to URLField that allows numbers to be present in the top level domain, e.g. https://towerhost.org42 Set by variable allow_numbers_in_top_level_domain in URLField __init__, and is set to True by default. If set to False, it will use the regex specified in the built-in django URLValidator class. This solution was originally implemented in LDAPServerURIField, but is now implemented in URLField to support this behavior more generally. The changes in LDAPServerURIField are longer needed and have been removed in this commit. Adds unit testing to make sure URLField changes handle regex input and settings correctly. --- awx/conf/fields.py | 28 +++++++++++++++++++++++++++- awx/conf/tests/unit/test_fields.py | 26 ++++++++++++++++++++++++-- awx/sso/fields.py | 23 ----------------------- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/awx/conf/fields.py b/awx/conf/fields.py index c5516b7c2e..82a1e2b8a3 100644 --- a/awx/conf/fields.py +++ b/awx/conf/fields.py @@ -1,11 +1,12 @@ # Python import os +import re import logging import urllib.parse as urlparse from collections import OrderedDict # Django -from django.core.validators import URLValidator +from django.core.validators import URLValidator, _lazy_re_compile from django.utils.translation import ugettext_lazy as _ # Django REST Framework @@ -118,17 +119,42 @@ class StringListPathField(StringListField): class URLField(CharField): + # these lines set up a custom regex that allow numbers in the + # top-level domain + tld_re = ( + r'\.' # dot + r'(?!-)' # can't start with a dash + r'(?:[a-z' + URLValidator.ul + r'0-9' + '-]{2,63}' # domain label, this line was changed from the original URLValidator + r'|xn--[a-z0-9]{1,59})' # or punycode label + r'(?