# HG changeset patch # User Matthew Wild # Date 1536852948 -3600 # Node ID feaef6215bb845c79963b0b3d0f9da6f6b3fabbb # Parent 35c128b42509dac298f6554baee1a9571dc1d9df util.stanza: Don't automatically generate ids for iq stanzas Users of this API should provide their own id attribute that meets their uniqueness requirements. The current implementation leaks information (i.e. how many iq stanzas have been sent by the server to other JIDs). Providing any strong guarantees of randomness here would need to pull in additional dependencies that we don't want in this simple library. diff -r 35c128b42509 -r feaef6215bb8 spec/util_stanza_spec.lua --- a/spec/util_stanza_spec.lua Thu Sep 13 17:28:50 2018 +0200 +++ b/spec/util_stanza_spec.lua Thu Sep 13 16:35:48 2018 +0100 @@ -84,9 +84,20 @@ end); describe("#iq()", function() - it("should work", function() - local i = st.iq(); - assert.are.equal(i.name, "iq"); + it("should create an iq stanza", function() + local i = st.iq({ id = "foo" }); + assert.are.equal("iq", i.name); + assert.are.equal("foo", i.attr.id); + end); + + it("should reject stanzas with no id", function () + assert.has.error_match(function () + local i = st.iq(); + end, "id attribute"); + + assert.has.error_match(function () + local i = st.iq({ foo = "bar" }); + end, "id attribute"); end); end); diff -r 35c128b42509 -r feaef6215bb8 util/stanza.lua --- a/util/stanza.lua Thu Sep 13 17:28:50 2018 +0200 +++ b/util/stanza.lua Thu Sep 13 16:35:48 2018 +0100 @@ -347,12 +347,6 @@ return error_type, condition or "undefined-condition", text; end -local id = 0; -local function new_id() - id = id + 1; - return "lx"..id; -end - local function preserialize(stanza) local s = { name = stanza.name, attr = stanza.attr }; for _, child in ipairs(stanza) do @@ -430,8 +424,10 @@ end end local function iq(attr) - if attr and not attr.id then attr.id = new_id(); end - return new_stanza("iq", attr or { id = new_id() }); + if not (attr and attr.id) then + error("iq stanzas require an id attribute"); + end + return new_stanza("iq", attr); end local function reply(orig) @@ -502,7 +498,6 @@ stanza_mt = stanza_mt; stanza = new_stanza; is_stanza = is_stanza; - new_id = new_id; preserialize = preserialize; deserialize = deserialize; clone = clone;