Skip to content

Commit c446f66

Browse files
authored
Reject malformed IPv6 target ports (#6198)
1 parent 2132348 commit c446f66

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

‎lib/core/common.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,7 @@ def parseTargetUrl():
18301830
singleTimeWarnMessage("credentials in the target URL are ignored. Use '--auth-cred' for HTTP authentication")
18311831
netloc = netloc.rsplit('@', 1)[-1]
18321832

1833-
hostnamePort = netloc.split(":") if not re.search(r"\[.+\]", netloc) else filterNone((re.search(r"\[.+\]", netloc).group(0), re.search(r"\](:(?P<port>\d+))?", netloc).group("port")))
1833+
hostnamePort = netloc.split(":") if not re.search(r"\[.+\]", netloc) else re.split(r"(?<=\]):", netloc, maxsplit=1)
18341834

18351835
conf.scheme = (urlSplit.scheme.strip().lower() or "http")
18361836
conf.path = urlSplit.path.strip()

‎tests/test_targeturl.py‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727

2828
from lib.core.common import parseTargetUrl
2929
from lib.core.data import conf
30+
from lib.core.exception import SqlmapSyntaxException
3031

31-
_TARGETURL_KEYS = ("url", "hostname", "port", "scheme", "path")
32+
_TARGETURL_KEYS = ("url", "hostname", "port", "scheme", "path", "ipv6")
3233
_saved = {}
3334

3435

@@ -101,5 +102,35 @@ def test_credentials_with_ipv6(self):
101102
self.assertEqual((host, port), ("::1", 8443))
102103

103104

105+
class TestIPv6Ports(unittest.TestCase):
106+
def test_invalid_port_suffixes(self):
107+
for port in ("abc", "-1", "8080junk", "80:90", ""):
108+
for scheme in ("http", "https", "ws", "wss"):
109+
url = "%s://[::1]:%s/path?id=1" % (scheme, port)
110+
with self.assertRaises(SqlmapSyntaxException):
111+
_parse(url)
112+
113+
def test_out_of_range_ports(self):
114+
for port in (0, 65536):
115+
with self.assertRaises(SqlmapSyntaxException):
116+
_parse("http://[::1]:%s/" % port)
117+
118+
def test_valid_explicit_ports(self):
119+
for host in ("::1", "2001:db8::1", "::ffff:192.0.2.1"):
120+
for port in (1, 80, 443, 8080, 65535):
121+
parsed = _parse("http://[%s]:%d/path?id=1" % (host, port))
122+
self.assertEqual(parsed, (host, port, "http", "/path"))
123+
self.assertTrue(conf.ipv6)
124+
125+
def test_default_ports(self):
126+
for scheme, port in (("http", 80), ("https", 443), ("ws", 80), ("wss", 443)):
127+
self.assertEqual(_parse("%s://[::1]/path" % scheme), ("::1", port, scheme, "/path"))
128+
self.assertEqual(conf.url, "%s://[::1]/path" % scheme)
129+
130+
def test_userinfo_with_invalid_port(self):
131+
with self.assertRaises(SqlmapSyntaxException):
132+
_parse("http://user:pass@[::1]:8080junk/path")
133+
134+
104135
if __name__ == "__main__":
105136
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)