diff options
author | Magnus Hagander | 2023-07-12 14:58:54 +0000 |
---|---|---|
committer | Magnus Hagander | 2023-07-12 14:58:54 +0000 |
commit | 2b408572b68a4facd12bcc68e2c0b4c013a7738b (patch) | |
tree | b4dc22859bdae8c37ae1ed88d1cec7431340c1a1 /postgresqleu/static/views.py | |
parent | 5fed85e3d9707a07caf8a39758ab959056c0f0b3 (diff) |
Implement static file URL filtering from pgweb
This filters filenames to be regular ascii characters, and sets a
max length for URLs. Changes the errors for some broken urls and
attempted-scriptkiddie-exploits to just be a not found instead fo an
internal server error.
Diffstat (limited to 'postgresqleu/static/views.py')
-rw-r--r-- | postgresqleu/static/views.py | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/postgresqleu/static/views.py b/postgresqleu/static/views.py index 9c04dd05..d45cbfb2 100644 --- a/postgresqleu/static/views.py +++ b/postgresqleu/static/views.py @@ -1,6 +1,10 @@ from django.http import HttpResponse, Http404 from django.template import loader, TemplateDoesNotExist +import re + +re_staticfilenames = re.compile("^[0-9A-Z/_-]+$", re.IGNORECASE) + # Fallback handler for URLs not matching anything else. Fall them # back to a static template. If that one is not found, send a 404 @@ -10,6 +14,14 @@ def static_fallback(request, url): if url.find('..') > -1: raise Http404('Page not found') + if not re_staticfilenames.match(url): + raise Http404('Page not found.') + + if len(url) > 250: + # Maximum length is really per-directory, but we shouldn't have any pages/fallback + # urls with anywhere *near* that, so let's just limit it on the whole + raise Http404('Page not found.') + try: t = loader.get_template('pages/%s.html' % url) return HttpResponse(t.render()) |