util/filters.lua
author Matthew Wild <mwild1@gmail.com>
Mon, 31 Oct 2022 14:32:02 +0000
branch0.12
changeset 12794 24b55f0e2db9
parent 8558 4f0f5b49bb03
permissions -rw-r--r--
mod_http: Allow disabling CORS in the http_cors_override option and by default Fixes #1779. Due to an oversight in the logic, if the user set 'enabled' to false in an override, it would disable the item's requested CORS settings, but still apply Prosody's default CORS policy. This change ensures that 'enabled = false' will now disable CORS entirely for the requested item. Due to the new structure of the code, it was necessary to have a flag to say whether CORS is to be applied at all. Rather than hard-coding 'true' here, I chose to add a new option: 'http_default_cors_enabled'. This is a boolean that allows the operator to disable Prosody's default CORS policy entirely (the one that is used when a module or config does not override it). This makes it easier to disable CORS and then selectively enable it only on services you want it on.

-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--

local t_insert, t_remove = table.insert, table.remove;

local _ENV = nil;
-- luacheck: std none

local new_filter_hooks = {};

local function initialize(session)
	if not session.filters then
		local filters = {};
		session.filters = filters;

		function session.filter(type, data)
			local filter_list = filters[type];
			if filter_list then
				for i = 1, #filter_list do
					data = filter_list[i](data, session);
					if data == nil then break; end
				end
			end
			return data;
		end
	end

	for i=1,#new_filter_hooks do
		new_filter_hooks[i](session);
	end

	return session.filter;
end

local function add_filter(session, type, callback, priority)
	if not session.filters then
		initialize(session);
	end

	local filter_list = session.filters[type];
	if not filter_list then
		filter_list = {};
		session.filters[type] = filter_list;
	elseif filter_list[callback] then
		return; -- Filter already added
	end

	priority = priority or 0;

	local i = 0;
	repeat
		i = i + 1;
	until not filter_list[i] or filter_list[filter_list[i]] < priority;

	t_insert(filter_list, i, callback);
	filter_list[callback] = priority;
end

local function remove_filter(session, type, callback)
	if not session.filters then return; end
	local filter_list = session.filters[type];
	if filter_list and filter_list[callback] then
		for i=1, #filter_list do
			if filter_list[i] == callback then
				t_remove(filter_list, i);
				filter_list[callback] = nil;
				return true;
			end
		end
	end
end

local function add_filter_hook(callback)
	t_insert(new_filter_hooks, callback);
end

local function remove_filter_hook(callback)
	for i=1,#new_filter_hooks do
		if new_filter_hooks[i] == callback then
			t_remove(new_filter_hooks, i);
		end
	end
end

return {
	initialize = initialize;
	add_filter = add_filter;
	remove_filter = remove_filter;
	add_filter_hook = add_filter_hook;
	remove_filter_hook = remove_filter_hook;
};