fix(nselib/url): drop incomplete HTML entity decoding in parse_query

parse_query opportunistically decoded &, < and > before
splitting the query string. The decoding was undocumented and
incomplete (only three named entities, no decimal/hex references), so
inputs such as "kt;=11<=22" were mis-split into a single pair
instead of two. Remove the entity decoding (and the now-unused
`entities` table); callers that need HTML decoding should do it
explicitly. Add a regression test.

Closes #3324
This commit is contained in:
toor11 2026-06-02 00:10:51 +02:00 • committed by Juri
parent dd3e040172
commit c60d492ef4
2 changed files with 6 additions and 11 deletions

View file

@ -92,6 +92,10 @@ 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 [NSE][GH#3324] url.parse_query no longer performs incomplete HTML entity
decoding (& < >) that could mis-split query strings. Callers that
need HTML decoding should do it explicitly.
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

@ -354,11 +354,6 @@ function build_path(parsed, unsafe)
return table.concat(path)
end
local entities = {
["amp"] = "&",
["lt"] = "<",
["gt"] = ">"
}
---
-- Breaks a query string into name/value pairs.
--
@ -372,12 +367,6 @@ local entities = {
-- <code>table["name"]</code> = <code>value</code>.
-----------------------------------------------------------------------------
function parse_query(query)
-- TODO: https://github.com/nmap/nmap/issues/3324
-- This opportunistic HTML decoding is problematic because
-- it might accidentally decode what should not be decoded.
query = string.gsub(query, "&([ampltg]+);", entities)
local parsed = {}
for qseg in query:gsub("%+", " "):gmatch("[^&]+") do
local k, v = qseg:match("^([^=]*)=?(.*)")
@ -517,6 +506,8 @@ 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"}},
-- names/values must not be HTML-entity-decoded (#3324)
{"kt;=11&lt;=22", {["kt;"] = "11", ["lt;"] = "22"}},
}
for k, v in ipairs(parse_query_tests) do
local query, expected = table.unpack(v)