Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch error when adding netbox with invalid ip #2764

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/nav/web/seeddb/page/netbox/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading