Skip to content

Commit

Permalink
fix(polls) improve message validation
Browse files Browse the repository at this point in the history
- Prevent creation of too many polls
- Discard absurdly large payloads
  • Loading branch information
saghul committed Aug 9, 2024
1 parent ce22adf commit 976f70e
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions resources/prosody-plugins/mod_polls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ local muc = module:depends("muc");
local NS_NICK = 'http://jabber.org/protocol/nick';
local is_healthcheck_room = util.is_healthcheck_room;

local POLLS_LIMIT = 128;
local POLL_PAYLOAD_LIMIT = 1024;

-- Checks if the given stanza contains a JSON message,
-- and that the message type pertains to the polls feature.
-- If yes, returns the parsed message. Otherwise, returns nil.
Expand All @@ -22,6 +25,10 @@ local function get_poll_message(stanza)
if json_data == nil then
return nil;
end
if string.len(json_data) >= POLL_PAYLOAD_LIMIT then
module:log('error', 'Poll payload too large, discarding. Sender: %s', stanza.attr.from);
return nil;
end
local data, error = json.decode(json_data);
if not data or (data.type ~= "new-poll" and data.type ~= "answer-poll") then
if error then
Expand Down Expand Up @@ -72,6 +79,7 @@ module:hook("muc-room-created", function(event)
room.polls = {
by_id = {};
order = {};
count = 0;
};
end);

Expand Down Expand Up @@ -100,6 +108,11 @@ module:hook("message/bare", function(event)
return
end

if room.polls.count >= POLLS_LIMIT then
module:log("error", "Too many polls created in %s", room.jid)
return
end

local answers = {}
local compact_answers = {}
for i, name in ipairs(data.answers) do
Expand All @@ -117,6 +130,7 @@ module:hook("message/bare", function(event)

room.polls.by_id[data.pollId] = poll
table.insert(room.polls.order, poll)
room.polls.count = room.polls.count + 1;

local pollData = {
event = event,
Expand Down

0 comments on commit 976f70e

Please sign in to comment.