From fb718c6c47a65cf1008bfa9adde6b24de707b1bb Mon Sep 17 00:00:00 2001 From: Thomas NOEL Date: Tue, 15 Jan 2019 11:59:09 +0100 Subject: [PATCH] public: do not redirect if template rendering is empty (#29763) Also, on template error, raise a 404 with a debug message. --- combo/public/views.py | 8 ++++++-- tests/test_public.py | 13 +++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/combo/public/views.py b/combo/public/views.py index 99fd1a97..60fb0829 100644 --- a/combo/public/views.py +++ b/combo/public/views.py @@ -446,8 +446,12 @@ def publish_page(request, page, status=200, template_name=None): raise PermissionDenied() if page.redirect_url: - return HttpResponseRedirect( - page.get_redirect_url(context=request.extra_context_data)) + try: + redirect_url = page.get_redirect_url(context=request.extra_context_data) + except utils.TemplateError: + raise Http404("combo: can't compute redirect URL (template error).") + if redirect_url: + return HttpResponseRedirect(redirect_url) cells = CellBase.get_cells(page=page) extend_with_parent_cells(cells, hierarchy=pages) diff --git a/tests/test_public.py b/tests/test_public.py index bbf09389..ad7b7196 100644 --- a/tests/test_public.py +++ b/tests/test_public.py @@ -197,6 +197,19 @@ def test_page_templated_redirect(app): resp = app.get('/elsewhere/', status=302) assert resp.location == 'http://example.net' + page.redirect_url = '{{test_url}}/ok' + page.save() + resp = app.get('/elsewhere/', status=302) + assert resp.location == 'http://example.net/ok' + + page.redirect_url = '{{unknown_variable}}' + page.save() + resp = app.get('/elsewhere/', status=200) + + page.redirect_url = '{% if error %}' + page.save() + resp = app.get('/elsewhere/', status=404) + def test_page_private_unlogged(app): Page.objects.all().delete() page = Page(title='Home', slug='index', template_name='standard', public=False)