Repository navigation
Deny container attach unless POST=1 - #58
Conversation
|
Thanks for opening this pull request! Be sure to follow the pull request template! |
|
This is an upstream issue. Next time tell your LLM/AI to check the repo correctly - it would have came across the following line; https://github.com/haproxy/haproxy |
Hi, I don't think this is an HAProxy issue. Line 10 only shows which HAProxy version is in the image. HAProxy just follows the rules in this repo's |
|
Correction to my earlier comment: this isn't an upstream issue. HAProxy only enforces what I reproduced the gap. I also tested what this PR does to Setup:
|
| Request | main | this PR | narrow rule |
|---|---|---|---|
GET /containers/{id}/json |
200 | 200 | 200 |
GET /containers/{id}/logs?stdout=1 |
403 | 403 | 403 |
GET /containers/{id}/attach/ws?logs=1&stdout=1 |
101, replays the whole log | 403 | 403 |
GET /containers/{id}/attach/ws?stream=1&stdin=1&stdout=1, sending id -un to a container started with -i |
101, returns root |
403 | 403 |
Both fixes close the gap. The last row is the worst part: with POST=0, which is meant to be read-only, main lets a client type into a container's stdin.
POST=1 (CONTAINERS, IMAGES, NETWORKS, VOLUMES, INFO = 1)
| Through the proxy | main | this PR | narrow rule |
|---|---|---|---|
docker run alpine echo run-ok |
run-ok |
unable to upgrade to tcp, received 403, exit 1 |
run-ok |
docker run -d alpine true |
works | works | works |
docker start -a <container> |
prints its output | unable to upgrade to tcp, received 403, exit 1 |
prints its output |
docker compose up |
works | HAProxy's 403 Forbidden page after Attaching to hello-1, exit 1 |
works |
POST /containers/{id}/attach?logs=1&stream=0 |
replays the log | 403 | replays the log |
How an existing setup breaks: take a deploy or CI container that drives Docker through the proxy:
services:
socket-proxy:
image: lscr.io/linuxserver/socket-proxy:latest
environment:
- CONTAINERS=1
- POST=1
- IMAGES=1
- NETWORKS=1
- VOLUMES=1
- INFO=1
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
deployer:
image: docker:cli
environment:
- DOCKER_HOST=tcp://socket-proxy:2375Today, docker compose up and docker run <image> <cmd> from deployer work. Once the image with this PR is pulled, both fail with a 403 as soon as they attach, and nothing in the user's config changed. The Docker CLI and Compose send POST /containers/{id}/attach?stream=1&stdout=1&stderr=1 and never logs=1 (cli/command/container/run.go, start.go and attach.go in docker/cli; internal/logstream/attach.go in docker/compose sets Logs: false). So the PR blocks the legitimate use of attach in order to stop the log replay, which no client relies on.
There's a second trap. When attach fails, Compose falls back to GET /containers/{id}/logs. A user who hits the 403 and sets ALLOW_LOGS=1 gets docker compose up to exit 0, but the service's output never shows up. In two runs out of two, compose-ok was missing. They've also opened /logs on every container, which is more access than they had before. Foreground docker run and docker start -a still fail.
Narrow fix
http-request deny if { path,url_dec -m reg -i ^(/v[\d\.]+)?(/libpod)?/containers/[a-zA-Z0-9_.-]+/(exec) } !allow_exec
+
+ # attach can write to a container's stdin and replay its log, so it needs POST=1
+ # even in its GET (WebSocket) form, /containers/{id}/attach/ws
+ http-request deny if { path,url_dec -m reg -i ^(/v[\d\.]+)?(/libpod)?/containers/[a-zA-Z0-9_.-]+/attach } !{ env(POST) -m bool }The path match is the same as this PR's. The difference is that it's gated on POST, not on a new variable that defaults to 0. Attach is a write operation even when it arrives as a GET, so it follows POST. With POST=0 the gap is closed (first table). With POST=1 nothing changes (second table). No new env var and no Dockerfile or README parameter changes are needed, only a changelog line.
What it leaves open: with POST=1, POST /containers/{id}/attach?logs=1 still replays a log while ALLOW_LOGS=0. I don't think that's worth breaking Compose over. With CONTAINERS=1 and POST=1, a client can already create and start a container with the host filesystem bind-mounted. start falls through to the /containers allow rule when ALLOW_START=0, which is why docker run -d worked in every column above. So ALLOW_LOGS isn't a real boundary in that mode. Filtering on the logs= query string in HAProxy would also be easy to get around, because Docker treats any value other than "", 0, no, false or none as true (BoolValue in daemon/server/httputils/form.go).
If you'd still like a switch for POST=1 users who want attach off, an ALLOW_ATTACH that defaults to the value of POST would keep existing setups working. That default would be set in docker-entrypoint.sh, not as ENV ALLOW_ATTACH=0 in the Dockerfile. I haven't tested that variant. I also haven't tested Podman (/libpod/containers/{id}/attach), though its attach is a POST, so with POST=1 the narrow rule doesn't change it.
Reproduce without rebuilding
Save the template variant as narrow.cfg and mount it over the image's template:
docker run -d --name sp-test -p 127.0.0.1:2375:2375 \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
-v "$PWD/narrow.cfg:/templates/haproxy.cfg:ro" \
-e CONTAINERS=1 -e POST=0 \
lscr.io/linuxserver/socket-proxy:latest
docker run -d --name target alpine:3.24 sh -c 'echo "SECRET_IN_LOG=hunter2"; exec sleep infinity'
curl -s --max-time 3 -w '\n%{http_code}\n' \
'ws://127.0.0.1:2375/containers/target/attach/ws?logs=1&stdout=1&stderr=1'main's template prints the log line and 101. narrow.cfg prints HAProxy's 403 page and 403. With -e POST=1 -e IMAGES=1 -e NETWORKS=1 -e VOLUMES=1 -e INFO=1, the following runs normally through main's template or narrow.cfg, and fails with unable to upgrade to tcp, received 403 through this PR's template:
DOCKER_HOST=tcp://127.0.0.1:2375 docker run alpine:3.24 echo run-okWith CONTAINERS=1, GET /containers/{id}/attach/ws passed both POST=0
and the /containers allow rule: it replayed a container's whole log
(what ALLOW_LOGS=0 denies on /logs) and could write to the stdin of a
container started with -i. Attach now needs POST=1, also in its
WebSocket form, so POST=1 setups (docker run, docker start -a,
docker compose up) keep working as before.
Co-authored-by: spannerman79 <[email protected]>
facb894 to
56fa3c5
Compare
|
@spannerman79 You're right, and thanks for testing it so carefully. I switched the PR to your rule and added you as co-author. I built the image from the branch and tested it in a VM: with |
I have read the contributing guideline and understand that I have made the correct modifications
An LLM assisted me in writing the code or text for this PR
An LLM agent wrote the code or text for this PR
Description:
With
CONTAINERS=1,GET /containers/{id}/attach/wspasses bothPOST=0(it's aGET) and the/containersallow rule. Withlogs=1Docker replays the container's whole log, whichALLOW_LOGS=0denies on/logs; withstdin=1a client writes to the stdin of a container started with-i.This adds one deny rule: attach, in its
POSTand its WebSocket form, needsPOST=1. No new env var. The rule is @spannerman79's, from his review below, and he's co-author of the commit. It replaces my first version, anALLOW_ATTACHdefaulting to0, which broke foregrounddocker run,docker start -aanddocker compose upforPOST=1users.With
POST=1,POST /containers/{id}/attach?logs=1still replays a log whileALLOW_LOGS=0. In that mode a client can already create and start containers, soALLOW_LOGSisn't a real boundary there.AI use: Claude Code found the gap, wrote the first patch, ran the tests below and wrote this text; I reviewed all of it.
Benefits of this PR and context:
A usual Traefik setup (
CONTAINERS=1,POST=0) today gives whoever compromises Traefik every container's log, and stdin on containers started with-i. The same kind of gap as #56.How Has This Been Tested?
An Ubuntu 26.04.1 VM with Docker 29.10.1 and Compose 5.8.1. "main" is
linuxserver/socket-proxy:3.4.9-r0-ls108, whose template is identical to main's. "This PR" is this branch built withdocker build --no-cache --pull -f Dockerfile.aarch64 -t socket-proxy:attach-pr .(arm64, HAProxy 3.4.9); it starts with[ls.io-init] done.POST=0,CONTAINERS=1,EVENTS=1,VERSION=1,PING=1:GET /_ping,/version,/containers/json,/containers/{id}/json,/eventsGET /containers/{id}/logs?stdout=1ws://…/containers/{id}/attach/ws?logs=1&stdout=1&stderr=1/v1.54/in frontws://…/containers/{id}/attach/ws?stream=1&stdin=1&stdout=1(shstarted with-i)ws://…/containers/{id}/%61ttach/ws?logs=1&stdout=1POST /containers/{id}/attach?logs=1&stream=0&stdout=1POST=1,CONTAINERS=1,IMAGES=1,NETWORKS=1,VOLUMES=1,INFO=1, the docker CLI through the proxy:docker run --rm alpine:3.24 echo run-okrun-ok, exit 0run-ok, exit 0docker run -d alpine:3.24 truedocker start -a <container>docker compose up(one service,echo compose-ok)compose-ok, exit 0compose-ok, exit 0POST /containers/{id}/attach?logs=1&stream=0&stdout=1After
docker restartof the proxy, the built image still answers/_pingwith 200 andattach/ws?logs=1with 403.Source / References:
execandpause.ContainerAttach(POST /containers/{id}/attach) andContainerAttachWebsocket(GET /containers/{id}/attach/ws).