Skip to content

Commit

Permalink
Catch error when adding netbox with invalid ip
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed Nov 28, 2023
1 parent cce6311 commit b2a8af8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion python/nav/web/seeddb/page/netbox/edit.py
Expand Up @@ -243,7 +243,7 @@ def get_sysname(ip_address):
try:
_, sysname = resolve_ip_and_sysname(ip_address)
return sysname
except SocketError:
except (SocketError, UnicodeError):

Check warning on line 246 in python/nav/web/seeddb/page/netbox/edit.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/page/netbox/edit.py#L246

Added line #L246 was not covered by tests
return None


Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/seeddb/page/netbox/forms.py
Expand Up @@ -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)

Expand Down
21 changes: 21 additions & 0 deletions tests/integration/seeddb_test.py
Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit b2a8af8

Please sign in to comment.