From dd3e040172bdc8b80da7adcbb5837e5bf434cf30 Mon Sep 17 00:00:00 2001 From: toor11 Date: Tue, 2 Jun 2026 00:08:13 +0200 Subject: [PATCH] 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"). --- CHANGELOG | 4 ++++ nselib/url.lua | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 9b5fe9ed5..15daddee0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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. diff --git a/nselib/url.lua b/nselib/url.lua index 0b3530c62..1a5ff5f62 100644 --- a/nselib/url.lua +++ b/nselib/url.lua @@ -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