Skip to content

Add ALLOW_ATTACH, deny container attach by default - #58

Open
antonisgr wants to merge 1 commit into
linuxserver:mainfrom
antonisgr:deny-attach
Open

antonisgr wants to merge 1 commit into
linuxserver:mainfrom
antonisgr:deny-attach

Conversation

@antonisgr

Copy link
Copy Markdown

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, the template lets /containers/{id}/attach through. Its WebSocket form, GET /containers/{id}/attach/ws, is a GET, so it passes even with POST=0: with logs=1 Docker replays the container's whole log (what ALLOW_LOGS=0 denies on /logs), and with stdin=1 a client writes to the stdin of a container started with -i. With POST=1, POST /containers/{id}/attach?logs=1 replays the log too.

This adds ALLOW_ATTACH, default 0: a deny rule next to the other subpath denies, so it applies whatever POST is set to. With POST=1, docker compose up, docker run and docker start -a without -d attach, so they now need ALLOW_ATTACH=1; so do Podman's, through /libpod/containers/{id}/attach (not tested with Podman).

AI use: Claude Code found the gap, wrote the patch and this description, and ran the tests below; I reviewed all of it and re-ran the curl reproduction below myself.

Benefits of this PR and context:

The same kind of gap as #56: a control (ALLOW_LOGS, POST=0) the template doesn't enforce on a nested container path. A usual Traefik setup (CONTAINERS=1, POST=0) gives whoever compromises Traefik the log of every container on the host.

How Has This Been Tested?

An Ubuntu 26.04.1 LTS VM with Docker 29.10.0. This branch built with docker build --no-cache --pull -f Dockerfile.aarch64 -t socket-proxy:attach-test . (arm64) and loaded into the VM, against 3.4.8-r0-ls107:

$ docker network create sp >/dev/null
$ docker run -d --name target --network sp alpine:3.24 sh -c 'echo "203.0.113.7 GET /login"; exec sleep infinity' >/dev/null

$ docker run -d --name proxy --network sp -v /var/run/docker.sock:/var/run/docker.sock:ro -e CONTAINERS=1 -e POST=0  linuxserver/socket-proxy:3.4.8-r0-ls107 >/dev/null; sleep 2
$ docker run --rm --network sp curlimages/curl:8.11.1 -s -o /dev/null -w '%{http_code}\n' 'http://proxy:2375/containers/target/logs?stdout=1'
403
$ docker run --rm --network sp curlimages/curl:8.11.1 -s --max-time 3 -w '%{http_code}\n' 'ws://proxy:2375/containers/target/attach/ws?logs=1&stdout=1&stderr=1' | tr -cd '[:print:]\n' | sed 's/<html>.*//'
203.0.113.7 GET /login
101

$ docker run -d --name proxy --network sp -v /var/run/docker.sock:/var/run/docker.sock:ro -e CONTAINERS=1 -e POST=0  socket-proxy:attach-test >/dev/null; sleep 2
$ docker run --rm --network sp curlimages/curl:8.11.1 -s -o /dev/null -w '%{http_code}\n' 'http://proxy:2375/containers/target/logs?stdout=1'
403
$ docker run --rm --network sp curlimages/curl:8.11.1 -s --max-time 3 -w '%{http_code}\n' 'ws://proxy:2375/containers/target/attach/ws?logs=1&stdout=1&stderr=1' | tr -cd '[:print:]\n' | sed 's/<html>.*//'
403

$ docker run -d --name proxy --network sp -v /var/run/docker.sock:/var/run/docker.sock:ro -e CONTAINERS=1 -e POST=0 -e ALLOW_ATTACH=1 socket-proxy:attach-test >/dev/null; sleep 2
$ docker run --rm --network sp curlimages/curl:8.11.1 -s -o /dev/null -w '%{http_code}\n' 'http://proxy:2375/containers/target/logs?stdout=1'
403
$ docker run --rm --network sp curlimages/curl:8.11.1 -s --max-time 3 -w '%{http_code}\n' 'ws://proxy:2375/containers/target/attach/ws?logs=1&stdout=1&stderr=1' | tr -cd '[:print:]\n' | sed 's/<html>.*//'
203.0.113.7 GET /login
101

(tr drops the WebSocket close frame's bytes, sed HAProxy's 403 page, so only the status code shows; the proxy was removed between runs.)

Also checked, unset and ALLOW_ATTACH=0 alike, POST=0 and POST=1: POST /containers/{id}/attach?logs=1&stream=0 (3.4.8: 403 with POST=0, the log with POST=1; this branch: 403), and a small WebSocket client sending id -un to alpine sh started with -i, through attach/ws?stream=1&stdin=1&stdout=1 (3.4.8: root; this branch: 403). ALLOW_ATTACH=1 brings both back. Traefik's calls (/_ping, /version, /containers/json, /containers/{id}/json, /events) answer 200 in every case.

The docker CLI through the proxy with POST=1 (and IMAGES, NETWORKS, INFO, VOLUMES), each command stopped after 20 s:

Command 3.4.8 This branch This branch, ALLOW_ATTACH=1
docker run --rm alpine:3.24 echo run-ok prints run-ok, then doesn't return unable to upgrade to tcp, received 403 as 3.4.8
docker run -d alpine:3.24 true works works works
docker start -a <container> prints its output, then doesn't return unable to upgrade to tcp, received 403 as 3.4.8
docker compose up works fails (403) works

Source / References:

GET /containers/{id}/attach/ws passed with CONTAINERS=1, even with
POST=0: it replayed a container's whole log (logs=1, which ALLOW_LOGS=0
is meant to deny) and wrote to the stdin of containers started with -i.
With POST=1, POST /containers/{id}/attach replayed the log too.

With POST=1, docker compose up, docker run and docker start -a without
-d attach, so they now need ALLOW_ATTACH=1; so do their Podman
equivalents, through /libpod/containers/{id}/attach.
@github-actions

Copy link
Copy Markdown

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

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.

1 participant