From c60d492ef483703f54da8e49ee7bf3b32c80e527 Mon Sep 17 00:00:00 2001 From: toor11 Date: Tue, 2 Jun 2026 00:10:51 +0200 Subject: [PATCH] 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 --- CHANGELOG | 4 ++++ nselib/url.lua | 13 ++----------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 15daddee0..f1de75fd9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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. diff --git a/nselib/url.lua b/nselib/url.lua index 1a5ff5f62..01411626c 100644 --- a/nselib/url.lua +++ b/nselib/url.lua @@ -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 = { -- table["name"] = value. ----------------------------------------------------------------------------- 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<=22", {["kt;"] = "11", ["lt;"] = "22"}}, } for k, v in ipairs(parse_query_tests) do local query, expected = table.unpack(v)