diff options
author | Magnus Hagander | 2020-02-09 11:22:37 +0000 |
---|---|---|
committer | Magnus Hagander | 2020-02-09 11:22:37 +0000 |
commit | 153846f8fa8d7af92d463e8f71766fd1a833ed75 (patch) | |
tree | 4b094ec623840c8bdad1d359db96efcc0e760e2b /postgresqleu/util/request.py | |
parent | 7b6bca146d560a4c613ef5b3d400e0f3329a60a6 (diff) |
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.
Diffstat (limited to 'postgresqleu/util/request.py')
-rw-r--r-- | postgresqleu/util/request.py | 14 |
1 files changed, 14 insertions, 0 deletions
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) |