Permalink
Browse files

Fixed #14156 -- Modified the way CSRF protection is applied to flatpa…

…ges so that the flatpage middleware doesn't cause all POSTs resulting in 404s to turn into 403s. Thanks to patrys for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13641 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 8ce4a19 commit 3f96a7a358895a90286c8752ebcc6b10ed426443 @freakboy3742 freakboy3742 committed Aug 27, 2010
@@ -0,0 +1,32 @@
+[
+ {
+ "pk": 1,
+ "model": "flatpages.flatpage",
+ "fields": {
+ "registration_required": false,
+ "title": "A Flatpage",
+ "url": "/flatpage/",
+ "template_name": "",
+ "sites": [
+ 1
+ ],
+ "content": "Isn't it flat!",
+ "enable_comments": false
+ }
+ },
+ {
+ "pk": 2,
+ "model": "flatpages.flatpage",
+ "fields": {
+ "registration_required": true,
+ "title": "Sekrit Flatpage",
+ "url": "/sekrit/",
+ "template_name": "",
+ "sites": [
+ 1
+ ],
+ "content": "Isn't it sekrit!",
+ "enable_comments": false
+ }
+ }
+]
@@ -0,0 +1,3 @@
+from django.contrib.flatpages.tests.csrf import *
+from django.contrib.flatpages.tests.middleware import *
+from django.contrib.flatpages.tests.views import *
@@ -0,0 +1,70 @@
+import os
+from django.conf import settings
+from django.test import TestCase, Client
+
+class FlatpageCSRFTests(TestCase):
+ fixtures = ['sample_flatpages']
+ urls = 'django.contrib.flatpages.tests.urls'
+
+ def setUp(self):
+ self.client = Client(enforce_csrf_checks=True)
+ self.old_MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
+ flatpage_middleware_class = 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
+ csrf_middleware_class = 'django.middleware.csrf.CsrfViewMiddleware'
+ if csrf_middleware_class not in settings.MIDDLEWARE_CLASSES:
+ settings.MIDDLEWARE_CLASSES += (csrf_middleware_class,)
+ if flatpage_middleware_class not in settings.MIDDLEWARE_CLASSES:
+ settings.MIDDLEWARE_CLASSES += (flatpage_middleware_class,)
+ self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
+ settings.TEMPLATE_DIRS = (
+ os.path.join(
+ os.path.dirname(__file__),
+ 'templates'
+ ),
+ )
+
+ def tearDown(self):
+ settings.MIDDLEWARE_CLASSES = self.old_MIDDLEWARE_CLASSES
+ settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
+
+ def test_view_flatpage(self):
+ "A flatpage can be served through a view, even when the middleware is in use"
+ response = self.client.get('/flatpage_root/flatpage/')
+ self.assertEquals(response.status_code, 200)
+ self.assertContains(response, "<p>Isn't it flat!</p>")
+
+ def test_view_non_existent_flatpage(self):
+ "A non-existent flatpage raises 404 when served through a view, even when the middleware is in use"
+ response = self.client.get('/flatpage_root/no_such_flatpage/')
+ self.assertEquals(response.status_code, 404)
+
+ def test_view_authenticated_flatpage(self):
+ "A flatpage served through a view can require authentication"
+ response = self.client.get('/flatpage_root/sekrit/')
+ self.assertRedirects(response, '/accounts/login/?next=/flatpage_root/sekrit/')
+
+ def test_fallback_flatpage(self):
+ "A flatpage can be served by the fallback middlware"
+ response = self.client.get('/flatpage/')
+ self.assertEquals(response.status_code, 200)
+ self.assertContains(response, "<p>Isn't it flat!</p>")
+
+ def test_fallback_non_existent_flatpage(self):
+ "A non-existent flatpage raises a 404 when served by the fallback middlware"
+ response = self.client.get('/no_such_flatpage/')
+ self.assertEquals(response.status_code, 404)
+
+ def test_post_view_flatpage(self):
+ "POSTing to a flatpage served through a view will raise a CSRF error if no token is provided (Refs #14156)"
+ response = self.client.post('/flatpage_root/flatpage/')
+ self.assertEquals(response.status_code, 403)
+
+ def test_post_fallback_flatpage(self):
+ "POSTing to a flatpage served by the middleware will raise a CSRF error if no token is provided (Refs #14156)"
+ response = self.client.post('/flatpage/')
+ self.assertEquals(response.status_code, 403)
+
+ def test_post_unknown_page(self):
+ "POSTing to an unknown page isn't caught as a 403 CSRF error"
+ response = self.client.post('/no_such_page/')
+ self.assertEquals(response.status_code, 404)
@@ -0,0 +1,56 @@
+import os
+from django.conf import settings
+from django.test import TestCase
+
+class FlatpageMiddlewareTests(TestCase):
+ fixtures = ['sample_flatpages']
+ urls = 'django.contrib.flatpages.tests.urls'
+
+ def setUp(self):
+ self.old_MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
+ flatpage_middleware_class = 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
+ if flatpage_middleware_class not in settings.MIDDLEWARE_CLASSES:
+ settings.MIDDLEWARE_CLASSES += (flatpage_middleware_class,)
+ self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
+ settings.TEMPLATE_DIRS = (
+ os.path.join(
+ os.path.dirname(__file__),
+ 'templates'
+ ),
+ )
+
+ def tearDown(self):
+ settings.MIDDLEWARE_CLASSES = self.old_MIDDLEWARE_CLASSES
+ settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
+
+ def test_view_flatpage(self):
+ "A flatpage can be served through a view, even when the middleware is in use"
+ response = self.client.get('/flatpage_root/flatpage/')
+ self.assertEquals(response.status_code, 200)
+ self.assertContains(response, "<p>Isn't it flat!</p>")
+
+ def test_view_non_existent_flatpage(self):
+ "A non-existent flatpage raises 404 when served through a view, even when the middleware is in use"
+ response = self.client.get('/flatpage_root/no_such_flatpage/')
+ self.assertEquals(response.status_code, 404)
+
+ def test_view_authenticated_flatpage(self):
+ "A flatpage served through a view can require authentication"
+ response = self.client.get('/flatpage_root/sekrit/')
+ self.assertRedirects(response, '/accounts/login/?next=/flatpage_root/sekrit/')
+
+ def test_fallback_flatpage(self):
+ "A flatpage can be served by the fallback middlware"
+ response = self.client.get('/flatpage/')
+ self.assertEquals(response.status_code, 200)
+ self.assertContains(response, "<p>Isn't it flat!</p>")
+
+ def test_fallback_non_existent_flatpage(self):
+ "A non-existent flatpage raises a 404 when served by the fallback middlware"
+ response = self.client.get('/no_such_flatpage/')
+ self.assertEquals(response.status_code, 404)
+
+ def test_fallback_authenticated_flatpage(self):
+ "A flatpage served by the middleware can require authentication"
+ response = self.client.get('/sekrit/')
+ self.assertRedirects(response, '/accounts/login/?next=/sekrit/')
@@ -0,0 +1 @@
+<h1>Oh Noes!</h1>
@@ -0,0 +1,2 @@
+<h1>{{ flatpage.title }}</h1>
+<p>{{ flatpage.content }}</p>
@@ -0,0 +1,8 @@
+from django.conf.urls.defaults import *
+
+# special urls for flatpage test cases
+urlpatterns = patterns('',
+ (r'^flatpage_root', include('django.contrib.flatpages.urls')),
+ (r'^accounts/', include('django.contrib.auth.urls')),
+)
+
@@ -0,0 +1,50 @@
+import os
+from django.conf import settings
+from django.test import TestCase
+
+class FlatpageViewTests(TestCase):
+ fixtures = ['sample_flatpages']
+ urls = 'django.contrib.flatpages.tests.urls'
+
+ def setUp(self):
+ self.old_MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
+ flatpage_middleware_class = 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
+ if flatpage_middleware_class in settings.MIDDLEWARE_CLASSES:
+ settings.MIDDLEWARE_CLASSES = tuple(m for m in settings.MIDDLEWARE_CLASSES if m != flatpage_middleware_class)
+ self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
+ settings.TEMPLATE_DIRS = (
+ os.path.join(
+ os.path.dirname(__file__),
+ 'templates'
+ ),
+ )
+ settings.DEBUG = True
+ def tearDown(self):
+ settings.MIDDLEWARE_CLASSES = self.old_MIDDLEWARE_CLASSES
+ settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
+
+ def test_view_flatpage(self):
+ "A flatpage can be served through a view"
+ response = self.client.get('/flatpage_root/flatpage/')
+ self.assertEquals(response.status_code, 200)
+ self.assertContains(response, "<p>Isn't it flat!</p>")
+
+ def test_view_non_existent_flatpage(self):
+ "A non-existent flatpage raises 404 when served through a view"
+ response = self.client.get('/flatpage_root/no_such_flatpage/')
+ self.assertEquals(response.status_code, 404)
+
+ def test_view_authenticated_flatpage(self):
+ "A flatpage served through a view can require authentication"
+ response = self.client.get('/flatpage_root/sekrit/')
+ self.assertRedirects(response, '/accounts/login/?next=/flatpage_root/sekrit/')
+
+ def test_fallback_flatpage(self):
+ "A fallback flatpage won't be served if the middleware is disabled"
+ response = self.client.get('/flatpage/')
+ self.assertEquals(response.status_code, 404)
+
+ def test_fallback_non_existent_flatpage(self):
+ "A non-existent flatpage won't be served if the fallback middlware is disabled"
+ response = self.client.get('/no_such_flatpage/')
+ self.assertEquals(response.status_code, 404)
@@ -13,10 +13,13 @@
# when a 404 is raised, which often means CsrfViewMiddleware.process_view
# has not been called even if CsrfViewMiddleware is installed. So we need
# to use @csrf_protect, in case the template needs {% csrf_token %}.
-@csrf_protect
+# However, we can't just wrap this view; if no matching flatpage exists,
+# or a redirect is required for authentication, the 404 needs to be returned
+# without any CSRF checks. Therefore, we only
+# CSRF protect the internal implementation.
def flatpage(request, url):
"""
- Flat page view.
+ Public interface to the flat page view.
Models: `flatpages.flatpages`
Templates: Uses the template defined by the ``template_name`` field,
@@ -30,6 +33,13 @@ def flatpage(request, url):
if not url.startswith('/'):
url = "/" + url
f = get_object_or_404(FlatPage, url__exact=url, sites__id__exact=settings.SITE_ID)
+ return render_flatpage(request, f)
+
+@csrf_protect
+def render_flatpage(request, f):
+ """
+ Internal interface to the flat page view.
+ """
# If registration is required for accessing this page, and the user isn't
# logged in, redirect to the login page.
if f.registration_required and not request.user.is_authenticated():

0 comments on commit 3f96a7a

Please sign in to comment.