Skip to content

Commit 3152362

Browse files
committed
Test: cover mount execution path, containment check and LDAP filter escaping
1 parent cc6a1af commit 3152362

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

‎usr/lib/python3/dist-packages/linuxmusterLinuxclient7/tests/test_ldapHelper.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from unittest import mock
12
from .. import ldapHelper
23

34

@@ -10,3 +11,13 @@ def test_escape():
1011
assert ldapHelper.escape("a\\b") == r"a\5cb"
1112
# Non-str input is handled
1213
assert ldapHelper.escape(123) == "123"
14+
15+
16+
@mock.patch("linuxmusterLinuxclient7.ldapHelper.searchOne")
17+
def test_isObjectInGroupEscapesFilter(mockSearchOne):
18+
mockSearchOne.return_value = (True, {})
19+
# A crafted group name with filter metacharacters must be escaped, not injected.
20+
assert ldapHelper.isObjectInGroup("cn=user,dc=x", "domain users)(cn=*")
21+
usedFilter = mockSearchOne.call_args.args[0]
22+
assert "*" not in usedFilter
23+
assert r"\2a" in usedFilter

‎usr/lib/python3/dist-packages/linuxmusterLinuxclient7/tests/test_shares.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,40 @@ def test_mountShareRejectsTraversal(mockSubprocessCall):
4444
for call_args in mockSubprocessCall.call_args_list:
4545
assert call_args.args[0][0] != "timeout"
4646

47+
48+
@mock.patch("builtins.open", new_callable=mock.mock_open)
49+
@mock.patch("subprocess.call")
50+
@mock.patch("linuxmusterLinuxclient7.shares._directoryIsMountpoint")
51+
@mock.patch("linuxmusterLinuxclient7.shares.Path")
52+
@mock.patch("linuxmusterLinuxclient7.shares.pwd.getpwnam")
53+
@mock.patch("linuxmusterLinuxclient7.shares.config.network")
54+
def test_mountShareValidPathMounts(mockNetwork, mockGetpwnam, mockPath, mockIsMountpoint, mockSubprocessCall, mockOpen):
55+
mockNetwork.return_value = (True, {"domain": "linuxmuster.lan"})
56+
mockGetpwnam.return_value = mock.Mock(pw_uid=1001, pw_gid=1001)
57+
mockIsMountpoint.return_value = False
58+
mockSubprocessCall.return_value = 0
59+
60+
rc, mountpoint = shares._mountShare("user1", "//server/share", "myshare", True, False)
61+
assert rc is True
62+
assert mountpoint == "/srv/samba/user1/myshare"
63+
64+
mountCalls = [c.args[0] for c in mockSubprocessCall.call_args_list if c.args[0][0] == "timeout"]
65+
assert len(mountCalls) == 1
66+
# mount.cifs is wrapped in `timeout` and mounts to the validated mountpoint
67+
assert mountCalls[0][0:2] == ["timeout", "10"]
68+
assert mountCalls[0][-2:] == ["//server/share", "/srv/samba/user1/myshare"]
69+
70+
71+
@mock.patch("subprocess.call")
72+
@mock.patch("linuxmusterLinuxclient7.shares._getShareMountpoint")
73+
def test_mountShareRejectsMountpointOutsideBase(mockGetShareMountpoint, mockSubprocessCall):
74+
# Even if the mountpoint calculation were to escape the base directory,
75+
# the containment check must refuse the mount (defense in depth).
76+
mockGetShareMountpoint.return_value = "/etc/cron.d"
77+
assert shares._mountShare("user1", "//server/share", "share", False, False) == (False, None)
78+
for call_args in mockSubprocessCall.call_args_list:
79+
assert call_args.args[0][0] != "timeout"
80+
4781
@mock.patch("linuxmusterLinuxclient7.shares.mountShare")
4882
@mock.patch("linuxmusterLinuxclient7.shares.computer.hostname")
4983
@mock.patch("linuxmusterLinuxclient7.shares.user.isRoot")

0 commit comments

Comments
 (0)