luci-lib-httpclient: prevent nil access with invalid IP address literals

Fixes: a54baf7be ("luci-lib-httpclient: revamp URL handling")
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2018-12-11 09:17:42 +01:00
parent a54baf7be0
commit a692ca789a

View file

@ -108,20 +108,20 @@ function parse_url(uri)
url.host, tmp = rest:match("^%[([0-9a-fA-F:]+)%](.*)$") url.host, tmp = rest:match("^%[([0-9a-fA-F:]+)%](.*)$")
if url.host and tmp then if url.host and tmp then
url.ip6addr = ip.IPv6(url.host) url.ip6addr = ip.IPv6(url.host)
url.host = string.format("[%s]", url.ip6addr:string())
rest = tmp
if not url.ip6addr then if not url.ip6addr then
return nil return nil
end end
url.host = string.format("[%s]", url.ip6addr:string())
rest = tmp
else else
url.host, tmp = rest:match("^(%d+%.%d+%.%d+%.%d+)(.*)$") url.host, tmp = rest:match("^(%d+%.%d+%.%d+%.%d+)(.*)$")
if url.host and tmp then if url.host and tmp then
url.ipaddr = ip.IPv4(url.host) url.ipaddr = ip.IPv4(url.host)
url.host = url.ipaddr:string()
rest = tmp
if not url.ipaddr then if not url.ipaddr then
return nil return nil
end end
url.host = url.ipaddr:string()
rest = tmp
else else
url.host, tmp = rest:match("^([0-9a-zA-Z%.%-]+)(.*)$") url.host, tmp = rest:match("^([0-9a-zA-Z%.%-]+)(.*)$")
if url.host and tmp then if url.host and tmp then