Add url validation

remotes/origin/4.0rc
Álex Hermida 2018-10-09 15:53:02 +02:00 committed by Alex Hermida
parent 1a850f5179
commit 221211e716
1 changed files with 15 additions and 0 deletions

View File

@ -15,12 +15,27 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import ipaddress
from django.utils.translation import ugettext as _
from taiga.base.api import validators
from urllib.parse import urlparse
from taiga.base.exceptions import ValidationError
from .models import Webhook
class WebhookValidator(validators.ModelValidator):
class Meta:
model = Webhook
def validate_url(self, attrs, source):
host = urlparse(attrs[source]).hostname
try:
ipa = ipaddress.ip_address(host)
except ValueError:
return attrs
if ipa.is_private:
raise ValidationError(_("Not allowed IP Address"))
return attrs