summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
Diffstat (limited to 'docs')
-rw-r--r--docs/api/exceptions.rst2
-rw-r--r--docs/api/session.rst2
-rw-r--r--docs/narr/sessions.rst24
-rw-r--r--docs/narr/viewconfig.rst14
-rw-r--r--docs/whatsnew-1.7.rst48
5 files changed, 65 insertions, 25 deletions
diff --git a/docs/api/exceptions.rst b/docs/api/exceptions.rst
index faca0fbb6..cb411458d 100644
--- a/docs/api/exceptions.rst
+++ b/docs/api/exceptions.rst
@@ -5,6 +5,8 @@
.. automodule:: pyramid.exceptions
+ .. autoexception:: BadCSRFOrigin
+
.. autoexception:: BadCSRFToken
.. autoexception:: PredicateMismatch
diff --git a/docs/api/session.rst b/docs/api/session.rst
index 474e2bb32..56c4f52d7 100644
--- a/docs/api/session.rst
+++ b/docs/api/session.rst
@@ -9,6 +9,8 @@
.. autofunction:: signed_deserialize
+ .. autofunction:: check_csrf_origin
+
.. autofunction:: check_csrf_token
.. autofunction:: SignedCookieSessionFactory
diff --git a/docs/narr/sessions.rst b/docs/narr/sessions.rst
index d66e86258..7cf96ac7d 100644
--- a/docs/narr/sessions.rst
+++ b/docs/narr/sessions.rst
@@ -391,8 +391,8 @@ will return ``True``, otherwise it will raise ``HTTPBadRequest``. Optionally,
you can specify ``raises=False`` to have the check return ``False`` instead of
raising an exception.
-By default, it checks for a GET or POST parameter named ``csrf_token`` or a
-header named ``X-CSRF-Token``.
+By default, it checks for a POST parameter named ``csrf_token`` or a header
+named ``X-CSRF-Token``.
.. code-block:: python
@@ -411,15 +411,16 @@ Checking CSRF Tokens Automatically
.. versionadded:: 1.7
-:app:`Pyramid` supports automatically checking CSRF tokens on POST requests.
-Any other request may be checked manually. This feature can be turned on
-globally for an application using the ``pyramid.require_default_csrf`` setting.
+:app:`Pyramid` supports automatically checking CSRF tokens on requests with an
+unsafe method as defined by RFC2616. Any other request may be checked manually.
+This feature can be turned on globally for an application using the
+``pyramid.require_default_csrf`` setting.
If the ``pyramid.required_default_csrf`` setting is a :term:`truthy string` or
``True`` then the default CSRF token parameter will be ``csrf_token``. If a
different token is desired, it may be passed as the value. Finally, a
:term:`falsey string` or ``False`` will turn off automatic CSRF checking
-globally on every POST request.
+globally on every request.
No matter what, CSRF checking may be explicitly enabled or disabled on a
per-view basis using the ``require_csrf`` view option. This option is of the
@@ -430,13 +431,20 @@ If ``require_csrf`` is ``True`` but does not explicitly define a token to
check, then the token name is pulled from whatever was set in the
``pyramid.require_default_csrf`` setting. Finally, if that setting does not
explicitly define a token, then ``csrf_token`` is the token required. This token
-name will be required in ``request.params`` which is a combination of the
-query string and a submitted form body.
+name will be required in ``request.POST`` which is the submitted form body.
It is always possible to pass the token in the ``X-CSRF-Token`` header as well.
There is currently no way to define an alternate name for this header without
performing CSRF checking manually.
+In addition to token based CSRF checks, the automatic CSRF checking will also
+check the referrer of the request to ensure that it matches one of the trusted
+origins. By default the only trusted origin is the current host, however
+additional origins may be configured by setting
+``pyramid.csrf_trusted_origins`` to a list of domain names (and ports if they
+are non standard). If a host in the list of domains starts with a ``.`` then
+that will allow all subdomains as well as the domain without the ``.``.
+
If CSRF checks fail then a :class:`pyramid.exceptions.BadCSRFToken` exception
will be raised. This exception may be caught and handled by an
:term:`exception view` but, by default, will result in a ``400 Bad Request``
diff --git a/docs/narr/viewconfig.rst b/docs/narr/viewconfig.rst
index e645185f5..cd5b8feb0 100644
--- a/docs/narr/viewconfig.rst
+++ b/docs/narr/viewconfig.rst
@@ -195,10 +195,11 @@ Non-Predicate Arguments
``require_csrf``
- CSRF checks only affect POST requests. Any other request methods will pass
- untouched. This option is used in combination with the
- ``pyramid.require_default_csrf`` setting to control which request parameters
- are checked for CSRF tokens.
+ CSRF checks will affect any request method that is not defined as a "safe"
+ method by RFC2616. In pratice this means that GET, HEAD, OPTIONS, and TRACE
+ methods will pass untouched and all others methods will require CSRF. This
+ option is used in combination with the ``pyramid.require_default_csrf``
+ setting to control which request parameters are checked for CSRF tokens.
This feature requires a configured :term:`session factory`.
@@ -214,6 +215,9 @@ Non-Predicate Arguments
If this option is set to ``False`` then CSRF checks will be disabled
regardless of the ``pyramid.require_default_csrf`` setting.
+ In addition, if this option is set to ``True`` or a string then CSRF origin
+ checking will be enabled.
+
See :ref:`auto_csrf_checking` for more information.
.. versionadded:: 1.7
@@ -459,7 +463,7 @@ configured view.
check name.
If CSRF checking is performed, the checked value will be the value of
- ``request.params[check_name]``. This value will be compared against the
+ ``request.POST[check_name]``. This value will be compared against the
value of ``request.session.get_csrf_token()``, and the check will pass if
these two values are the same. If the check passes, the associated view will
be permitted to execute. If the check fails, the associated view will not be
diff --git a/docs/whatsnew-1.7.rst b/docs/whatsnew-1.7.rst
index d202a4140..fd144a24a 100644
--- a/docs/whatsnew-1.7.rst
+++ b/docs/whatsnew-1.7.rst
@@ -15,8 +15,9 @@ Backwards Incompatibilities
``md5`` to ``sha512``. If you are using the authentication policy and need to
continue using ``md5``, please explicitly set ``hashalg='md5'``.
- This change means that any existing auth tickets (and associated cookies)
- will no longer be valid, users will be logged out, and have to login to their
+ If you are not currently specifying the ``hashalg`` option in your apps, then
+ this change means any existing auth tickets (and associated cookies) will no
+ longer be valid, users will be logged out, and have to login to their
accounts again.
This change has been issuing a DeprecationWarning since :app:`Pyramid` 1.4.
@@ -27,6 +28,10 @@ Backwards Incompatibilities
https://github.com/Pylons/pyramid/issues/2368 and
https://github.com/Pylons/pyramid/pull/2256
+- The :func:`pyramid.session.check_csrf_token` function no longer validates a
+ csrf token in the query string of a request. Only headers and request bodies
+ are supported. See https://github.com/Pylons/pyramid/pull/2500
+
Feature Additions
-----------------
@@ -38,21 +43,38 @@ Feature Additions
to security checks. See https://github.com/Pylons/pyramid/pull/2021
- Added a new setting, ``pyramid.require_default_csrf`` which may be used
- to turn on CSRF checks globally for every POST request in the application.
+ to turn on CSRF checks globally for every request in the application.
This should be considered a good default for websites built on Pyramid.
It is possible to opt-out of CSRF checks on a per-view basis by setting
``require_csrf=False`` on those views.
See :ref:`auto_csrf_checking` and
https://github.com/Pylons/pyramid/pull/2413
-- Added a ``require_csrf`` view option which will enforce CSRF checks on POST
- requests. If the CSRF check fails a ``BadCSRFToken`` exception will be
- raised and may be caught by exception views (the default response is a
- ``400 Bad Request``). This option should be used in place of the deprecated
- ``check_csrf`` view predicate which would normally result in unexpected
- ``404 Not Found`` response to the client instead of a catchable exception.
- See :ref:`auto_csrf_checking` and
- https://github.com/Pylons/pyramid/pull/2413
+- Added a ``require_csrf`` view option which will enforce CSRF checks on
+ requests with an unsafe method as defined by RFC2616. If the CSRF check fails
+ a ``BadCSRFToken`` exception will be raised and may be caught by exception
+ views (the default response is a ``400 Bad Request``). This option should be
+ used in place of the deprecated ``check_csrf`` view predicate which would
+ normally result in unexpected ``404 Not Found`` response to the client
+ instead of a catchable exception. See :ref:`auto_csrf_checking`,
+ https://github.com/Pylons/pyramid/pull/2413 and
+ https://github.com/Pylons/pyramid/pull/2500
+
+- Added an additional CSRF validation that checks the origin/referrer of a
+ request and makes sure it matches the current ``request.domain``. This
+ particular check is only active when accessing a site over HTTPS as otherwise
+ browsers don't always send the required information. If this additional CSRF
+ validation fails a ``BadCSRFOrigin`` exception will be raised and may be
+ caught by exception views (the default response is ``400 Bad Request``).
+ Additional allowed origins may be configured by setting
+ ``pyramid.csrf_trusted_origins`` to a list of domain names (with ports if on
+ a non standard port) to allow. Subdomains are not allowed unless the domain
+ name has been prefixed with a ``.``. See
+ https://github.com/Pylons/pyramid/pull/2501
+
+- Added a new :func:`pyramid.session.check_csrf_origin` API for validating the
+ origin or referrer headers against the request's domain.
+ See https://github.com/Pylons/pyramid/pull/2501
- Subclasses of :class:`pyramid.httpexceptions.HTTPException` will now take
into account the best match for the clients ``Accept`` header, and depending
@@ -64,7 +86,8 @@ Feature Additions
- A new event, :class:`pyramid.events.BeforeTraversal`, and interface
:class:`pyramid.interfaces.IBeforeTraversal` have been introduced that will
notify listeners before traversal starts in the router.
- See https://github.com/Pylons/pyramid/pull/2469 and
+ See :ref:`router_chapter` as well as
+ https://github.com/Pylons/pyramid/pull/2469 and
https://github.com/Pylons/pyramid/pull/1876
- A new method, :meth:`pyramid.request.Request.invoke_exception_view`, which
@@ -106,6 +129,7 @@ Scaffolding Enhancements
practices with regards to SQLAlchemy session management, as well as a more
modular approach to configuration, separating routes into a separate module
to illustrate uses of :meth:`pyramid.config.Configurator.include`.
+ See https://github.com/Pylons/pyramid/pull/2024
Documentation Enhancements
--------------------------