Trap invalid URLs for submission forms
authorMagnus Hagander <magnus@hagander.net>
Sun, 26 Feb 2017 15:09:36 +0000 (16:09 +0100)
committerMagnus Hagander <magnus@hagander.net>
Sun, 26 Feb 2017 15:09:36 +0000 (16:09 +0100)
I still haven't figured out where these come from, but generate a proper
404 when the URL is malformatted rather than trying to render it and
crash with an exception

pgweb/util/helpers.py

index 71134bd2677f4c3f26bd6836365513154fd3ab27..a203b0efd5e4050e8321e684cb5346b9fe2e6f50 100644 (file)
@@ -1,6 +1,6 @@
 from django.shortcuts import render_to_response, get_object_or_404
 from pgweb.util.contexts import NavContext
-from django.http import HttpResponseRedirect
+from django.http import HttpResponseRedirect, Http404
 from django.template import Context
 from django.template.loader import get_template
 import django.utils.xmlutils
@@ -10,6 +10,10 @@ def simple_form(instancetype, itemid, request, formclass, formtemplate='base/for
                instance = instancetype()
        else:
                # Regular form item, attempt to edit it
+               try:
+                       i = int(itemid)
+               except ValueError:
+                       raise Http404("Invalid URL")
                if createifempty:
                        (instance, wascreated) = instancetype.objects.get_or_create(pk=itemid)
                else: