Skip to content

Commit ec4e719

Browse files
committed
allow leading zeros in ipv4 addresses
1 parent 426ff3a commit ec4e719

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

synapse/models/inet.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,16 @@ def _normPyStr(self, text):
242242

243243
try:
244244
byts = socket.inet_pton(socket.AF_INET, valu)
245-
addr = (4, int.from_bytes(byts, 'big'))
246-
ipaddr = ipaddress.IPv4Address(addr[1])
247-
subs['version'] = 4
248-
except OSError as e:
249-
mesg = f'Invalid IP address: {text}'
250-
raise s_exc.BadTypeValu(mesg=mesg) from None
245+
except OSError:
246+
try:
247+
byts = socket.inet_aton(valu)
248+
except OSError as e:
249+
mesg = f'Invalid IP address: {text}'
250+
raise s_exc.BadTypeValu(mesg=mesg) from None
251+
252+
addr = (4, int.from_bytes(byts, 'big'))
253+
ipaddr = ipaddress.IPv4Address(addr[1])
254+
subs['version'] = 4
251255

252256
subs['type'] = getAddrType(ipaddr)
253257

synapse/tests/test_model_inet.py

+18
Original file line numberDiff line numberDiff line change
@@ -3070,3 +3070,21 @@ async def test_model_inet_service(self):
30703070
self.len(1, await core.nodes('inet:service:subscription -> inet:service:subscription:level:taxonomy'))
30713071
self.len(1, await core.nodes('inet:service:subscription :pay:instrument -> econ:bank:account'))
30723072
self.len(1, await core.nodes('inet:service:subscription :subscriber -> inet:service:tenant'))
3073+
3074+
async def test_ipv4_fallback(self):
3075+
3076+
async with self.getTestCore() as core:
3077+
self.len(1, await core.nodes('[inet:ip=192.168.1.1]'))
3078+
3079+
self.len(1, await core.nodes('[inet:ip=3.0.000.115]'))
3080+
self.len(1, await core.nodes('[inet:ip=192.168.001.001]'))
3081+
self.len(1, await core.nodes('[inet:ip=10.0.0.001]'))
3082+
3083+
with self.raises(s_exc.BadTypeValu):
3084+
await core.nodes('[inet:ip=256.256.256.256]')
3085+
3086+
with self.raises(s_exc.BadTypeValu):
3087+
await core.nodes('[inet:ip=192.168.001.001.001]')
3088+
3089+
with self.raises(s_exc.BadTypeValu):
3090+
await core.nodes('[inet:ip=192.168.001.001.abc]')

0 commit comments

Comments
 (0)