diff --git a/python/nav/web/seeddb/page/netbox/edit.py b/python/nav/web/seeddb/page/netbox/edit.py index 2febc78184..209ac090cf 100644 --- a/python/nav/web/seeddb/page/netbox/edit.py +++ b/python/nav/web/seeddb/page/netbox/edit.py @@ -243,7 +243,7 @@ def get_sysname(ip_address): try: _, sysname = resolve_ip_and_sysname(ip_address) return sysname - except SocketError: + except (SocketError, UnicodeError): return None diff --git a/python/nav/web/seeddb/page/netbox/forms.py b/python/nav/web/seeddb/page/netbox/forms.py index fb882e0017..3dde54c40b 100644 --- a/python/nav/web/seeddb/page/netbox/forms.py +++ b/python/nav/web/seeddb/page/netbox/forms.py @@ -204,7 +204,7 @@ def clean_ip(self): name = self.cleaned_data['ip'].strip() try: ip, _ = resolve_ip_and_sysname(name) - except SocketError: + except (SocketError, UnicodeError): raise forms.ValidationError("Could not resolve name %s" % name) return str(ip) diff --git a/tests/integration/seeddb_test.py b/tests/integration/seeddb_test.py index 55bb0edcb7..b34f996646 100644 --- a/tests/integration/seeddb_test.py +++ b/tests/integration/seeddb_test.py @@ -5,6 +5,7 @@ from django.test.client import RequestFactory from mock import MagicMock +from nav.compatibility import smart_str from nav.models.manage import Netbox, Room from nav.web.seeddb.page.netbox.edit import netbox_edit, log_netbox_change from nav.web.seeddb.utils.delete import dependencies @@ -28,6 +29,26 @@ def test_editing_deleted_netboxes_should_raise_404(admin_account): netbox_edit(request, netboxid) +def test_adding_netbox_with_invalid_ip_should_fail(db, client): + url = reverse('seeddb-netbox-edit') + ip = "195.88.54.16'))) OR 2121=(SELECT COUNT(*) FROM GENERATE_SERIES(1,15000000)) AND ((('FRyc' LIKE 'FRyc" + + response = client.post( + url, + follow=True, + data={ + "ip": ip, + "room": "myroom", + "category": "GW", + "organization": "myorg", + }, + ) + + assert response.status_code == 200 + assert 'Form was not valid' in smart_str(response.content) + assert 'Could not resolve name' in smart_str(response.content) + + @pytest.fixture() def netbox(management_profile): box = Netbox(