# HG changeset patch
# User Kim Alvefur <zash@zash.se>
# Date 1574711523 -3600
# Node ID f53c03ab4357f81973f1ef7ae8ef3d01f63028d5
# Parent  4eab1f5a4f3b112f33c48b90ff8af3e9233bb21f
util.stanza: Check that argument to error_reply is NOT a stanza of type error

Replying to an error is Very Bad

diff -r 4eab1f5a4f3b -r f53c03ab4357 spec/util_stanza_spec.lua
--- a/spec/util_stanza_spec.lua	Mon Nov 25 20:52:01 2019 +0100
+++ b/spec/util_stanza_spec.lua	Mon Nov 25 20:52:03 2019 +0100
@@ -220,6 +220,16 @@
 				st.error_reply(not "a stanza", "modify", "bad-request");
 			end, "expected stanza");
 		end);
+
+		it("should reject stanzas of type error", function ()
+			assert.has.error_match(function ()
+				st.error_reply(st.message({type="error"}), "cancel", "conflict");
+			end, "got stanza of type error");
+			assert.has.error_match(function ()
+				st.error_reply(st.error_reply(st.message({type="chat"}), "modify", "forbidden"), "cancel", "service-unavailable");
+			end, "got stanza of type error");
+		end);
+
 	end);
 
 	describe("should reject #invalid", function ()
diff -r 4eab1f5a4f3b -r f53c03ab4357 util/stanza.lua
--- a/util/stanza.lua	Mon Nov 25 20:52:01 2019 +0100
+++ b/util/stanza.lua	Mon Nov 25 20:52:03 2019 +0100
@@ -450,6 +450,8 @@
 local function error_reply(orig, error_type, condition, error_message)
 	if not is_stanza(orig) then
 		error("bad argument to error_reply: expected stanza, got "..type(orig));
+	elseif orig.attr.type == "error" then
+		error("bad argument to error_reply: got stanza of type error which must not be replied to");
 	end
 	local t = reply(orig);
 	t.attr.type = "error";