Przeglądaj źródła

Fixes #4510: Enforce address family for device primary IPv4/v6 addresses

Jeremy Stretch 5 lat temu
rodzic
commit
ada55dfdfb
2 zmienionych plików z 11 dodań i 4 usunięć
  1. 1 0
      docs/release-notes/version-2.8.md
  2. 10 4
      netbox/dcim/models/__init__.py

+ 1 - 0
docs/release-notes/version-2.8.md

@@ -14,6 +14,7 @@
 * [#4388](https://github.com/netbox-community/netbox/issues/4388) - Fix detection of connected endpoints when connecting rear ports
 * [#4489](https://github.com/netbox-community/netbox/issues/4489) - Fix display of parent/child role on device type view
 * [#4496](https://github.com/netbox-community/netbox/issues/4496) - Fix exception when validating certain models via REST API
+* [#4510](https://github.com/netbox-community/netbox/issues/4510) - Enforce address family for device primary IPv4/v6 addresses
 
 ---
 

+ 10 - 4
netbox/dcim/models/__init__.py

@@ -1514,24 +1514,30 @@ class Device(ChangeLoggedModel, ConfigContextModel, CustomFieldModel):
         # Validate primary IP addresses
         vc_interfaces = self.vc_interfaces.all()
         if self.primary_ip4:
+            if self.primary_ip4.family != 4:
+                raise ValidationError({
+                    'primary_ip4': f"{self.primary_ip4} is not an IPv4 address."
+                })
             if self.primary_ip4.interface in vc_interfaces:
                 pass
             elif self.primary_ip4.nat_inside is not None and self.primary_ip4.nat_inside.interface in vc_interfaces:
                 pass
             else:
                 raise ValidationError({
-                    'primary_ip4': "The specified IP address ({}) is not assigned to this device.".format(
-                        self.primary_ip4),
+                    'primary_ip4': f"The specified IP address ({self.primary_ip4}) is not assigned to this device."
                 })
         if self.primary_ip6:
+            if self.primary_ip6.family != 6:
+                raise ValidationError({
+                    'primary_ip6': f"{self.primary_ip4} is not an IPv6 address."
+                })
             if self.primary_ip6.interface in vc_interfaces:
                 pass
             elif self.primary_ip6.nat_inside is not None and self.primary_ip6.nat_inside.interface in vc_interfaces:
                 pass
             else:
                 raise ValidationError({
-                    'primary_ip6': "The specified IP address ({}) is not assigned to this device.".format(
-                        self.primary_ip6),
+                    'primary_ip6': f"The specified IP address ({self.primary_ip6}) is not assigned to this device."
                 })
 
         # Validate manufacturer/platform