Skip to content

Commit 2bdc552

Browse files
authored
fix: enforce --readonly in the router for every write (#75) (#77)
* spec: model --readonly as a router rule (#75) * test: pin --readonly as a router rule in all three (#75) * fix: enforce --readonly in the router for every write (#75) * docs: note the attach gap and tidy the read-only docs (#75)
1 parent b6a845b commit 2bdc552

25 files changed

Lines changed: 518 additions & 254 deletions

‎AGENTS.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ deploy/ — Docker Compose + integration tests
2727

2828
## Architecture (same across all 3 languages)
2929
- `policy/` — Policy types + Manager (loads YAML from config dir)
30-
- `middleware/` — Gate trait + Chain: ReadonlyGate, RegistryGate, MountSourceGate, EnvFileGate, CmdGate, ContainerConfigMutator
30+
- `middleware/` — Gate trait + Chain: RegistryGate, MountSourceGate, EnvFileGate, CmdGate, ContainerConfigMutator (`--readonly` and exec are denied by the router)
3131
- `proxy/` — Router (default-deny), Handler (orchestrates chain), Transport (forwards to Docker socket)
3232
- `audit/` — JSON-structured logging
3333

@@ -39,17 +39,17 @@ deploy/ — Docker Compose + integration tests
3939
- Zero external deps where possible (Go: yaml.v3, Rust: tokio/hyper/serde/clap, TS: yaml)
4040

4141
## Test Coverage
42-
- Go: 108 unit tests (main/listener: 24, policy: 10, middleware: 27, proxy: 43, audit: 4)
43-
- Rust: 143 unit tests (main/listener: 23, policy: 15, middleware: 46, proxy: 45, handler: 8, audit: 4, transport: 2)
44-
- TypeScript: 161 unit tests, 1 skipped (flags: 44, listen: 13 incl. 1 skipped concurrency test (#46), middleware: 38, proxy: 32, policy: 10, handler: 10, shutdown: 5, transport: 5, audit: 4)
45-
- Integration, per implementation: 45 tests via deploy/test.sh and 15 socket tests via deploy/test-sock.sh (docker-compose)
46-
- Quint: `make test-spec` runs the `spec/listener.qnt` `run` tests (instances `listener_locked`, `listener_unlocked`) and the `spec/router.qnt` `run` tests (instances `router`, `router_pre48`, `router_pre53`, `router_pre55`)
42+
- Go: 108 unit tests (main/listener: 24, policy: 10, middleware: 25, proxy: 45, audit: 4)
43+
- Rust: 140 unit tests (main/listener: 23, policy: 15, middleware: 41, proxy: 46, handler: 9, audit: 4, transport: 2)
44+
- TypeScript: 157 unit tests, 1 skipped (flags: 44, listen: 13 incl. 1 skipped concurrency test (#46), middleware: 32, proxy: 33, policy: 10, handler: 11, shutdown: 5, transport: 5, audit: 4)
45+
- Integration, per implementation: 50 tests via deploy/test.sh and 15 socket tests via deploy/test-sock.sh (docker-compose)
46+
- Quint: `make test-spec` runs the `spec/listener.qnt` `run` tests (instances `listener_locked`, `listener_unlocked`) and the `spec/router.qnt` `run` tests (instances `router`, `router_pre48`, `router_pre53`, `router_pre55`, and `router_readonly`, whose `readonly*` tests run with `--match`)
4747

4848
## Test Conventions
4949
- Go: stdlib `testing` package, `go test ./...`
5050
- Rust: `#[cfg(test)]` inline modules, `cargo test`
5151
- TypeScript: `node:test` framework, `npm run build && node --test dist/*.test.js`
52-
- Integration: `make test-integration` (45 test cases) and `make test-integration-sock` (15 socket cases) via Docker Compose
52+
- Integration: `make test-integration` (50 test cases) and `make test-integration-sock` (15 socket cases) via Docker Compose
5353

5454
## Contribution Workflow
5555

‎CONTRIBUTING.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ To bypass a hook once, pass `--no-verify` to `git commit` or `git push`. CI runs
3434

3535
All three languages implement the same architecture:
3636
- `policy/` — Policy types + Manager (loads YAML)
37-
- `middleware/` — Gate trait + Chain (5 gates + 1 mutator: ReadonlyGate, RegistryGate, MountSourceGate, EnvFileGate, CmdGate, ContainerConfigMutator; exec is denied by the router)
37+
- `middleware/` — Gate trait + Chain (4 gates + 1 mutator: RegistryGate, MountSourceGate, EnvFileGate, CmdGate, ContainerConfigMutator; `--readonly` and exec are denied by the router)
3838
- `proxy/` — Router (default-deny) + Handler + Transport
3939

4040
Changes should be applied to **all three implementations** unless they are language-specific (e.g., CLI flag parsing).

‎Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ test-spec:
102102
$(QUINT) test $(ROUTER_SPEC) --main=router_pre48
103103
$(QUINT) test $(ROUTER_SPEC) --main=router_pre53
104104
$(QUINT) test $(ROUTER_SPEC) --main=router_pre55
105+
$(QUINT) test $(ROUTER_SPEC) --main=router_readonly --match='^readonly.*Test$$'
105106

106107
verify-ts:
107108
$(QUINT) run $(SPEC) --max-steps=50 --invariants allInvariants --backend typescript

‎README.md‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ A validating Docker API proxy that enforces **per-service container policies** t
1010
Key features:
1111

1212
- **Policy-driven**: Per-service YAML policies, selected by image name, control images, volumes, flags, and env vars. The listening socket is the trust boundary: every caller of a socket can use every policy behind it (see [Trust model](#trust-model))
13-
- **Middleware pipeline**: 5 validation gates + 1 config mutator chain
13+
- **Middleware pipeline**: 4 validation gates + 1 config mutator chain
1414
- **Default-deny router**: Only explicitly allowed endpoints pass through
15-
- **Formally verified**: [Quint](https://quint-lang.org/) specification: 9 request-handling invariants, 6 listener invariants, and router properties for percent-encoded paths, empty segments, exec and the container lifecycle
15+
- **Formally verified**: [Quint](https://quint-lang.org/) specification: 9 request-handling invariants, 6 listener invariants, and router properties for `--readonly`, percent-encoded paths, empty segments, exec and the container lifecycle
1616
- **Audit logging**: JSON-structured logs for all requests and decisions
1717
- **Three implementations**: [Go](go/), [Rust](rs/), [TypeScript](ts/) — equal peer languages
1818
- **Minimal dependencies**: Zero external deps for Go, crate-based for Rust, npm for TypeScript
@@ -100,9 +100,9 @@ All three implementations expose the same API surface, share the same [Quint spe
100100

101101
| Language | Directory | Tests | Stack |
102102
|----------|-----------|-------|-------|
103-
| Go | [go/](go/) | 108 unit + 45 integration + 15 socket | stdlib net/http + yaml.v3 |
104-
| Rust | [rs/](rs/) | 143 unit + 45 integration + 15 socket | tokio, hyper, serde, clap |
105-
| TypeScript | [ts/](ts/) | 161 unit (1 skipped) + 45 integration + 15 socket | Node 22 ESM, built-in http |
103+
| Go | [go/](go/) | 108 unit + 50 integration + 15 socket | stdlib net/http + yaml.v3 |
104+
| Rust | [rs/](rs/) | 140 unit + 50 integration + 15 socket | tokio, hyper, serde, clap |
105+
| TypeScript | [ts/](ts/) | 157 unit (1 skipped) + 50 integration + 15 socket | Node 22 ESM, built-in http |
106106

107107
### Build All
108108

@@ -193,16 +193,18 @@ docker pull attacker/malware:latest # denied: image not in allowlist
193193
| Middleware | Type | What it checks |
194194
|------------|------|----------------|
195195
| ContainerConfigMutator | Mutator | Forces `network_mode`, `user`, `security_options`, `restart_policy` from policy |
196-
| ReadonlyGate | Gate | Denies `POST`, `PUT`, `DELETE`, `PATCH` (optional `--readonly` flag). Like every gate it runs only on container create, so lifecycle actions and `DELETE` are not blocked yet ([#75](https://github.com/ChainSafe/docker-socket-policy/issues/75)) |
197196
| RegistryGate | Gate | Validates image ref against `allowed_image_prefixes` |
198197
| MountSourceGate | Gate | Validates volume binds against whitelist |
199198
| EnvFileGate | Gate | Strips `Env` field from create body; env must come from locked `env_file` |
200199
| CmdGate | Gate | Validates each CLI flag in `Cmd` array against allowlist + denylist |
201200

201+
The chain runs only on container create. `--readonly` is not a gate: the router denies every request that is not `GET` or `HEAD`, on every path, before any other check ([#75](https://github.com/ChainSafe/docker-socket-policy/issues/75)). A websocket attach (`GET /containers/{name}/attach/ws` with `Upgrade`) is a `GET` and is not denied yet; the Go proxy lets it write to a container's stdin ([#76](https://github.com/ChainSafe/docker-socket-policy/issues/76)).
202+
202203
## Endpoint Access
203204

204205
| HTTP Method | Path | Action |
205206
|-------------|------|--------|
207+
| Not GET/HEAD | Any (with `--readonly`) | **DENIED** (checked first) |
206208
| Any | Path containing `%` | **DENIED** (see below) |
207209
| Any | Path containing `//` | **DENIED** |
208210
| POST | `/containers/create` | Validated by middleware chain |
@@ -232,7 +234,7 @@ Exec is matched on whole path segments, so containers named like `exec-runner` w
232234
| `--docker-host` | `/var/run/docker.sock` | Docker daemon socket path (Unix socket only) |
233235
| `--config-dir` | `/etc/docker-socket-policy/services` | Policy config directory |
234236
| `--log-file` | `/var/log/docker-socket-policy.log` | Audit log path |
235-
| `--readonly` | `false` | Enable read-only mode |
237+
| `--readonly` | `false` | Enable read-only mode: the router denies every request that is not `GET` or `HEAD` |
236238
| `--listen-socket-group` | `docker-socket-policy` | Group owning the socket (default `docker-socket-policy`; `""` = the proxy's own group) |
237239

238240
> **Unix socket security boundary**: the proxy listens on a Unix socket only,
@@ -439,7 +441,7 @@ not at the socket.
439441

440442
## Formal Verification
441443

442-
This project includes a [Quint](https://quint-lang.org/) formal specification that models the security invariants as a state machine. Random-simulation verification runs 10,000 sampled traces of up to 100 steps each, checking all 9 invariants on every state transition. A second module, `spec/listener.qnt`, models listening-socket startup (group selection, existing-path checks, the single-instance lock) with 6 more invariants. A third module, `spec/router.qnt`, models the router's denies for percent-encoded paths ([#53](https://github.com/ChainSafe/docker-socket-policy/issues/53)), empty interior segments ([#55](https://github.com/ChainSafe/docker-socket-policy/issues/55)) and exec ([#49](https://github.com/ChainSafe/docker-socket-policy/issues/49)), the `POST /containers/create` route, container-name extraction and the container-lifecycle branch, not the full routing table ([#24](https://github.com/ChainSafe/docker-socket-policy/issues/24), [#48](https://github.com/ChainSafe/docker-socket-policy/issues/48)). Its 7 properties are checked exhaustively over the methods `GET`, `HEAD`, `POST`, `DELETE` and every path of 1 to 3 segments over the model's word set; exec is denied by the router, not by a middleware gate.
444+
This project includes a [Quint](https://quint-lang.org/) formal specification that models the security invariants as a state machine. Random-simulation verification runs 10,000 sampled traces of up to 100 steps each, checking all 9 invariants on every state transition. A second module, `spec/listener.qnt`, models listening-socket startup (group selection, existing-path checks, the single-instance lock) with 6 more invariants. A third module, `spec/router.qnt`, models the router's denies for writes under `--readonly` ([#75](https://github.com/ChainSafe/docker-socket-policy/issues/75)), percent-encoded paths ([#53](https://github.com/ChainSafe/docker-socket-policy/issues/53)), empty interior segments ([#55](https://github.com/ChainSafe/docker-socket-policy/issues/55)) and exec ([#49](https://github.com/ChainSafe/docker-socket-policy/issues/49)), the `POST /containers/create` route, container-name extraction and the container-lifecycle branch, not the full routing table ([#24](https://github.com/ChainSafe/docker-socket-policy/issues/24), [#48](https://github.com/ChainSafe/docker-socket-policy/issues/48)). Its 8 properties are checked exhaustively over the methods `GET`, `HEAD`, `POST`, `DELETE` and every path of 1 to 3 segments over the model's word set; exec is denied by the router, not by a middleware gate.
443445

444446
The CI pipeline runs verification on every push and PR. A violation blocks the build.
445447

‎deploy/docker-compose.yml‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,33 @@ services:
1616
- --config-dir=/etc/docker-socket-policy/services
1717
- --log-file=/tmp/docker-socket-policy.log
1818

19+
# The same proxy with --readonly (#75), on its own socket in the shared volume.
20+
proxy-readonly:
21+
build:
22+
context: ../${IMPL:-go}
23+
dockerfile: Dockerfile
24+
user: "0:0"
25+
volumes:
26+
- /var/run/docker.sock:/var/run/docker.sock
27+
- ./config:/etc/docker-socket-policy/services:ro
28+
- proxy-sock:/sock
29+
command:
30+
- --docker-host=/var/run/docker.sock
31+
- --listen-socket=/sock/readonly.sock
32+
- --config-dir=/etc/docker-socket-policy/services
33+
- --log-file=/tmp/docker-socket-policy-readonly.log
34+
- --readonly
35+
1936
test:
2037
build:
2138
context: .
2239
dockerfile: Dockerfile.test
2340
depends_on:
2441
- proxy
42+
- proxy-readonly
2543
environment:
2644
PROXY_SOCK: /sock/proxy.sock
45+
PROXY_RO_SOCK: /sock/readonly.sock
2746
# Hostname the proxy would have been reachable on when it still had a
2847
# TCP listener, so the suite can assert that it no longer is.
2948
PROXY_HOST: proxy

‎deploy/test.sh‎

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
# Integration tests for docker-socket-policy.
33
# Sends HTTP requests directly to the proxy's Docker API and checks status codes,
44
# and the deny message on every expected proxy 403.
5-
# The proxy's ReadonlyGate, RegistryGate, MountSourceGate, EnvFileGate,
6-
# CmdGate, and ContainerConfigMutator are all exercised; exec is denied by the router.
5+
# The proxy's RegistryGate, MountSourceGate, EnvFileGate, CmdGate, and
6+
# ContainerConfigMutator are all exercised; exec is denied by the router, and so
7+
# is every write on a second proxy run with --readonly (#75).
78

89
set -e
910

@@ -29,24 +30,31 @@ fi
2930
# instead of failing it — and the readiness counter below would never advance.
3031
TIMEOUT="--max-time 10 --connect-timeout 2"
3132

32-
# Wait for the proxy to create and serve its socket.
33-
echo "Waiting for proxy at ${PROXY_SOCK}..."
34-
i=0
35-
while [ $i -lt 30 ]; do
36-
if curl -sf $TIMEOUT --unix-socket "$PROXY_SOCK" "$PROXY/_ping" >/dev/null 2>&1; then
37-
echo "Proxy ready."
38-
break
33+
# The --readonly proxy (#75) serves the same config on its own socket.
34+
PROXY_RO_SOCK="${PROXY_RO_SOCK:-/sock/readonly.sock}"
35+
36+
# Wait for a proxy to create and serve its socket.
37+
wait_for_sock() {
38+
echo "Waiting for proxy at $1..."
39+
i=0
40+
while [ $i -lt 30 ]; do
41+
if curl -sf $TIMEOUT --unix-socket "$1" "$PROXY/_ping" >/dev/null 2>&1; then
42+
echo "Proxy ready."
43+
break
44+
fi
45+
printf "."
46+
sleep 1
47+
i=$((i + 1))
48+
done
49+
if [ $i -eq 30 ]; then
50+
echo ""
51+
echo "ERROR: Proxy at $1 failed to respond within 30s"
52+
exit 1
3953
fi
40-
printf "."
41-
sleep 1
42-
i=$((i + 1))
43-
done
44-
if [ $i -eq 30 ]; then
4554
echo ""
46-
echo "ERROR: Proxy failed to respond within 30s"
47-
exit 1
48-
fi
49-
echo ""
55+
}
56+
wait_for_sock "$PROXY_SOCK"
57+
wait_for_sock "$PROXY_RO_SOCK"
5058

5159
# Helpers: report the HTTP status code of a request sent over the Unix socket.
5260
# curl exits non-zero when it cannot connect at all, which under `set -e` would
@@ -440,6 +448,34 @@ S="${S:-000}"
440448
check_denied "GET http://docker/exec/*/json -> 403 (absolute-form target)" "$S" "$BODY" \
441449
'exec is not allowed' 'empty path segment not allowed'
442450

451+
# ─── Read-only mode (#75) ────────────────────────────────
452+
453+
# The same config behind --readonly. Every request whose method is not GET or
454+
# HEAD is denied by the router. Before #75 the flag only reached the gate
455+
# chain, which runs on create alone, so these reached the daemon (a 404 for
456+
# the unknown container below, i.e. a leak, not a proxy 403).
457+
echo ""
458+
echo "--- Read-only mode ---"
459+
460+
# The request helpers read PROXY_SOCK, so this switches every check below to
461+
# the read-only proxy. Keep this section last.
462+
PROXY_SOCK="$PROXY_RO_SOCK"
463+
464+
S=$(get_status "$PROXY/_ping")
465+
check "GET /_ping -> 200 (read-only)" "200" "$S"
466+
467+
S=$(post_empty "$PROXY/containers/no-such-container/stop")
468+
check_denied "POST /containers/*/stop -> 403 (read-only)" "$S" "$BODY" 'read-only mode: POST requests are not allowed'
469+
470+
S=$(delete_status "$PROXY/containers/no-such-container")
471+
check_denied "DELETE /containers/* -> 403 (read-only)" "$S" "$BODY" 'read-only mode: DELETE requests are not allowed'
472+
473+
S=$(post_json '{"Image":"chainsafe/lodestar:beacon","Cmd":["--rcConfig","/data/config.yml"]}' "$PROXY/containers/create")
474+
check_denied "POST /containers/create with allowed image -> 403 (read-only)" "$S" "$BODY" 'read-only mode: POST requests are not allowed'
475+
476+
S=$(post_empty "$PROXY/images/create?fromImage=chainsafe/lodestar&tag=beacon")
477+
check_denied "POST /images/create?fromImage=chainsafe/lodestar -> 403 (read-only)" "$S" "$BODY" 'read-only mode: POST requests are not allowed'
478+
443479
# ─── Summary ──────────────────────────────────────────
444480

445481
echo ""

‎go/internal/middleware/chain.go‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@ type Chain struct {
2626
mutators []Mutator
2727
}
2828

29-
func NewChain(readonly bool) *Chain {
29+
func NewChain() *Chain {
3030
c := &Chain{}
3131

32-
if readonly {
33-
c.gates = append(c.gates, &ReadonlyGate{})
34-
}
3532
c.gates = append(c.gates, &RegistryGate{})
3633
c.gates = append(c.gates, &MountSourceGate{})
3734
c.gates = append(c.gates, &EnvFileGate{})

0 commit comments

Comments
 (0)