Skip to main content

Security for browser forms

Project description

Introduction

This package contains utilities that can help to protect parts of Plone or applications build on top of the Plone framework.

1. Restricting to HTTP POST

a) Using decorator

If you only need to allow HTTP POST requests you can use the PostOnly checker:

from plone.protect import PostOnly
from plone.protect import protect

@protect(PostOnly)
def manage_doSomething(self, param, REQUEST=None):
    pass

This checker only operates on HTTP requests; other types of requests are not checked.

b) Passing request to a function validator

Simply:

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 token

To 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.

Validating the token

a) ZCA way

Next 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 Unauthorized

b) Using decorator

You can do the same thing more conveniently using the protect decorator:

from plone.protect import CheckAuthenticator
from plone.protect import protect

@protect(CheckAuthenticator)
def manage_doSomething(self, param, REQUEST=None):
    pass

c) Passing request to a function validator

Or just:

from plone.protect import CheckAuthenticator

...
CheckAuthenticator(self.context.REQUEST)
...

Headers

You can also pass in the token by using the header X-CSRF-TOKEN. This can be useful for AJAX requests.

Protect decorator

The most common way to use plone.protect is through the protect decorator. This decorator takes a list of checkers as 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.

This relies on the protected method having a parameter called REQUEST (case sensitive).

Customized Form Authentication

If 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 Unauthorized

With 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):
    pass

Automatic CSRF Protection

Since 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 programatically

Just add an interface 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.

If you just want to allow an object to be writable on a request…

You can use the safeWrite helper function.

from plone.protect.auto import safeWrite safeWrite(myobj, request)

Clickjacking Protection

plone.protect also provides, by default, clickjacking protection since version 3.0.

To protect against this attack, plone employs the use of the X-Frame-Options header. plone.protect will set the X-Frame-Options value to SAMEORIGIN.

To customize this value, you can either override it at your proxy server or you can set the environment variable of PLONE_X_FRAME_OPTIONS to whatever value you’d like plone.protect to set this to.

Disable All Automatic CSRF Protection

To disable all automatic CSRF protection, set the environment variable PLONE_CSRF_DISABLED value to true.

WARNING! It 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.

Notes

This 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 protection

Changelog

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)

2.0 - 2010-07-18

2.0a1 - 2009-11-14

  • Removed 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-02

  • Add an optional GenericSetup profile to make it easier to install plone.protect. [mj]

1.0 - 2008-04-19

  • The protect decorator had a serious design flaw which broke it. Added proper tests for it and fixed the problems. [wichert]

1.0rc1 - 2008-03-28

  • Rename 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, 2008

  • Refactor 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, 2008

  • Initial release [wichert]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plone.protect-3.0.10.zip (35.5 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page