Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent race condition in guarded_import #124

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ For changes before version 3.0, see ``HISTORY.rst``.
5.4 (unreleased)
----------------

- Nothing changed yet.
- Prevent race condition in guarded_import
(see `#123 <https://github.com/zopefoundation/AccessControl/issues/123>`_)


5.3 (2022-02-25)
Expand Down
7 changes: 6 additions & 1 deletion src/AccessControl/SecurityInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,12 @@ def secureModule(mname, *imp):

if imp:
__import__(mname, *imp)
del _moduleSecurity[mname]
try:
del _moduleSecurity[mname]
except KeyError:
# In case of access from multiple threads, the del might fail, but that
# is OK.
pass
module = sys.modules[mname]
modsec.apply(module.__dict__)
_appliedModuleSecurity[mname] = modsec
Expand Down
21 changes: 21 additions & 0 deletions src/AccessControl/tests/testModuleSecurity.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ def testPublicModule(self):
self.assertAuth('AccessControl.tests.public_module.submodule',
('pub',))

def testPublicModuleThreaded(self):
"""
Import the same module from two threads simultaneously, checking that
this does not result in a race condition.
"""
import threading
finished = []

def threaded_run():
self.assertAuth('AccessControl.tests.public_module', ())
finished.append(True)

threads = [threading.Thread(target=threaded_run) for _ in range(2)]

for t in threads:
t.start()
for t in threads:
t.join()

self.assertEqual(len(finished), 2)

def test_public_module_asterisk_not_allowed(self):
self.assertUnauth('AccessControl.tests.public_module', ('*',))

Expand Down