File

spec/util_xmppstream_spec.lua @ 12642:9061f9621330

Switch to a new role-based authorization framework, removing is_admin() We began moving away from simple "is this user an admin?" permission checks before 0.12, with the introduction of mod_authz_internal and the ability to dynamically change the roles of individual users. The approach in 0.12 still had various limitations however, and apart from the introduction of roles other than "admin" and the ability to pull that info from storage, not much actually changed. This new framework shakes things up a lot, though aims to maintain the same functionality and behaviour on the surface for a default Prosody configuration. That is, if you don't take advantage of any of the new features, you shouldn't notice any change. The biggest change visible to developers is that usermanager.is_admin() (and the auth provider is_admin() method) have been removed. Gone. Completely. Permission checks should now be performed using a new module API method: module:may(action_name, context) This method accepts an action name, followed by either a JID (string) or (preferably) a table containing 'origin'/'session' and 'stanza' fields (e.g. the standard object passed to most events). It will return true if the action should be permitted, or false/nil otherwise. Modules should no longer perform permission checks based on the role name. E.g. a lot of code previously checked if the user's role was prosody:admin before permitting some action. Since many roles might now exist with similar permissions, and the permissions of prosody:admin may be redefined dynamically, it is no longer suitable to use this method for permission checks. Use module:may(). If you start an action name with ':' (recommended) then the current module's name will automatically be used as a prefix. To define a new permission, use the new module API: module:default_permission(role_name, action_name) module:default_permissions(role_name, { action_name[, action_name...] }) This grants the specified role permission to execute the named action(s) by default. This may be overridden via other mechanisms external to your module. The built-in roles that developers should use are: - prosody:user (normal user) - prosody:admin (host admin) - prosody:operator (global admin) The new prosody:operator role is intended for server-wide actions (such as shutting down Prosody). Finally, all usage of is_admin() in modules has been fixed by this commit. Some of these changes were trickier than others, but no change is expected to break existing deployments. EXCEPT: mod_auth_ldap no longer supports the ldap_admin_filter option. It's very possible nobody is using this, but if someone is then we can later update it to pull roles from LDAP somehow.
author Matthew Wild <mwild1@gmail.com>
date Wed, 15 Jun 2022 12:15:01 +0100
parent 9021:548ba4090012
line wrap: on
line source


local xmppstream = require "util.xmppstream";

describe("util.xmppstream", function()
	local function test(xml, expect_success, ex)
		local stanzas = {};
		local session = { notopen = true };
		local callbacks = {
			stream_ns = "streamns";
			stream_tag = "stream";
			default_ns = "stanzans";
			streamopened = function (_session)
				assert.are.equal(session, _session);
				assert.are.equal(session.notopen, true);
				_session.notopen = nil;
				return true;
			end;
			handlestanza = function (_session, stanza)
				assert.are.equal(session, _session);
				assert.are.equal(_session.notopen, nil);
				table.insert(stanzas, stanza);
			end;
			streamclosed = function (_session)
				assert.are.equal(session, _session);
				assert.are.equal(_session.notopen, nil);
				_session.notopen = nil;
			end;
		}
		if type(ex) == "table" then
			for k, v in pairs(ex) do
				if k ~= "_size_limit" then
					callbacks[k] = v;
				end
			end
		end
		local stream = xmppstream.new(session, callbacks, ex and ex._size_limit or nil);
		local ok, err = pcall(function ()
			assert(stream:feed(xml));
		end);

		if ok and type(expect_success) == "function" then
			expect_success(stanzas);
		end
		assert.are.equal(not not ok, not not expect_success, "Expected "..(expect_success and ("success ("..tostring(err)..")") or "failure"));
	end

	local function test_stanza(stanza, expect_success, ex)
		return test([[<stream:stream xmlns:stream="streamns" xmlns="stanzans">]]..stanza, expect_success, ex);
	end

	describe("#new()", function()
		it("should work", function()
			test([[<stream:stream xmlns:stream="streamns"/>]], true);
			test([[<stream xmlns="streamns"/>]], true);

			-- Incorrect stream tag name should be rejected
			test([[<stream1 xmlns="streamns"/>]], false);
			-- Incorrect stream namespace should be rejected
			test([[<stream xmlns="streamns1"/>]], false);
			-- Invalid XML should be rejected
			test("<>", false);

			test_stanza("<message/>", function (stanzas)
				assert.are.equal(#stanzas, 1);
				assert.are.equal(stanzas[1].name, "message");
			end);
			test_stanza("< message>>>>/>\n", false);

			test_stanza([[<x xmlns:a="b">
				<y xmlns:a="c">
					<a:z/>
				</y>
				<a:z/>
			</x>]], function (stanzas)
				assert.are.equal(#stanzas, 1);
				local s = stanzas[1];
				assert.are.equal(s.name, "x");
				assert.are.equal(#s.tags, 2);

				assert.are.equal(s.tags[1].name, "y");
				assert.are.equal(s.tags[1].attr.xmlns, nil);

				assert.are.equal(s.tags[1].tags[1].name, "z");
				assert.are.equal(s.tags[1].tags[1].attr.xmlns, "c");

				assert.are.equal(s.tags[2].name, "z");
				assert.are.equal(s.tags[2].attr.xmlns, "b");

				assert.are.equal(s.namespaces, nil);
			end);
		end);
	end);

	it("should allow an XML declaration", function ()
		test([[<?xml version="1.0" encoding="UTF-8"?><stream xmlns="streamns"/>]], true);
		test([[<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><stream xmlns="streamns"/>]], true);
		test([[<?xml version="1.0" encoding="utf-8" ?><stream xmlns="streamns"/>]], true);
	end);

	it("should not accept XML versions other than 1.0", function ()
		test([[<?xml version="1.1" encoding="utf-8" ?><stream xmlns="streamns"/>]], false);
	end);

	it("should not allow a misplaced XML declaration", function ()
		test([[<stream xmlns="streamns"><?xml version="1.0" encoding="UTF-8"?></stream>]], false);
	end);

	describe("should forbid restricted XML:", function ()
		it("comments", function ()
			test_stanza("<!-- hello world -->", false);
		end);
		it("DOCTYPE", function ()
			test([[<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE stream SYSTEM "mydtd.dtd">]], false);
		end);
		it("incorrect encoding specification", function ()
			-- This is actually caught by the underlying XML parser
			test([[<?xml version="1.0" encoding="UTF-16"?><stream xmlns="streamns"/>]], false);
		end);
		it("non-UTF8 encodings: ISO-8859-1", function ()
			test([[<?xml version="1.0" encoding="ISO-8859-1"?><stream xmlns="streamns"/>]], false);
		end);
		it("non-UTF8 encodings: UTF-16", function ()
			-- <?xml version="1.0" encoding="UTF-16"?><stream xmlns="streamns"/>
			-- encoded into UTF-16
			local hx = ([[fffe3c003f0078006d006c002000760065007200730069006f006e003d00
			220031002e0030002200200065006e0063006f00640069006e0067003d00
			22005500540046002d003100360022003f003e003c007300740072006500
			61006d00200078006d006c006e0073003d00220073007400720065006100
			6d006e00730022002f003e00]]):gsub("%x%x", function (c) return string.char(tonumber(c, 16)); end);
			test(hx, false);
		end);
		it("processing instructions", function ()
			test([[<stream xmlns="streamns"><?xml-stylesheet type="text/xsl" href="style.xsl"?></stream>]], false);
		end);
	end);
end);