Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Reverting history is only possible with a POST

Roland Geider1a7bb4c7f3f0
diff --git a/wger/exercises/templates/history/overview.html b/wger/exercises/templates/history/overview.html
index 8c747e5b1..a28f7b1eb 100644
--- a/wger/exercises/templates/history/overview.html
+++ b/wger/exercises/templates/history/overview.html
@@ -151,11 +151,14 @@
<h4 class="modal-title">{% translate 'Are you sure?' %}</h4>
</div>
<div class="modal-body">
- <a href="{% url 'exercise:history:revert' event.history_id event.content_type_id %}"
- id="view-changes"
- class="btn btn-danger btn-sm btn-block">
- {% translate "Yes" %}
- </a>
+ <form method="post"
+ action="{% url 'exercise:history:revert' event.history_id event.content_type_id %}">
+ {% csrf_token %}
+ <button type="submit"
+ class="btn btn-danger btn-sm btn-block">
+ {% translate "Yes" %}
+ </button>
+ </form>
</div>
<div class="modal-footer">
<button type="button"
diff --git a/wger/exercises/tests/test_history.py b/wger/exercises/tests/test_history.py
index 550c4676c..73762607f 100644
--- a/wger/exercises/tests/test_history.py
+++ b/wger/exercises/tests/test_history.py
@@ -66,7 +66,7 @@ class ExerciseHistoryControl(WgerTestCase):
def test_admin_revert_view(self):
"""
- Test that revert is accessible
+ Test that revert is accessible via POST
"""
self.user_login()
@@ -78,7 +78,7 @@ class ExerciseHistoryControl(WgerTestCase):
translation.description = 'Very cool exercise!'
translation.save()
- self.client.get(
+ response = self.client.post(
reverse(
'exercise:history:revert',
kwargs={
@@ -88,9 +88,38 @@ class ExerciseHistoryControl(WgerTestCase):
)
)
+ self.assertEqual(response.status_code, 302)
translation = Translation.objects.get(pk=2)
self.assertEqual(translation.description, 'Boring exercise')
+ def test_admin_revert_rejects_get(self):
+ """
+ Revert is state-changing and must not be reachable via GET; only POST
+ with a CSRF token is allowed.
+ """
+ self.user_login()
+
+ translation = Translation.objects.get(pk=2)
+ translation.description = 'Boring exercise'
+ translation.save()
+ most_recent_history = translation.history.order_by('history_date').last()
+ translation.description = 'Very cool exercise!'
+ translation.save()
+
+ response = self.client.get(
+ reverse(
+ 'exercise:history:revert',
+ kwargs={
+ 'history_pk': most_recent_history.history_id,
+ 'content_type_id': ContentType.objects.get_for_model(translation).id,
+ },
+ )
+ )
+
+ self.assertEqual(response.status_code, 405)
+ translation = Translation.objects.get(pk=2)
+ self.assertEqual(translation.description, 'Very cool exercise!')
+
class ExerciseHistoryControlExtras(WgerTestCase):
"""
diff --git a/wger/exercises/views/history.py b/wger/exercises/views/history.py
index 3ec1bd420..1580b1fe3 100644
--- a/wger/exercises/views/history.py
+++ b/wger/exercises/views/history.py
@@ -36,6 +36,7 @@ from django.shortcuts import (
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
+from django.views.decorators.http import require_POST
# Third Party
import django_filters
@@ -262,6 +263,7 @@ def control(request: HttpRequest) -> HttpResponse:
)
+@require_POST
@permission_required('exercises.change_exercise')
def history_revert(request, history_pk, content_type_id):
"""