diff --git a/src/olympia/amo/fields.py b/src/olympia/amo/fields.py index ffbc251f3472..9fad7739cc86 100644 --- a/src/olympia/amo/fields.py +++ b/src/olympia/amo/fields.py @@ -120,7 +120,11 @@ def to_python(self, value): value = value.strip() try: - return ipaddress.ip_network(value) + # We pass strict=False to allow networks with host bits from the + # database to avoid generating an exception even though they should + # not have passed validation in case they were added by automation. + # It should still represent the correct network anyway. + return ipaddress.ip_network(value, strict=False) except Exception as exc: raise exceptions.ValidationError(exc) from exc diff --git a/src/olympia/amo/tests/test_fields.py b/src/olympia/amo/tests/test_fields.py index bf84836bf3ab..7cdbd014e913 100644 --- a/src/olympia/amo/tests/test_fields.py +++ b/src/olympia/amo/tests/test_fields.py @@ -123,6 +123,12 @@ def test_validates_ip4_cidr(self): self.field.clean('127.0.0.0/28') + def test_to_python(self): + # Technically invalid because it has the same bits set as the netmask + # but we want to be able to load those from the db. + assert self.field.to_python('127.0.0.1/28') + assert self.field.to_python('::1/28') + class TestIPAddressBinaryField(TestCase): def test_from_db_value(self):