Skip to content

Commit

Permalink
Fix marmoset monkey patching for Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens authored and pbauer committed Sep 21, 2018
1 parent 838c8bd commit 9064061
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Bug fixes:
stored in a plone.scale.storage.ScalesDict.
[davisagli]

- Fix marmoset monkey patching for Python 3
[jensens]


3.1.3 (2018-04-04)
------------------
Expand Down
1 change: 1 addition & 0 deletions plone/protect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from plone.protect.authenticator import CustomCheckAuthenticator
from plone.protect.postonly import check as PostOnly
from plone.protect.utils import protect
from plone.protect import monkey
2 changes: 1 addition & 1 deletion plone/protect/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
description="Special handling for write on read Zope2 locking issues"
class="webdav.Lockable.LockableItem"
original="wl_lockmapping"
replacement=".monkey.wl_lockmapping"
replacement=".monkey_webdav.wl_lockmapping"
preserveOriginal="True"
/>
</configure>
10 changes: 6 additions & 4 deletions plone/protect/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ def pluggableauth__checkCSRFToken(request, token='csrf_token', raises=True):

def marmoset_patch(func, replacement):
source = inspect.getsource(replacement)
exec source in func.func_globals
func.func_code = replacement.func_code
exec(source, func.__globals__)
func.__code__ = replacement.__code__


# otherwise the patches do not get applied in some cases
if hasattr(pluggable_utils, 'checkCSRFToken'):
marmoset_patch(pluggable_utils.checkCSRFToken,
pluggableauth__checkCSRFToken)
marmoset_patch(
pluggable_utils.checkCSRFToken,
pluggableauth__checkCSRFToken,
)
if hasattr(pluggable_utils, 'getCSRFToken'):
marmoset_patch(pluggable_utils.getCSRFToken, pluggableauth__getCSRFToken)
16 changes: 16 additions & 0 deletions plone/protect/monkey_webdav.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
from plone.protect.auto import safeWrite


def wl_lockmapping(self, killinvalids=0, create=0):
has_write_locks = hasattr(self, '_dav_writelocks')
locks = self._old_wl_lockmapping(killinvalids=killinvalids, create=create)
try:
safeWrite(locks)
if not has_write_locks and create:
# first time writing to object, need to mark it safe
safeWrite(self)
except AttributeError:
# not a persistent class, ignore
pass
return locks
1 change: 0 additions & 1 deletion plone/protect/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# -*- coding: utf-8 -*-
# Poof
21 changes: 15 additions & 6 deletions plone/protect/tests/testPatches.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@ def setUp(self):
self.browser = Browser(self.layer['app'])
self.request = self.layer['request']
self.browser.addHeader(
'Authorization', 'Basic %s:%s' % (SITE_OWNER_NAME, SITE_OWNER_PASSWORD,))
'Authorization',
'Basic %s:%s' % (SITE_OWNER_NAME, SITE_OWNER_PASSWORD,)
)

def test_change_password_on_root_does_not_throw_other_csrf_protection(
self):
self.browser.open('%s/acl_users/users/manage_users?user_id=%s&passwd=1' % (
self.layer['app'].absolute_url(), SITE_OWNER_NAME))
self
):
self.browser.open(
'%s/acl_users/users/manage_users?user_id=%s&passwd=1' % (
self.layer['app'].absolute_url(), SITE_OWNER_NAME
)
)
self.browser.getControl(name='password').value = SITE_OWNER_PASSWORD
self.browser.getControl(name='confirm').value = SITE_OWNER_PASSWORD
self.browser.getForm().submit()
self.assertEquals(
self.browser.url,
'%s/acl_users/users/manage_users?manage_tabs_message=password+updated' % (
self.layer['app'].absolute_url()))
'%s/acl_users/users/manage_users?'
'manage_tabs_message=password+updated' % (
self.layer['app'].absolute_url()
)
)

0 comments on commit 9064061

Please sign in to comment.