Escape XML special characters in OMP2 authenticate() credentials

nselib/omp2.lua built the authentication XML by concatenating the raw
username and password, so credentials containing &, <, >, " or ' could
produce malformed XML or failed logins. Add a local xml_escape() helper
and apply it to both credentials (resolving the TODO). Fixes #3311.
This commit is contained in:
Mrx 2026-09-21 02:22:07 +05:30
parent e135983b21
commit 57b1444471

View file

@ -39,6 +39,18 @@ if pcall(require,'openssl') then
HAVE_SSL = true
end
--- Escapes XML special characters in a string so it can be safely
-- embedded in XML element content.
-- @param s the string to escape
-- @return the escaped string
local function xml_escape(s)
return (s:gsub("&", "&amp;")
:gsub("<", "&lt;")
:gsub(">", "&gt;")
:gsub('"', "&quot;")
:gsub("'", "&apos;"))
end
--- A Session class holds connection and interaction with the server
Session = {
@ -74,10 +86,9 @@ Session = {
authenticate = function(self, username, password)
local status, err, xmldata
-- TODO escape credentials
status, err = self.socket:send("<authenticate><credentials>"
.. "<username>" .. username .. "</username>"
.. "<password>" .. password .. "</password>"
.. "<username>" .. xml_escape(username) .. "</username>"
.. "<password>" .. xml_escape(password) .. "</password>"
.. "</credentials></authenticate>")
if not status then