From 50f63355017b033f4534f6012c796c7b5241a2d5 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 17 Nov 2018 15:26:11 +0100 Subject: util.stanza: Validate input to clone() (with brief tests) --- spec/util_stanza_spec.lua | 14 ++++++++++++++ util/stanza.lua | 11 +++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/spec/util_stanza_spec.lua b/spec/util_stanza_spec.lua index 7eaadfe6..7fe836c7 100644 --- a/spec/util_stanza_spec.lua +++ b/spec/util_stanza_spec.lua @@ -346,4 +346,18 @@ describe("util.stanza", function() end, "Invalid stanza"); end); end); + + describe("#clone", function () + it("works", function () + local s = st.message({type="chat"}, "Hello"):reset(); + local c = st.clone(s); + assert.same(s, c); + end); + + it("works", function () + assert.has_error(function () + st.clone("this is not a stanza"); + end); + end); + end); end); diff --git a/util/stanza.lua b/util/stanza.lua index 85c89d43..8d199912 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -398,7 +398,7 @@ local function deserialize(stanza) return stanza; end -local function clone(stanza) +local function _clone(stanza) local attr, tags = {}, {}; for k,v in pairs(stanza.attr) do attr[k] = v; end local old_namespaces, namespaces = stanza.namespaces; @@ -410,7 +410,7 @@ local function clone(stanza) for i=1,#stanza do local child = stanza[i]; if child.name then - child = clone(child); + child = _clone(child); t_insert(tags, child); end t_insert(new, child); @@ -418,6 +418,13 @@ local function clone(stanza) return setmetatable(new, stanza_mt); end +local function clone(stanza) + if not is_stanza(stanza) then + error("bad argument to clone: expected stanza, got "..type(stanza)); + end + return _clone(stanza); +end + local function message(attr, body) if not body then return new_stanza("message", attr); -- cgit v1.2.3 From 23be1afef652d71745c7b0aa7a9786bad95e3f3f Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 17 Nov 2018 15:28:49 +0100 Subject: mod_csi_simple: Skip delay tags on objects other than stanzas (thanks quest) This may be triggered by sending strings, eg as done by mod_c2s for keepalives, stream errors, "". --- plugins/mod_csi_simple.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/mod_csi_simple.lua b/plugins/mod_csi_simple.lua index 1535edae..6fc88291 100644 --- a/plugins/mod_csi_simple.lua +++ b/plugins/mod_csi_simple.lua @@ -82,8 +82,10 @@ module:hook("csi-client-inactive", function (event) pump:flush(); send(stanza); else - stanza = st.clone(stanza); - stanza:add_direct_child(st.stanza("delay", {xmlns = "urn:xmpp:delay", from = bare_jid, stamp = dt.datetime()})); + if st.is_stanza(stanza) then + stanza = st.clone(stanza); + stanza:add_direct_child(st.stanza("delay", {xmlns = "urn:xmpp:delay", from = bare_jid, stamp = dt.datetime()})); + end pump:push(stanza); end return true; -- cgit v1.2.3 From 3d3e0fa0826c0d107a7b99d875190ba4421f668a Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 17 Nov 2018 15:36:10 +0100 Subject: mod_csi_simple: Consider non-stanza objects important Most cases are for keepalive or stream closing, where it needs to be flushed anyways. --- plugins/mod_csi_simple.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/mod_csi_simple.lua b/plugins/mod_csi_simple.lua index 6fc88291..c8151df5 100644 --- a/plugins/mod_csi_simple.lua +++ b/plugins/mod_csi_simple.lua @@ -48,6 +48,9 @@ local queue_size = module:get_option_number("csi_queue_size", 256); module:hook("csi-is-stanza-important", function (event) local stanza = event.stanza; + if not st.is_stanza(stanza) then + return true; + end local st_name = stanza.name; if not st_name then return false; end local st_type = stanza.attr.type; -- cgit v1.2.3