From 777721f5f712b9094c742c381cb2b3bf6d295557 Mon Sep 17 00:00:00 2001 From: Alexey Artamonov Date: Tue, 1 Dec 2026 16:13:33 +0300 Subject: [PATCH] fix(dashboard): keep a slow re-login from failing on an expired CSRF cookie After a logout the console lands straight on the Keycloak login page. If that page stays open for more than 15 minutes before the password is entered, the callback fails with a 403 and the gatekeeper logs unable to obtain CSRF cookie: ... CSRF cookie with name 'kc-access__csrf' was not found oauth2-proxy gives the CSRF cookie a fixed Max-Age of --cookie-csrf-expire (15m by default), while Keycloak keeps a login page usable for 30 minutes and then restarts the flow with the original state, so the browser drops the cookie while the login it belongs to is still alive. The lifetime becomes 1h, which outlives Keycloak's login lifespan. It stays finite and well below --cookie-expire (168h) on purpose: a CSRF cookie with an older signature fails validation, and the per-request limit skips such cookies instead of deleting them, so a cookie the browser keeps longer than that would stay in the request headers. Tested by running the chart-rendered oauth2-proxy v7.12.0 config against a live Keycloak, waiting in real time: after a logout, a login page left open for 16 minutes fails with the 403 above on the default lifetime and signs in once the cookie outlives the wait; at 31 minutes Keycloak asks for the password again and the login then completes. The same binary sets Max-Age=3600 on the CSRF cookie with this flag. Assisted-by: LLM Signed-off-by: Alexey Artamonov --- .../system/dashboard/templates/gatekeeper.yaml | 6 ++++++ .../system/dashboard/tests/gatekeeper_test.yaml | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/packages/system/dashboard/templates/gatekeeper.yaml b/packages/system/dashboard/templates/gatekeeper.yaml index aa4a6b2a8..ecebdaf72 100644 --- a/packages/system/dashboard/templates/gatekeeper.yaml +++ b/packages/system/dashboard/templates/gatekeeper.yaml @@ -76,6 +76,12 @@ spec: # abandoned flows from piling up in the request headers. - --cookie-csrf-per-request=true - --cookie-csrf-per-request-limit=10 + # Keycloak accepts a login page for 30m and then restarts it with the + # original state, so the default 15m fails the callback of a login + # page left open longer. Keep it finite and below the 168h + # --cookie-expire: older CSRF cookies fail signature checks, and + # the per-request limit skips them instead of deleting them. + - --cookie-csrf-expire=1h - --skip-provider-button {{- if eq $oidcInsecureSkipVerify "true" }} - --ssl-insecure-skip-verify=true diff --git a/packages/system/dashboard/tests/gatekeeper_test.yaml b/packages/system/dashboard/tests/gatekeeper_test.yaml index ff2ad9d2d..d6ad496c3 100644 --- a/packages/system/dashboard/tests/gatekeeper_test.yaml +++ b/packages/system/dashboard/tests/gatekeeper_test.yaml @@ -221,3 +221,20 @@ tests: - contains: path: spec.template.spec.containers[0].args content: --cookie-csrf-per-request-limit=10 + + # The CSRF cookie has to outlive Keycloak's 30m login lifespan, or a login page + # left open that long fails its callback, and stay finite and below the 168h + # signature lifetime, past which oauth2-proxy can neither read nor prune it. + - it: keeps the CSRF cookie for an hour + template: templates/gatekeeper.yaml + set: + _cluster: + root-host: example.org + oidc-enabled: "true" + asserts: + - contains: + path: spec.template.spec.containers[0].args + content: --cookie-csrf-expire=1h + - notContains: + path: spec.template.spec.containers[0].args + content: --cookie-csrf-expire=0