From 153846f8fa8d7af92d463e8f71766fd1a833ed75 Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Sun, 9 Feb 2020 12:22:37 +0100 Subject: Verify that integer parameters are integers at an early stage Previously we'd in many places pass down the value directly from get or post requests to a lower layer, only to have that layer throw an exception because it wasn't an integer, or we'd ust wrap it in int() which also causes a hard exception when it's not an integer. Instead create a small wrapper for get_int_or_error() which can be called with a parameter that's supposed to be integer, and will then just return a 404 if the parameter doesn't exist or is not an integer. These are all "should never happen" scenarios, so not generating hard crashes and stackdumps are an improvement. None of these were places where the actual bad data would get anywyhere, they would all just cause an ugly exception, but should get fixed regardless. One or two instances spotted by Daniel Gustafsson, and then a lot of grep to try to find most of the rest. --- postgresqleu/util/request.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 postgresqleu/util/request.py (limited to 'postgresqleu/util/request.py') diff --git a/postgresqleu/util/request.py b/postgresqleu/util/request.py new file mode 100644 index 00000000..878763d5 --- /dev/null +++ b/postgresqleu/util/request.py @@ -0,0 +1,14 @@ +from django.http import Http404 + + +def get_int_or_error(reqmap, paramname, default=None): + if paramname not in reqmap: + if default: + return default + raise Http404("Parameter {} missing".format(paramname)) + + p = reqmap.get(paramname) + if not p.isnumeric(): + raise Http404("Parameter {} is not an integer".format(paramname)) + + return int(p) -- cgit v1.2.3