Skip to content

Deny container attach unless POST=1 - #58

Merged
thespad merged 1 commit into
linuxserver:mainfrom
antonisgr:deny-attach
Nov 29, 2026
Merged

thespad merged 1 commit into
linuxserver:mainfrom
antonisgr:deny-attach

Conversation

@antonisgr

@antonisgr antonisgr commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

linuxserver.io


  • 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/ws passes both POST=0 (it's a GET) and the /containers allow rule. With logs=1 Docker replays the container's whole log, which ALLOW_LOGS=0 denies on /logs; with stdin=1 a client writes to the stdin of a container started with -i.

This adds one deny rule: attach, in its POST and its WebSocket form, needs POST=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, an ALLOW_ATTACH defaulting to 0, which broke foreground docker run, docker start -a and docker compose up for POST=1 users.

With POST=1, POST /containers/{id}/attach?logs=1 still replays a log while ALLOW_LOGS=0. In that mode a client can already create and start containers, so ALLOW_LOGS isn'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 with docker 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:

Request main this PR
GET /_ping, /version, /containers/json, /containers/{id}/json, /events 200 200
GET /containers/{id}/logs?stdout=1 403 403
ws://…/containers/{id}/attach/ws?logs=1&stdout=1&stderr=1 101, the log replayed 403
the same with /v1.54/ in front 101, the log replayed 403
ws://…/containers/{id}/attach/ws?stream=1&stdin=1&stdout=1 (sh started with -i) 101 403
ws://…/containers/{id}/%61ttach/ws?logs=1&stdout=1 101, the log replayed 403
POST /containers/{id}/attach?logs=1&stream=0&stdout=1 403 403

POST=1, CONTAINERS=1, IMAGES=1, NETWORKS=1, VOLUMES=1, INFO=1, the docker CLI through the proxy:

Command main this PR
docker run --rm alpine:3.24 echo run-ok run-ok, exit 0 run-ok, exit 0
docker run -d alpine:3.24 true exit 0 exit 0
docker start -a <container> its output, exit 0 its output, exit 0
docker compose up (one service, echo compose-ok) compose-ok, exit 0 compose-ok, exit 0
POST /containers/{id}/attach?logs=1&stream=0&stdout=1 200, the log replayed 200, the log replayed

After docker restart of the proxy, the built image still answers /_ping with 200 and attach/ws?logs=1 with 403.

Source / References:

@github-actions

Copy link
Copy Markdown

Thanks for opening this pull request! Be sure to follow the pull request template!

@spannerman79

Copy link
Copy Markdown
Contributor

@antonisgr

Copy link
Copy Markdown
Contributor Author

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/linuxserver/docker-socket-proxy/blob/main/package_versions.txt#L10

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 root/templates/haproxy.cfg, and that file has no deny rule for attach, so with CONTAINERS=1 it goes through. This PR adds that rule. The tests above show it: same image, ALLOW_ATTACH unset gives 403, ALLOW_ATTACH=1 gives the logs again. #56 fixed the same kind of thing for exec and pause in the same file.

@spannerman79

Copy link
Copy Markdown
Contributor

Correction to my earlier comment: this isn't an upstream issue. HAProxy only enforces what root/templates/haproxy.cfg says, and the template has no rule for attach. GET /containers/{id}/attach/ws is a GET, so it passes deny unless METH_GET || { env(POST) -m bool }, and the ^(/v[\d\.]+)?/containers allow rule then lets it through. Sorry for the noise.

I reproduced the gap. I also tested what this PR does to POST=1 setups. The gap is real, but as written the PR breaks foreground docker run, docker start -a and docker compose up for every POST=1 user. A narrower rule closes the same hole without breaking any of them.

Setup: lscr.io/linuxserver/socket-proxy:3.4.9-r0-ls108 (HAProxy 3.4.9), Docker 29.10.1, Compose 5.8.1. I ran three templates, each mounted over /templates/haproxy.cfg so nothing had to be rebuilt: main unchanged, main + this PR, and main + the narrow rule below.

POST=0, CONTAINERS=1 (the usual Traefik 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:2375

Today, 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-ok

With 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]>
@antonisgr antonisgr changed the title Add ALLOW_ATTACH, deny container attach by default Deny container attach unless POST=1 Nov 29, 2026
@antonisgr

Copy link
Copy Markdown
Contributor Author

@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 POST=0 attach is closed (log replay and stdin), and with POST=1 docker run, docker start -a and docker compose up work as before. Details in the PR description.

@github-project-automation github-project-automation Bot moved this from PRs to PRs Approved in Issue & PR Tracker Nov 29, 2026
@thespad
thespad merged commit 5e2f32b into linuxserver:main Nov 29, 2026
2 checks passed
@LinuxServer-CI LinuxServer-CI moved this from PRs Approved to Done in Issue & PR Tracker Nov 29, 2026
@antonisgr
antonisgr deleted the deny-attach branch November 29, 2026 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

3 participants