fix(nselib/url): repair parse_query, broken since "Simplify" refactor

parse_query passed `query` as the gsub pattern and " " as the count
argument (`query:gsub(query, "%+", " ")`), so every call raised
"bad argument #3 to 'gsub' (number expected, got string)" under the
bundled Lua 5.4, breaking all callers (http-open-redirect,
http-sql-injection, http-rfi-spider, http-unsafe-output-escaping).

Restore the intended `+`-to-space decoding (`query:gsub("%+", " ")`)
and add parse_query unit tests.

Regression introduced in 9371dd1c6 ("Simplify url.parse_query").
This commit is contained in:
toor11 2026-06-02 00:08:13 +02:00 • committed by Juri
parent 8c9d5655da
commit dd3e040172
2 changed files with 20 additions and 1 deletions

View file

@ -88,6 +88,10 @@ o Fixed an issue where Nmap OS scan trusts a packet's ip_len to size a CRC32
computation over TCP RST payload data, which may result in reading arbitrary
heap data. Reported by Michael Bommarito.
o [NSE] Fixed url.parse_query, which raised a Lua error on every call due to
passing the query string as the gsub pattern. Plus-to-space decoding is
restored.
o [Ncat][GH#3005] Correctly report listening port number. `ncat -l -p 0` will
cause Ncat to listen on an ephemeral port, but it was reporting itself as
listening on port 0.

View file

@ -379,7 +379,7 @@ function parse_query(query)
query = string.gsub(query, "&([ampltg]+);", entities)
local parsed = {}
for qseg in query:gsub(query, "%+", " "):gmatch("[^&]+") do
for qseg in query:gsub("%+", " "):gmatch("[^&]+") do
local k, v = qseg:match("^([^=]*)=?(.*)")
parsed[unescape(k)] = unescape(v)
end
@ -512,6 +512,21 @@ for _, t in ipairs(test_urls) do
test_suite:add_test(unittest.equal(build(result), t._url), "parse/build round trip")
end
-- parse_query tests. {query, expected name/value table}
local parse_query_tests = {
{"foo=1&bar=2", {foo = "1", bar = "2"}},
{"a+b=c+d", {["a b"] = "c d"}},
{"a%20b=c%26d", {["a b"] = "c&d"}},
}
for k, v in ipairs(parse_query_tests) do
local query, expected = table.unpack(v)
local result = parse_query(query)
for name, value in pairs(expected) do
test_suite:add_test(unittest.equal(result[name], value),
("parse_query #%d (%q) %q"):format(k, query, name))
end
end
-- path merging tests for compliance with RFC 3986, section 5.2
-- https://tools.ietf.org/html/rfc3986#section-5.2