Annotate

spec/util_roles_spec.lua @ 13652:a08065207ef0

net.server_epoll: Call :shutdown() on TLS sockets when supported Comment from Matthew: This fixes a potential issue where the Prosody process gets blocked on sockets waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends layer 7 data, and this can cause problems for sockets which are in the process of being cleaned up. This depends on LuaSec changes which are not yet upstream. From Martijn's original email: So first my analysis of luasec. in ssl.c the socket is put into blocking mode right before calling SSL_shutdown() inside meth_destroy(). My best guess to why this is is because meth_destroy is linked to the __close and __gc methods, which can't exactly be called multiple times and luasec does want to make sure that a tls session is shutdown as clean as possible. I can't say I disagree with this reasoning and don't want to change this behaviour. My solution to this without changing the current behaviour is to introduce a shutdown() method. I am aware that this overlaps in a conflicting way with tcp's shutdown method, but it stays close to the OpenSSL name. This method calls SSL_shutdown() in the current (non)blocking mode of the underlying socket and returns a boolean whether or not the shutdown is completed (matching SSL_shutdown()'s 0 or 1 return values), and returns the familiar ssl_ioerror() strings on error with a false for completion. This error can then be used to determine if we have wantread/wantwrite to finalize things. Once meth_shutdown() has been called once a shutdown flag will be set, which indicates to meth_destroy() that the SSL_shutdown() has been handled by the application and it shouldn't be needed to set the socket to blocking mode. I've left the SSL_shutdown() call in the LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a timeout for the shutdown code, which might allow SSL_shutdown() to clean up anyway at the last possible moment. Another thing I've changed to luasec is the call to socket_setblocking() right before calling close(2) in socket_destroy() in usocket.c. According to the latest POSIX[0]: Note that the requirement for close() on a socket to block for up to the current linger interval is not conditional on the O_NONBLOCK setting. Which I read to mean that removing O_NONBLOCK on the socket before close doesn't impact the behaviour and only causes noise in system call tracers. I didn't touch the windows bits of this, since I don't do windows. For the prosody side of things I've made the TLS shutdown bits resemble interface:onwritable(), and put it under a combined guard of self._tls and self.conn.shutdown. The self._tls bit is there to prevent getting stuck on this condition, and self.conn.shutdown is there to prevent the code being called by instances where the patched luasec isn't deployed. The destroy() method can be called from various places and is read by me as the "we give up" error path. To accommodate for these unexpected entrypoints I've added a single call to self.conn:shutdown() to prevent the socket being put into blocking mode. I have no expectations that there is any other use here. Same as previous, the self.conn.shutdown check is there to make sure it's not called on unpatched luasec deployments and self._tls is there to make sure we don't call shutdown() on tcp sockets. I wouldn't recommend logging of the conn:shutdown() error inside close(), since a lot of clients simply close the connection before SSL_shutdown() is done.
author Martijn van Duren <martijn@openbsd.org>
date Thu, 06 Feb 2025 15:04:38 +0000
parent 12754:a92ca737d05f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12747
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 describe("util.roles", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 randomize(false);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local roles;
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 it("can be loaded", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 roles = require "util.roles";
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local test_role;
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 it("can create a new role", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 test_role = roles.new();
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 assert.is_not_nil(test_role);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 assert.is_truthy(roles.is_role(test_role));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 describe("role object", function ()
12753
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
14 it("can be initialized with permissions", function ()
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
15 local test_role_2 = roles.new({
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
16 permissions = {
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
17 perm1 = true;
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
18 perm2 = false;
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
19 };
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
20 });
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
21 assert.truthy(test_role_2:may("perm1"));
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
22 assert.falsy(test_role_2:may("perm2"));
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
23 end);
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
24 it("has a sensible tostring", function ()
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
25 local test_role_2 = roles.new({
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
26 id = "test-role-2";
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
27 name = "Test Role 2";
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
28 });
12754
a92ca737d05f util.roles: Fix tests to use autogenerated role id
Matthew Wild <mwild1@gmail.com>
parents: 12753
diff changeset
29 assert.truthy(tostring(test_role_2):find(test_role_2.id, 1, true));
12753
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
30 assert.truthy(tostring(test_role_2):find("Test Role 2", 1, true));
2eb02b32bb4c util.roles: Add some more missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12747
diff changeset
31 end);
12747
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 it("is restrictive by default", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 assert.falsy(test_role:may("my-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 it("allows you to set permissions", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 test_role:set_permission("my-permission", true);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 assert.truthy(test_role:may("my-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 it("allows you to set negative permissions", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 test_role:set_permission("my-other-permission", false);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 assert.falsy(test_role:may("my-other-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 it("does not allows you to override previously set permissions by default", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 local ok, err = test_role:set_permission("my-permission", false);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 assert.falsy(ok);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 assert.is_equal("policy-already-exists", err);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 -- Confirm old permission still in place
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 assert.truthy(test_role:may("my-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 it("allows you to explicitly override previously set permissions", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 assert.truthy(test_role:set_permission("my-permission", false, true));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 assert.falsy(test_role:may("my-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 describe("inheritance", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local child_role;
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 it("works", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 test_role:set_permission("inherited-permission", true);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 child_role = roles.new({
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 inherits = { test_role };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 });
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 assert.truthy(child_role:may("inherited-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 assert.falsy(child_role:may("my-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 it("allows listing policies", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 local expected = {
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 ["my-permission"] = false;
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 ["my-other-permission"] = false;
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 ["inherited-permission"] = true;
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 local received = {};
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 for permission_name, permission_policy in child_role:policies() do
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 received[permission_name] = permission_policy;
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 assert.same(expected, received);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 it("supports multiple depths of inheritance", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 local grandchild_role = roles.new({
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 inherits = { child_role };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 });
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 assert.truthy(grandchild_role:may("inherited-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 describe("supports ordered inheritance from multiple roles", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 local parent_role = roles.new();
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 local final_role = roles.new({
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 -- Yes, the names are getting confusing.
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 -- btw, test_role is inherited through child_role.
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 inherits = { parent_role, child_role };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 });
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 local test_cases = {
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 -- { <final_role policy>, <parent_role policy>, <test_role policy> }
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 { true, nil, false, result = true };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 { nil, false, true, result = false };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 { nil, true, false, result = true };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 { nil, nil, false, result = false };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 { nil, nil, true, result = true };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 for n, test_case in ipairs(test_cases) do
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 it("(case "..n..")", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 local perm_name = ("multi-inheritance-perm-%d"):format(n);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 assert.truthy(final_role:set_permission(perm_name, test_case[1]));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 assert.truthy(parent_role:set_permission(perm_name, test_case[2]));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 assert.truthy(test_role:set_permission(perm_name, test_case[3]));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 assert.equal(test_case.result, final_role:may(perm_name));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 end
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 it("updates child roles when parent roles change", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 assert.truthy(child_role:may("inherited-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 assert.truthy(test_role:set_permission("inherited-permission", false, true));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 assert.falsy(child_role:may("inherited-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 describe("cloning", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 local cloned_role;
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 it("works", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 assert.truthy(test_role:set_permission("perm-1", true));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 cloned_role = test_role:clone();
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 assert.truthy(cloned_role:may("perm-1"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 it("isolates changes", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 -- After cloning, changes in either the original or the clone
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 -- should not appear in the other.
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 assert.truthy(test_role:set_permission("perm-1", false, true));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 assert.truthy(test_role:set_permission("perm-2", true));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 assert.truthy(cloned_role:set_permission("perm-3", true));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 assert.truthy(cloned_role:may("perm-1"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 assert.falsy(cloned_role:may("perm-2"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 assert.falsy(test_role:may("perm-3"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 end);