1
0
mirror of https://github.com/pi-hole/pi-hole synced 2024-12-22 14:58:08 +00:00

Merge pull request #4083 from jbzdarkid/patch-2

Improve regexes for IPv4 matching
This commit is contained in:
Adam Warner 2021-04-12 21:12:20 +01:00 committed by GitHub
commit c6e3805bbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 34 deletions

View File

@ -1018,9 +1018,11 @@ valid_ip() {
local stat=1 local stat=1
# Regex matching one IPv4 component, i.e. an integer from 0 to 255. # Regex matching one IPv4 component, i.e. an integer from 0 to 255.
# See https://tools.ietf.org/html/rfc1340
local ipv4elem="(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)"; local ipv4elem="(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)";
# Regex matching an optional port beginning with # matching optional port number starting '#' with range of 1 to 65536 # Regex matching an optional port beginning with : from 0 to 65535
local portelem="(#([1-9]|[1-8][0-9]|9[0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-9]|[1-8][0-9]{3}|9[0-8][0-9]{2}|99[0-8][0-9]|999[0-9]|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-6]))?" # See https://tools.ietf.org/html/rfc1340#page-33
local portelem="(:(6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{0,3}|0))?";
# Build a full IPv4 regex from the above subexpressions # Build a full IPv4 regex from the above subexpressions
local regex="^${ipv4elem}\.${ipv4elem}\.${ipv4elem}\.${ipv4elem}${portelem}$" local regex="^${ipv4elem}\.${ipv4elem}\.${ipv4elem}\.${ipv4elem}${portelem}$"

View File

@ -524,43 +524,45 @@ def test_IPv6_ULA_GUA_test(Pihole):
assert expected_stdout in detectPlatform.stdout assert expected_stdout in detectPlatform.stdout
def test_validate_ip_valid(Pihole): def test_validate_ip(Pihole):
''' '''
Given a valid IP address, valid_ip returns success Tests valid_ip for various IP addresses
''' '''
def test_address(addr, success=True):
output = Pihole.run(''' output = Pihole.run('''
source /opt/pihole/basic-install.sh source /opt/pihole/basic-install.sh
valid_ip "192.168.1.1" valid_ip "{addr}"
''') '''.format(addr=addr))
assert output.rc == 0 assert output.rc == 0 if success else 1
test_address('192.168.1.1')
def test_validate_ip_invalid_octet(Pihole): test_address('127.0.0.1')
''' test_address('255.255.255.255')
Given an invalid IP address (large octet), valid_ip returns an error test_address('255.255.255.256', False)
''' test_address('255.255.256.255', False)
test_address('255.256.255.255', False)
output = Pihole.run(''' test_address('256.255.255.255', False)
source /opt/pihole/basic-install.sh test_address('1092.168.1.1', False)
valid_ip "1092.168.1.1" test_address('not an IP', False)
''') test_address('8.8.8.8:', False)
test_address('8.8.8.8:0')
assert output.rc == 1 test_address('8.8.8.8:1')
test_address('8.8.8.8:42')
test_address('8.8.8.8:888')
def test_validate_ip_invalid_letters(Pihole): test_address('8.8.8.8:1337')
''' test_address('8.8.8.8:65535')
Given an invalid IP address (contains letters), valid_ip returns an error test_address('8.8.8.8:65536', False)
''' test_address('8.8.8.8:-1', False)
test_address('00.0.0.0', False)
output = Pihole.run(''' test_address('010.0.0.0', False)
source /opt/pihole/basic-install.sh test_address('001.0.0.0', False)
valid_ip "not an IP" test_address('0.0.0.0:00', False)
''') test_address('0.0.0.0:01', False)
test_address('0.0.0.0:001', False)
assert output.rc == 1 test_address('0.0.0.0:0001', False)
test_address('0.0.0.0:00001', False)
def test_os_check_fails(Pihole): def test_os_check_fails(Pihole):