Skip to content

Commit 0f21d06

Browse files
authored
fix(dashboard): raise the console nginx header buffers for large OIDC tokens (#4376)
## What this PR does A user whose OIDC access token carries many group claims cannot use the console: nginx answers `400 request header or cookie too large` before the request reaches the apiserver proxy. The config baked into the console image kept the nginx defaults, 1k for `client_header_buffer_size` and four buffers of 8k for `large_client_header_buffers`, while the token arrives as one Cookie header several kilobytes long. A header field has to fit into a single buffer, so the per-buffer size is what decides this. Both directives are now 16k, at the top level of that config rather than inside the server block: a header field read before the `Host` header has selected the virtual server is measured against the default server's buffers, so the same lines inside a server block cover that server only while it stays the default one. Measured in the pinned nginx base image: a 12 KB cookie gets 400 on the old config and 200 on the new one, a 20 KB one still gets 400. `nginx -t` accepts the result. This hop sees more than the browser sent. The gatekeeper in front of it runs with `--pass-access-token=true` and `--pass-authorization-header=true`, so the request arrives carrying the `kc-access` cookie, an `Authorization: Bearer` header and `X-Forwarded-Access-Token`, three separately bounded header fields where the edge saw one. This is the console image only. The platform ingress-nginx controller also runs on the defaults, which is #2853. Worth reading that trace again before fixing it there: the controller logged `upstream_status 400`, so it forwarded the request and the 400 came back from behind the gatekeeper, which is this layer. The check in `hack/console-nginx-header-buffers.bats` reads the config out of the Containerfile heredoc and fails when either directive is missing, below 16k, or no longer ahead of the first server block. That config lives inside an image build, so no chart template and no helm-unittest case can reach it. `make unit-tests` picks the file up on its own. No image reference changes here. CI stamps the console digest. Closes #3113 ### Screenshots No UI change. This is the nginx config in front of the SPA. ### Downstream repositories The diff is the console image's nginx config plus one new `hack/*.bats` file. `cozystack-ui` is archived and the console lives in this repo, so a console change belongs in this PR. Nothing under `hack/` moved or was renamed, and no make target changed behaviour. - [x] No downstream repository is affected by this change - [ ] [cozystack/website](https://github.com/cozystack/website) - follow-up: - [ ] [cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack) - follow-up: - [ ] [cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack) - follow-up: - [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up: - [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up: - [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) - follow-up: - [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) - follow-up: - [ ] [cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server) - follow-up: - [ ] [cozystack/external-apps-example](https://github.com/cozystack/external-apps-example) - follow-up: - [ ] [cozystack/examples](https://github.com/cozystack/examples) - follow-up: - [ ] [cozystack/community](https://github.com/cozystack/community) - follow-up: ### Release note ```release-note fix(dashboard): the console no longer answers `400 request header or cookie too large` to users whose OIDC access token carries many group claims ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Increased the console’s NGINX request-header buffer capacity to support larger header fields and request headers. * Improved handling of requests with headers that previously exceeded the default buffer limits. * **Tests** * Added automated checks to verify the console’s NGINX header-buffer settings remain correctly configured. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents 8c5126c + 7d53f7a commit 0f21d06

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env bats
2+
# The console image bakes its nginx config into the Containerfile as an inline
3+
# heredoc, so no chart template and no helm-unittest case can see it. This pins
4+
# the header-buffer pair that image ships. The per-buffer size in
5+
# large_client_header_buffers is what bounds a single Cookie header: on the
6+
# default 4 8k an access token carrying many group claims does not fit one
7+
# buffer, and nginx answers 400 "request header or cookie too large" before the
8+
# request reaches the /apis, /api or /k8s/ upstream. client_header_buffer_size
9+
# is raised alongside it so such a request is read without escalating to a large
10+
# buffer at all. Either line is cheap to drop in an unrelated rewrite of that
11+
# config, and the loss is invisible until a user with a big token logs in.
12+
#
13+
# The check reads the heredoc body, not the whole Containerfile, so a directive
14+
# that moved out of the served config -- into a comment, or into a build stage
15+
# that ships nothing -- does not read as present.
16+
#
17+
# Harness note: the CI path is hack/cozytest.sh, NOT real bats. No `run`,
18+
# `$status`, `$output`, `skip` or setup()/teardown(); each @test is a shell
19+
# function under `set -eu -x`, and paths resolve from the repo root. The two
20+
# runners disagree on negative assertions, so an assertion written for bats can
21+
# be vacuous under the one that gates a merge.
22+
# Run with: hack/cozytest.sh hack/console-nginx-header-buffers.bats
23+
24+
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
25+
CONSOLE_CONTAINERFILE="packages/system/dashboard/images/console/Containerfile"
26+
27+
# Prints an nginx size in KiB, or -1 for a spelling this check cannot read.
28+
nginx_size_kib() {
29+
printf '%s\n' "$1" | awk '
30+
/^[0-9]+[kK]$/ { print int(substr($0, 1, length($0) - 1)); next }
31+
/^[0-9]+[mM]$/ { print int(substr($0, 1, length($0) - 1)) * 1024; next }
32+
/^[0-9]+$/ { print int($0 / 1024); next }
33+
{ print -1 }
34+
'
35+
}
36+
37+
@test "console nginx config reads client request headers into buffers larger than the nginx defaults" {
38+
cd "$REPO_ROOT"
39+
40+
conf=$(awk '
41+
/^COPY <<.EOF. \/etc\/nginx\/conf\.d\/default\.conf$/ { inblock = 1; next }
42+
inblock && /^EOF$/ { inblock = 0; next }
43+
inblock { print }
44+
' "$CONSOLE_CONTAINERFILE")
45+
[ -n "$conf" ] || { echo "no /etc/nginx/conf.d/default.conf heredoc found in $CONSOLE_CONTAINERFILE" >&2; exit 1; }
46+
47+
# An extraction that silently grabbed the wrong slice would let every
48+
# assertion below pass on an empty-ish config, so anchor on a line the
49+
# served config is known to carry.
50+
printf '%s\n' "$conf" | grep -q 'proxy_pass https://kubernetes.default.svc:443;' \
51+
|| { echo "extracted heredoc carries no apiserver proxy_pass; the extraction is wrong, not the config" >&2; exit 1; }
52+
53+
header_buffer=$(printf '%s\n' "$conf" | awk '$1 == "client_header_buffer_size" { sub(/;$/, "", $2); print $2 }')
54+
[ -n "$header_buffer" ] || { echo "client_header_buffer_size is absent; the console ships it at 16k next to large_client_header_buffers" >&2; exit 1; }
55+
header_buffer_kib=$(nginx_size_kib "$header_buffer")
56+
[ "$header_buffer_kib" -ge 16 ] \
57+
|| { echo "client_header_buffer_size reads as $header_buffer, and this check wants at least 16k" >&2; exit 1; }
58+
59+
# Third field, because the directive is `large_client_header_buffers <number> <size>`
60+
# and it is the per-buffer size that bounds one Cookie header.
61+
large_buffer=$(printf '%s\n' "$conf" | awk '$1 == "large_client_header_buffers" { sub(/;$/, "", $3); print $3 }')
62+
[ -n "$large_buffer" ] || { echo "large_client_header_buffers is absent: a Cookie header over 8k gets a 400 from the console nginx" >&2; exit 1; }
63+
large_buffer_kib=$(nginx_size_kib "$large_buffer")
64+
[ "$large_buffer_kib" -ge 16 ] \
65+
|| { echo "large_client_header_buffers reads as $large_buffer per buffer, and a large OIDC token needs at least 16k" >&2; exit 1; }
66+
67+
# Placement, not only presence. A header field read before the Host header
68+
# has selected the virtual server is measured against the default server's
69+
# buffers, so the same lines inside a server block cover that server only
70+
# while it stays the default one. The shipped config puts them ahead of the
71+
# first server block, and that is the shape checked here.
72+
prelude=$(printf '%s\n' "$conf" | awk '/^server[[:space:]]*{/ { exit } { print }')
73+
printf '%s\n' "$prelude" | grep -q '^client_header_buffer_size' \
74+
|| { echo "client_header_buffer_size does not stand at the top of the config, ahead of the first server block" >&2; exit 1; }
75+
printf '%s\n' "$prelude" | grep -q '^large_client_header_buffers' \
76+
|| { echo "large_client_header_buffers does not stand at the top of the config, ahead of the first server block" >&2; exit 1; }
77+
}

‎packages/system/dashboard/images/console/Containerfile‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ map $http_upgrade $connection_upgrade {
3838
'' close;
3939
}
4040

41+
# An OIDC access token with many group claims pushes the Cookie header past the
42+
# 8k default, and a header field must fit in a single buffer. Kept here rather
43+
# than inside the server block: a field read before the Host header has selected
44+
# the virtual server is measured against the default server's buffers, so the
45+
# same lines inside a server block cover this one only while it stays default.
46+
client_header_buffer_size 16k;
47+
large_client_header_buffers 4 16k;
48+
4149
server {
4250
listen 8080 default_server;
4351
listen [::]:8080 default_server;

0 commit comments

Comments
 (0)