We have clarified our Privacy Statement. Please have a look at our changes.
Browse Source

handle trailing slash in path and redirect

master
Henning Jacobs 6 days ago
parent
commit
de68dad065
2 changed files with 18 additions and 0 deletions
  1. +11
    -0
      kube_web/web.py
  2. +7
    -0
      tests/e2e/test_view.py

+ 11
- 0
kube_web/web.py View File

@@ -1554,6 +1554,16 @@ async def error_handler(request, handler):
return response


@web.middleware
async def trailing_slash(request, handler):
path = request.url.path
if path != "/" and path.endswith("/"):
redirect = request.url.with_path(path[:-1]).with_query(request.url.query)
raise web.HTTPFound(location=redirect)
response = await handler(request)
return response


def get_app(cluster_manager, config):
templates_paths = [str(Path(__file__).parent / "templates")]
if config.templates_path:
@@ -1615,6 +1625,7 @@ def get_app(cluster_manager, config):
app.middlewares.append(auth)

app.middlewares.append(error_handler)
app.middlewares.append(trailing_slash)

app[CLUSTER_MANAGER] = cluster_manager
app[CONFIG] = config

+ 7
- 0
tests/e2e/test_view.py View File

@@ -10,6 +10,13 @@ def test_view_namespace(session):
assert title.text == "default (Namespace) - Kubernetes Web View"


def test_view_namespace_trailing_slash(session):
response = session.get("/clusters/local/namespaces/default/")
response.raise_for_status()
title = response.html.find("title", first=True)
assert title.text == "default (Namespace) - Kubernetes Web View"


def test_view_namespace_forbidden(session):
response = session.get("/clusters/local/namespaces/my-forbidden-namespace")
assert response.status_code == 403

Loading…
Cancel
Save