Skip to content

Commit

Permalink
Relax parsing of IP networks coming from the database (#22806)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
diox committed Oct 30, 2024
1 parent e4e461b commit cc7deb5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/olympia/amo/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions src/olympia/amo/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit cc7deb5

Please sign in to comment.