Skip to content

Commit 0032822

Browse files
committed
Fixes #7051: Fix permissions evaluation and improve error handling for connected device REST API endpoint
1 parent b31ba4e commit 0032822

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

docs/release-notes/version-3.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
### Bug Fixes
1111

12+
* [#7051](https://github.com/netbox-community/netbox/issues/7051) - Fix permissions evaluation and improve error handling for connected device REST API endpoint
1213
* [#7471](https://github.com/netbox-community/netbox/issues/7471) - Correct redirect URL when attaching images via "add another" button
1314
* [#7474](https://github.com/netbox-community/netbox/issues/7474) - Fix AttributeError exception when rendering a report or custom script
1415

netbox/dcim/api/views.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from collections import OrderedDict
33

44
from django.conf import settings
5-
from django.http import HttpResponseForbidden, HttpResponse
5+
from django.http import Http404, HttpResponse, HttpResponseForbidden
66
from django.shortcuts import get_object_or_404
77
from drf_yasg import openapi
88
from drf_yasg.openapi import Parameter
@@ -17,10 +17,10 @@
1717
from dcim.models import *
1818
from extras.api.views import ConfigContextQuerySetMixin, CustomFieldModelViewSet
1919
from ipam.models import Prefix, VLAN
20-
from netbox.api.views import ModelViewSet
2120
from netbox.api.authentication import IsAuthenticatedOrLoginNotRequired
2221
from netbox.api.exceptions import ServiceUnavailable
2322
from netbox.api.metadata import ContentTypeMetadata
23+
from netbox.api.views import ModelViewSet
2424
from utilities.api import get_serializer_for_model
2525
from utilities.utils import count_related, decode_dict
2626
from virtualization.models import VirtualMachine
@@ -675,15 +675,25 @@ def list(self, request):
675675
if not peer_device_name or not peer_interface_name:
676676
raise MissingFilterException(detail='Request must include "peer_device" and "peer_interface" filters.')
677677

678-
# Determine local interface from peer interface's connection
678+
# Determine local endpoint from peer interface's connection
679+
peer_device = get_object_or_404(
680+
Device.objects.restrict(request.user, 'view'),
681+
name=peer_device_name
682+
)
679683
peer_interface = get_object_or_404(
680-
Interface.objects.all(),
681-
device__name=peer_device_name,
684+
Interface.objects.restrict(request.user, 'view'),
685+
device=peer_device,
682686
name=peer_interface_name
683687
)
684-
local_interface = peer_interface.connected_endpoint
688+
endpoint = peer_interface.connected_endpoint
685689

686-
if local_interface is None:
687-
return Response()
690+
# If an Interface, return the parent device
691+
if type(endpoint) is Interface:
692+
device = get_object_or_404(
693+
Device.objects.restrict(request.user, 'view'),
694+
pk=endpoint.device_id
695+
)
696+
return Response(serializers.DeviceSerializer(device, context={'request': request}).data)
688697

689-
return Response(serializers.DeviceSerializer(local_interface.device, context={'request': request}).data)
698+
# Connected endpoint is none or not an Interface
699+
raise Http404

0 commit comments

Comments
 (0)