Skip to content

Commit

Permalink
Add a few tests demonstrating the CSRF protection intergration in
Browse files Browse the repository at this point in the history
zope.formlib.
  • Loading branch information
janwijbrand committed Nov 18, 2013
1 parent a5eda97 commit 994e2b8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
1 change: 0 additions & 1 deletion buildout.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ extends = https://raw.github.com/zopefoundation/groktoolkit/master/grok.cfg
versions = versions
extensions = buildout.dumppickedversions


[versions]
grokcore.formlib =
zope.formlib =
Expand Down
51 changes: 48 additions & 3 deletions src/grokcore/formlib/ftests/form/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,45 @@
...Really big...
...
"Protected" forms only validate and process request data for the given
HTTP verb - usually POST. In addition, CSRF protection can be enabled.
"Submitting" the form over GET parameters should result in an
MethodNotAllowed error as the form is configured to only allow submits
over POST requests::
>>> browser.open(
... "http://localhost/manfred/@@editprotected?"
... "form.name=Manfred&form.size=Big&form.actions.apply=Apply")
Traceback (most recent call last):
...
MethodNotAllowed: <grokcore.formlib.ftests.form.form.Mammoth object at ...>,
<zope.publisher.browser.BrowserRequest
instance URL=http://localhost/manfred/@@editprotected>
When CSRF protection is enabled, the corresponding hidden form field is
rendered by the form templates::
>>> browser.open("http://localhost/manfred/@@editcsrfprotected")
>>> print browser.contents
<html>...
<input type="hidden" name="__csrftoken__" value="..." />...
>>> browser.cookies['__csrftoken__'] in browser.contents
True
Submitting the form with a wrong token or a missing token, will raise an
InvalidForm error::
>>> browser.open("http://localhost/manfred/@@editcsrfprotected")
>>> browser.getControl(name="form.name").value = "Manfred the Mammoth"
>>> browser.getControl(name="form.size").value = "Really big"
>>> browser.getControl(name="__csrftoken__").value = "invalid value"
>>> browser.getControl("Apply").click()
Traceback (most recent call last):
...
InvalidCSRFTokenError: Invalid CSRF token
"""
import grokcore.formlib as grok
from zope import schema
Expand All @@ -39,12 +78,18 @@ class Mammoth(grok.testing.Model):
implements(IMammoth)
grok.testing.protect_get(grok.Public, 'name', 'size')
grok.testing.protect_set(grok.Public, 'name', 'size')
name = FieldProperty(IMammoth['name'])
size = FieldProperty(IMammoth['size'])

name = FieldProperty(IMammoth['name'])
size = FieldProperty(IMammoth['size'])

class Edit(grok.EditForm):
pass

class Display(grok.DisplayForm):
pass

class EditProtected(grok.EditForm):
method = 'POST' # only allow submits over POST requests.

class EditCsrfProtected(EditProtected):
protected = True # enables CSRF protection.
2 changes: 1 addition & 1 deletion src/grokcore/formlib/templates/default_display_form.pt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<tr class="controls">
<td colspan="2" class="align-right">
<input type="hidden"
name="___csrftoken__"
name="__csrftoken__"
tal:condition="view/protected"
tal:attributes="value view/csrftoken"
/>
Expand Down
2 changes: 1 addition & 1 deletion src/grokcore/formlib/templates/default_edit_form.pt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

<div id="actionsView">
<input type="hidden"
name="___csrftoken__"
name="__csrftoken__"
tal:condition="view/protected"
tal:attributes="value view/csrftoken"
/>
Expand Down

0 comments on commit 994e2b8

Please sign in to comment.