From d818c250b0678c53a485b066c7d4c429d9235435 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Thu, 1 Sep 2022 09:31:42 -0400 Subject: [PATCH] Fixes #10220: Validate IP version when assigning primary IPs to a virtual machine --- docs/release-notes/version-3.3.md | 1 + netbox/virtualization/models.py | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/release-notes/version-3.3.md b/docs/release-notes/version-3.3.md index 70e868c49..6049133e9 100644 --- a/docs/release-notes/version-3.3.md +++ b/docs/release-notes/version-3.3.md @@ -24,6 +24,7 @@ * [#10181](https://github.com/netbox-community/netbox/issues/10181) - Restore MultiPartParser (regression from #10031) * [#10208](https://github.com/netbox-community/netbox/issues/10208) - Fix permissions evaluation for interface actions dropdown menu * [#10217](https://github.com/netbox-community/netbox/issues/10217) - Handle exception when trace splits to multiple rear ports +* [#10220](https://github.com/netbox-community/netbox/issues/10220) - Validate IP version when assigning primary IPs to a virtual machine --- diff --git a/netbox/virtualization/models.py b/netbox/virtualization/models.py index b8131c1ce..abad57f88 100644 --- a/netbox/virtualization/models.py +++ b/netbox/virtualization/models.py @@ -368,9 +368,14 @@ class VirtualMachine(NetBoxModel, ConfigContextModel): # Validate primary IP addresses interfaces = self.interfaces.all() - for field in ['primary_ip4', 'primary_ip6']: + for family in (4, 6): + field = f'primary_ip{family}' ip = getattr(self, field) if ip is not None: + if ip.address.version != family: + raise ValidationError({ + field: f"Must be an IPv{family} address. ({ip} is an IPv{ip.address.version} address.)", + }) if ip.assigned_object in interfaces: pass elif ip.nat_inside is not None and ip.nat_inside.assigned_object in interfaces: