Package: plone.protect
IntroductionThis package contains utilities that can help protect parts of Plone
or applications build on top of the Plone framework.1. Restricting to HTTP POSTa) Using decoratorIf you only need to allow HTTP POST requests you can use thePostOnlychecker:from plone.protect import PostOnly
from plone.protect import protect

@protect(PostOnly)
def manage_doSomething(self, param, REQUEST=None):
    passThis checker operates only on HTTP requests; other types of requests
are not checked.b) Passing request to a function validatorSimply:from plone.protect import PostOnly

...
PostOnly(self.context.REQUEST)
...2. Form authentication (CSRF)A common problem in web applications is Cross Site Request Forgery or CSRF.
This is an attack method in which an attacker tricks a browser to do a HTTP
form submit to another site. To do this the attacker needs to know the exact
form parameters. Form authentication is a method to make it impossible for an
attacker to predict those parameters by adding an extra authenticator which
can be verified.Generating the tokenTo use the form authenticator you first need to insert it into your form.
This can be done using a simple TAL statement inside your form:<span tal:replace="structure context/@@authenticator/authenticator"/>this will produce a HTML input element with the authentication information.If you want to create the token value programmatically, use the following:from plone.protect.authenticator import createToken
token = createToken()Validating the tokena) Zope Component Architecture wayNext you need to add logic somewhere to verify the authenticator. This
can be done using a call to the authenticator view. For example:authenticator=getMultiAdapter((context, request), name=u"authenticator")
if not authenticator.verify():
    raise Unauthorizedb) Using decoratorYou can do the same thing more conveniently using theprotectdecorator:from plone.protect import CheckAuthenticator
from plone.protect import protect

@protect(CheckAuthenticator)
def manage_doSomething(self, param, REQUEST=None):
    passc) Passing request to a function validatorOr just:from plone.protect import CheckAuthenticator

...
CheckAuthenticator(self.context.REQUEST)
...HeadersYou can also pass in the token by using the headerX-CSRF-TOKEN. This can be
useful for AJAX requests.Protect decoratorThe most common way to use plone.protect is through theprotectdecorator. This decorator takes a list ofcheckersas parameters: each
checker will check a specific security aspect of the request. For example:from plone.protect import protect
from plone.protect import PostOnly

@protect(PostOnly)
def SensitiveMethod(self, REQUEST=None):
    # This is only allowed with HTTP POST requests.Thisrelieson the protected method having a parameter calledREQUEST (case sensitive).Customized Form AuthenticationIf you’d like use a different authentication token for different forms,
you can provide an extra string to use with the token:<tal:authenticator tal:define="authenticator context/@@authenticator">
  <span tal:replace="structure python: authenticator.authenticator('a-form-related-value')"/>
</tal:authenticator>To verify:authenticator=getMultiAdapter((context, request), name=u"authenticator")
if not authenticator.verify('a-form-related-value'):
    raise UnauthorizedWith the decorator:from plone.protect import CustomCheckAuthenticator
from plone.protect import protect

@protect(CustomCheckAuthenticator('a-form-related-value'))
def manage_doSomething(self, param, REQUEST=None):
    passAutomatic CSRF ProtectionSince version 3, plone.protect provides automatic CSRF protection. It does
this by automatically including the auth token to all internal forms when
the user requesting the page is logged in.Additionally, whenever a particular request attempts to write to the ZODB,
it’ll check for the existence of a correct auth token.Allowing write on read programmaticallyWhen you need to allow a known write on read, you’ve got several options.Adding a CSRF token to your linksIf you’ve got a GET request that causes a known write on read, your first
option should be to simply add a CSRF token to the URLs that result in that
request.plone.protectprovides theaddTokenToUrlfunction for this
purpose:from plone.protect.utils import addTokenToUrl

url = addTokenToUrl(url)If you just want to allow an object to be writable on a request…You can use thesafeWritehelper function:from plone.protect.utils import safeWrite

safeWrite(myobj, request)Marking the entire request as safeJust add theIDisableCSRFProtectioninterface to the current request
object:from plone.protect.interfaces import IDisableCSRFProtection
from zope.interface import alsoProvides

alsoProvides(request, IDisableCSRFProtection)Warning! When you do this, the current request is susceptible to CSRF
exploits so do any required CSRF protection manually.Clickjacking Protectionplone.protect also provides, by default, clickjacking protection since
version 3.0.To protect against this attack, Plone uses the X-Frame-Options
header. plone.protect will set the X-Frame-Options value toSAMEORIGIN.To customize this value, you can set it to a custom value for a custom view
(e.g.self.request.response.setHeader('X-Frame-Options','ALLOWALL')),
override it at your proxy server, or you can set the environment variable ofPLONE_X_FRAME_OPTIONSto whatever value you’d like plone.protect to set
this to globally.You can opt out of this by making the environment variable empty.Disable All Automatic CSRF ProtectionTo disable all automatic CSRF protection, set the environment variablePLONE_CSRF_DISABLEDvalue totrue.WarningIt is very dangerous to do this. Do not do this unless the ZEO client
with this setting is not public and you know what you are doing.NoteThis doesn’t disable explicit and manual CSRF protection checks.Fixing CSRF Protection failures in testsIf you getUnauthorizederrors in tests due to unprotected form submission
where normally automatic protection would be in place you can use the following
blueprint to protect your forms:from plone.protect.authenticator import createToken
from ..testing import MY_INTEGRATION_TESTING_LAYER
import unittest

class MyTest(unittest.TestCase):

    layer = MY_INTEGRATION_TESTING_LAYER

    def setUp(self):
        self.request = self.layer['request']
        # Disable plone.protect for these tests
        self.request.form['_authenticator'] = createToken()
        # Eventuelly you find this also useful
        self.request.environ['REQUEST_METHOD'] = 'POST'NotesThis package monkey patches a number of modules in order to better handle CSRF
protection:- Archetypes add forms, add csrf
- Zope2 object locking support
- pluggable auth csrf protectionIf you are using a proxy cache in front of your site, be aware that
you will need to clear the entry for++resource++protect.jsevery
time you update this package or you will find issues with modals while
editing content.Compatibilityplone.protectversion 3 was made for Plone 5.  You can use it on
Plone 4 for better protection, but you will need theplone4.csrffixeshotfix package as well to avoid getting
needless warnings or errors.  See thehotfix announcementand thehotfix page.Changelog5.0.1 (2024-01-22)Internal:Update configuration files.
[plone devs] (6e36bcc4, 7723aeaf)5.0.0 (2023-04-15)New features:Drop support for Python <3.8. (5390ebc6)Bug fixes:Do not hard-depend onplone.portlets.
Prepare for Plone with portlets optional.
@jensens (#99)Internal:Update configuration files.
[plone devs] (a9dd65cc)4.1.8 (2022-12-16)Bug fixes:Testing: explicitly set response content type header to html.  [jeromeperrin] (#97)4.1.7 (2022-12-02)Bug fixes:Add missing z3c.zcmlhook dependency.  [icemac] (#96)4.1.6 (2020-09-26)Bug fixes:Fixed deprecation warning forwebdav.Lockable.LockableItem.
[maurits] (#3130)4.1.5 (2020-04-21)Bug fixes:Minor packaging updates. (#1)4.1.4 (2020-03-13)Bug fixes:Remove deprecation warnings (#90)4.1.3 (2019-08-23)Bug fixes:When marking an OOBTree as safe, also mark its buckets as safe. Fixes issues with objects that have many annotations. (#88)4.1.2 (2019-02-13)Bug fixes:Avoid deprecation warnings. [gforcada] (#87)4.1.1 (2018-12-11)Breaking changes:Remove five.globalrequest dependency.
It has been deprecated upstream (Zope 4).
[gforcada]4.1.0 (2018-11-02)Breaking changes:Adapt to changed visibility ofbuildfacadeinAccessControl.requestmethod. Requires AccessControl >= 4.0b6
[tschorr]Bug fixes:More Python 2 / 3 compatibility
[pbauer, MatthewWilkes]Fix marmoset monkey patching for Python 3
[jensens]Don’t patch until zcml loaded
[davisagli]Put the marmoset on a leash  (reset csrf-checks after tests)
[davisagli]4.0.1 (2018-07-16)Bug fixes:Fix package dependencies;cssselecthas been an extra oflxmlsince 2014 (closes#79).
[hvelarde]Fixed tests to work with merged plone.login
[jensens]4.0.0 (2018-07-16)Breaking changes:Version 3.1.3 introduced a Python 3 compatibility fix that broke some Python 2 versions with aSyntaxError.
Reports are mostly for Python 2.7.8 and lower, but also one for 2.7.14, but only on Travis.
So this marks a breaking change.
The incompatibility will be reverted on branch 3.x.
Version 3.1.4 should be safe to use again.
Seeissue 74.
andissue 75.
[maurits]Bug fixes:Avoid CSRF warnings due to generating image scales
stored in a plone.scale.storage.ScalesDict.
[davisagli]3.1.3 (2018-04-04)Bug fixes:More Python 2 / 3 compatibility.
Warning: this gives a SyntaxError on Python 2.7.8 or lower.
Seeissue 74.
[pbauer]3.1.2 (2018-02-02)Bug fixes:Transform does not log a warning for empty responses
(Fixeshttps://github.com/plone/plone.protect/issues/15)
[fRiSi]Add Python 2 / 3 compatibility
[vincero]3.1.1 (2017-08-27)Bug fixes:README wording tweaks
[tkimnguyen]3.1 (2017-08-14)New features:Log forbidden URLs.
Fixeshttps://github.com/plone/plone.protect/issues/66[gforcada]3.0.26 (2017-08-04)New features:CatchAttributeErroron transform.
[hvelarde]3.0.25 (2017-07-18)Bug fixes:Fix logging to no longer write traceback to stdout, but include it in the
logging message instead.
[jone]3.0.24 (2017-07-03)Bug fixes:Remove unittest2 dependency
[kakshay21]3.0.23 (2016-11-26)Bug fixes:Allowconfirm-actionfor all contexts, instead of only Plone Site root.
This avoids an error when calling it on a subsite.
Fixesissue #51.
[maurits]Code Style: utf8-headers, import sorting, new style namespace declaration, autopep8
[jensens]Fix #57: Html must contain “body”, otherwise plone.protect breaks.
[jensens]3.0.22 (2016-11-17)Bug fixes:avoid zope.globalrequest.getRequest()
[tschorr]3.0.21 (2016-10-05)Bug fixes:Avoid regenerating image scale over and over in Plone 4.
Avoid (unnoticed) error when refreshing lock in Plone 4,
plus a few other cases that were handled by plone4.csrffixes.
Fixeshttps://github.com/plone/plone.protect/issues/47[maurits]3.0.20 (2016-09-08)Bug fixes:Only try the confirm view for urls that are in the portal.
This applies PloneHotfix20160830.  [maurits]RemovedRedirectTopatch.  The patch has been merged toProducts.CMFFormController3.0.7 (Plone 4.3 and 5.0) and 3.1.2
(Plone 5.1).  Note that we are not requiring those versions in oursetup.py, because the code in this package no longer needs it.
[maurits]3.0.19 (2016-08-19)New:Added protect.js from plone4.csrffixes.  This adds anX-CSRF-TOKENheader to ajax requests.
Fixeshttps://github.com/plone/plone.protect/issues/42[maurits]Fixes:Use zope.interface decorator.
[gforcada]3.0.18 (2016-02-25)Fixes:Fixed AttributeError when callingsafeWriteon aTestRequest, because this has noenviron..  [maurits]3.0.17 (2015-12-07)Fixes:Internationalized button in confirm.pt.
[vincentfretin]3.0.16 (2015-11-05)Fixes:Make sure transforms don’t fail on redirects.
[lgraf]3.0.15 (2015-10-30)make sure to always compare content type with a string when checking
if we should show the confirm-action view.
[vangheem]Internationalized confirm.pt
[vincentfretin]Disable editable border for @@confirm-action view.
[lgraf]Make title and description show up on @@confirm-action view.
[lgraf]Allow views to override ‘X-Frame-Options’ by setting the response header
manually.
[alecm]Avoid parsing redirect responses (this avoids a warning on the log files).
[gforcada]3.0.14 (2015-10-08)Handle TypeError caused by getToolByName on an
invalid context
[vangheem]You can opt out of clickjacking protection by setting the
environment variablePLONE_X_FRAME_OPTIONSto an empty string.
[maurits]Be more flexible in parsing thePLONE_CSRF_DISABLEDenvironment
variable.  We are no longer case sensitive, and we accepttrue,t,yes,y,1as true values.
[maurits]Avoid TypeError when checking the content-type header.
[maurits]3.0.13 (2015-10-07)Always force html serializer as the XHTML variant seems
to cause character encoding issues
[vangheem]3.0.12 (2015-10-06)Do not check writes to temporary storage like session storage
[davisagli]3.0.11 (2015-10-06)play nicer with inline JavaScript
[vangheem]3.0.10 (2015-10-06)make imports backward compatible
[vangheem]3.0.9 (2015-09-27)patch pluggable auth with marmoset patch because
the patch would not apply otherwise depending on
somewhat-random import order
[vangheem]get auto-csrf protection working on the zope root
[vangheem]3.0.8 (2015-09-20)conditionally patch Products.PluggableAuthService if needed
[vangheem]Do not raise ComponentLookupError on transform
[vangheem]3.0.7 (2015-07-24)Fix pluggable auth CSRF warnings on zope root. Very difficult to reproduce.
Just let plone.protect do it’s job also on zope root.
[vangheem]3.0.6 (2015-07-20)Just return if the request object is not valid.
[vangheem]3.0.5 (2015-07-20)fix pluggable auth CSRF warnings
[vangheem]fix detecting safe object writes on non-GET requests
[vangheem]instead of using _v_safe_write users should now use the safeWrite function
in plone.protect.auto
[vangheem]3.0.4 (2015-05-13)patch locking functions to use _v_safe_write attribute
[vangheem]Be able to use _v_safe_write attribute to specify objects are safe to write
[vangheem]3.0.3 (2015-03-30)handle zope root not having IKeyManager Utility and CRSF protection
not being supported on zope root requests yet
[vangheem]3.0.2 (2015-03-13)Add ITransform.transformBytes for protect transform to fix compatibility
with plone.app.blocks’ ESI-rendering
[atsoukka]3.0.1 (2014-11-01)auto CSRF protection: check for changes on all the storages
[mamico]CSRF test fixed
[mamico]3.0.0 (2014-04-13)auto-rotate keyrings
[vangheem]use specific keyring for protected forms
[vangheem]add automatic clickjacking protection(thanks to Manish Bhattacharya)
[vangheem]add automatic CSRF protection
[vangheem]2.0.2 (2012-12-09)Use constant time comparison to verify the authenticator. This is part of the
fix forhttps://plone.org/products/plone/security/advisories/20121106/23[davisagli]Add MANIFEST.in.
[WouterVH]Add ability to customize the token created.
[vangheem]2.0 - 2010-07-18Update license to BSD following board decision.http://lists.plone.org/pipermail/membership/2009-August/001038.html[elro]2.0a1 - 2009-11-14Removed deprecated AuthenticateForm class and zope.deprecation dependency.
[hannosch]Avoid deprecation warning for the sha module in Python 2.6.
[hannosch]Specify package dependencies
[hannosch]1.1 - 2008-06-02Add an optional GenericSetup profile to make it easier to install
plone.protect.
[mj]1.0 - 2008-04-19The protect decorator had a serious design flaw which broke it. Added
proper tests for it and fixed the problems.
[wichert]1.0rc1 - 2008-03-28Rename plone.app.protect to plone.protect: there is nothing Plone-specific
about the functionality in this package and it really should be used outside
of Plone as well.
[wichert]Made utils.protect work with Zope >= 2.11.
[stefan]1.0b1 - March 7, 2008Refactor the code to offer a generic protect decorator for methods
which takes a list of checkers as options. Add checkers for both the
authenticator verification and HTTP POST-only.
[wichert]1.0a1 - January 27, 2008Initial release
[wichert]