Skip to content

Commit cc7deb5

Browse files
committed
Relax parsing of IP networks coming from the database (#22806)
Even if technically the bits corresponding to the netmask should be set to 0, in practice it's fine to accept that they are not, it will result in the same network.
1 parent e4e461b commit cc7deb5

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/olympia/amo/fields.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ def to_python(self, value):
120120
value = value.strip()
121121

122122
try:
123-
return ipaddress.ip_network(value)
123+
# We pass strict=False to allow networks with host bits from the
124+
# database to avoid generating an exception even though they should
125+
# not have passed validation in case they were added by automation.
126+
# It should still represent the correct network anyway.
127+
return ipaddress.ip_network(value, strict=False)
124128
except Exception as exc:
125129
raise exceptions.ValidationError(exc) from exc
126130

src/olympia/amo/tests/test_fields.py

+6
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ def test_validates_ip4_cidr(self):
123123

124124
self.field.clean('127.0.0.0/28')
125125

126+
def test_to_python(self):
127+
# Technically invalid because it has the same bits set as the netmask
128+
# but we want to be able to load those from the db.
129+
assert self.field.to_python('127.0.0.1/28')
130+
assert self.field.to_python('::1/28')
131+
126132

127133
class TestIPAddressBinaryField(TestCase):
128134
def test_from_db_value(self):

0 commit comments

Comments
 (0)