From 13d4b0cf3cd3ea544fd8ff504cf2c58420b8e2d8 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sat, 9 Jul 2022 12:06:32 +0200 Subject: add CSRF protection for actions that modify data --- fietsboek/__init__.py | 3 +++ fietsboek/templates/admin.jinja2 | 3 +++ fietsboek/templates/details.jinja2 | 2 ++ fietsboek/templates/edit.jinja2 | 2 ++ fietsboek/templates/finish_upload.jinja2 | 7 ++++++- fietsboek/templates/profile.jinja2 | 6 ++++++ fietsboek/templates/upload.jinja2 | 2 ++ fietsboek/templates/util.jinja2 | 4 ++++ fietsboek/views/upload.py | 2 +- 9 files changed, 29 insertions(+), 2 deletions(-) diff --git a/fietsboek/__init__.py b/fietsboek/__init__.py index 6ed9028..4a28dd4 100644 --- a/fietsboek/__init__.py +++ b/fietsboek/__init__.py @@ -4,6 +4,7 @@ For more information, see the README or the included documentation. """ from pyramid.config import Configurator from pyramid.session import SignedCookieSessionFactory +from pyramid.csrf import CookieCSRFStoragePolicy from pyramid.settings import asbool, aslist from pyramid.i18n import default_locale_negotiator @@ -59,6 +60,8 @@ def main(global_config, **settings): config.add_translation_dirs('fietsboek:locale/') config.set_session_factory(my_session_factory) config.set_security_policy(SecurityPolicy()) + config.set_csrf_storage_policy(CookieCSRFStoragePolicy()) + config.set_default_csrf_options(require_csrf=True) config.set_locale_negotiator(locale_negotiator) jinja2_env = config.get_jinja2_environment() diff --git a/fietsboek/templates/admin.jinja2 b/fietsboek/templates/admin.jinja2 index 47c1832..3201d8d 100644 --- a/fietsboek/templates/admin.jinja2 +++ b/fietsboek/templates/admin.jinja2 @@ -18,12 +18,14 @@
+ {{ util.hidden_csrf_input() }}
+ {{ util.hidden_csrf_input() }}
@@ -39,6 +41,7 @@ + {{ util.hidden_csrf_input() }} diff --git a/fietsboek/templates/details.jinja2 b/fietsboek/templates/details.jinja2 index 741c488..f83ecb1 100644 --- a/fietsboek/templates/details.jinja2 +++ b/fietsboek/templates/details.jinja2 @@ -23,6 +23,7 @@ {% endif %} diff --git a/fietsboek/templates/edit.jinja2 b/fietsboek/templates/edit.jinja2 index 9d48cf1..6053509 100644 --- a/fietsboek/templates/edit.jinja2 +++ b/fietsboek/templates/edit.jinja2 @@ -1,6 +1,7 @@ {% extends "layout.jinja2" %} {% import "edit_form.jinja2" as edit_form %} +{% import "util.jinja2" as util with context %} {% block content %}
@@ -10,6 +11,7 @@
{{ edit_form.edit_track(track.title, track.date_raw, track.date_tz or 0, track.visibility, track.description, track.text_tags(), badges, track.tagged_people) }} + {{ util.hidden_csrf_input() }}
{{ _("page.edit.form.cancel") }} diff --git a/fietsboek/templates/finish_upload.jinja2 b/fietsboek/templates/finish_upload.jinja2 index 66ba926..58c67d4 100644 --- a/fietsboek/templates/finish_upload.jinja2 +++ b/fietsboek/templates/finish_upload.jinja2 @@ -1,6 +1,7 @@ {% extends "layout.jinja2" %} {% import "edit_form.jinja2" as edit_form %} +{% import "util.jinja2" as util with context %} {% block content %}
@@ -10,10 +11,14 @@
{{ edit_form.edit_track(upload_title, upload_date, upload_date_tz, upload_visibility, upload_description, upload_tags, badges, upload_tagged_people) }} + {{ util.hidden_csrf_input() }}
- {{ _("page.upload.form.cancel") }} +
+
+ {{ util.hidden_csrf_input() }} +
{% endblock %} diff --git a/fietsboek/templates/profile.jinja2 b/fietsboek/templates/profile.jinja2 index d8722ef..8b1c748 100644 --- a/fietsboek/templates/profile.jinja2 +++ b/fietsboek/templates/profile.jinja2 @@ -1,5 +1,7 @@ {% extends "layout.jinja2" %} +{% import "util.jinja2" as util with context %} + {% block content %}

{{ _("page.my_profile.title") }}

@@ -27,6 +29,7 @@
+ {{ util.hidden_csrf_input() }} @@ -39,6 +42,7 @@
  • + {{ util.hidden_csrf_input() }}
    {{ friend.name }} ({{ friend.email }}) @@ -48,6 +52,7 @@
  • + {{ util.hidden_csrf_input() }}
    {{ friend_request.sender.name }} ({{ friend_request.sender.email }}) @@ -60,6 +65,7 @@
    + {{ util.hidden_csrf_input() }}
    diff --git a/fietsboek/templates/upload.jinja2 b/fietsboek/templates/upload.jinja2 index b3b4ddc..6406e91 100644 --- a/fietsboek/templates/upload.jinja2 +++ b/fietsboek/templates/upload.jinja2 @@ -1,4 +1,5 @@ {% extends "layout.jinja2" %} +{% import "util.jinja2" as util with context %} {% block content %}
    @@ -8,6 +9,7 @@
    + {{ util.hidden_csrf_input() }}
    diff --git a/fietsboek/templates/util.jinja2 b/fietsboek/templates/util.jinja2 index d334473..7b39477 100644 --- a/fietsboek/templates/util.jinja2 +++ b/fietsboek/templates/util.jinja2 @@ -1,3 +1,7 @@ {% macro render_badge(badge) -%}
    {%- endmacro %} + +{% macro hidden_csrf_input() %} + +{% endmacro %} diff --git a/fietsboek/views/upload.py b/fietsboek/views/upload.py index 9e38069..4db0cd6 100644 --- a/fietsboek/views/upload.py +++ b/fietsboek/views/upload.py @@ -173,7 +173,7 @@ def do_finish_upload(request): return HTTPFound(request.route_url('details', track_id=track.id)) -@view_config(route_name='cancel-upload', permission='upload.finish') +@view_config(route_name='cancel-upload', permission='upload.finish', request_method="POST") def cancel_upload(request): """Cancels the upload and clears the temporary data. -- cgit v1.2.3