Skip to content

fix(security): add non-root USER directive to 4 production Dockerfiles (CSO hardening, persistent since 2026-04-05) #874

Closed

Description

@AndriiPasternak31

Summary

Four production Dockerfiles run their CMD as root. Only docker/base-image/Dockerfile (agent base) drops to a non-root developer user. This is a defense-in-depth gap — not a standalone exploit, but it amplifies the blast radius of any future RCE.

Flagged by /cso at MEDIUM, 10/10 confidence, persistent since the 2026-04-05 audit (~6 weeks).

Affected Files

  • docker/backend/Dockerfile — FastAPI backend, python:3.11-slim base, no USER directive (ends at line 70). Highest risk — backend has /var/run/docker.sock:ro mounted, so root inside this container can enumerate every Trinity agent container by label.
  • docker/scheduler/Dockerfile — APScheduler service, python:3.11-slim base, no USER directive.
  • docker/frontend/Dockerfile.prod — FROM nginx:alpine, no USER directive. nginx master defaults to root; only worker processes drop to nginx.
  • src/mcp-server/Dockerfile — TypeScript MCP server, node:22-alpine base, no USER directive.

No user: override in docker-compose.yml or docker-compose.prod.yml, confirming containers actually run as root in production.

Exploit Scenario

Any RCE in the FastAPI backend (e.g., a future variant of #590 file-write RCE, a deserialization bug in a router, a vulnerable dep introduced via supply chain) lands as root inside the backend container. Because /var/run/docker.sock is bind-mounted read-only:

  1. Attacker reads Docker socket → enumerates every agent container, owner labels, network bindings.
  2. Reads in-container env vars (ANTHROPIC_API_KEY, REDIS_URL with backend creds, SECRET_KEY).
  3. Reaches Redis on trinity-platform-network with the credential pair already in env.
  4. If combined with any write primitive (volume bind to host paths, Redis ops on platform queue), pivots further.

Read-only socket blocks docker exec / docker run, so this is not a direct container escape. But "no USER" turns a single-point RCE into a fleet-wide reconnaissance primitive.

Acceptance Criteria

  • docker/backend/Dockerfile ends on USER trinity (or equivalent non-root UID 1000).
  • docker/scheduler/Dockerfile ends on a non-root USER.
  • docker/frontend/Dockerfile.prod runs nginx unprivileged. Either USER nginx with listen 8080; adjustment + compose port remap, or the nginxinc/nginx-unprivileged base image.
  • src/mcp-server/Dockerfile ends on a non-root USER.
  • ./scripts/deploy/start.sh brings up the full stack with no permission-denied errors.
  • Backend can still read /data/trinity.db (bind mount), /data/agent-files/, write logs.
  • Vector still scrapes container logs (Docker socket access unaffected — Vector has its own :ro mount, no change needed).
  • MCP server binds port 8080 and authenticates Bearer tokens normally.
  • nginx (frontend.prod) serves /, reverse-proxies /api/* to backend, serves WebSocket upgrades.
  • No CI workflow breakage (frontend-e2e.yml uses these images).

Reference Pattern

docker/base-image/Dockerfile:70-132 already demonstrates the working pattern — groupadd / useradd near the top, USER root only around steps that need it (e.g., apt-get), USER developer for the final state. Use this as a template.

For the backend specifically:

# docker/backend/Dockerfile (end of file)
RUN groupadd --gid 1000 trinity && \
    useradd --uid 1000 --gid trinity --home /app --shell /bin/bash trinity && \
    chown -R trinity:trinity /app
USER trinity

Technical Notes

  • nginx:alpine requires either (a) switching to nginxinc/nginx-unprivileged which listens on 8080 by default and runs as nginx user, then remapping 80 → 8080 in compose, or (b) writing a custom nginx.conf with listen 8080; + creating writable temp dirs for the nginx user. The unprivileged image is simpler.
  • MCP server (Node) needs the workspace ownership flipped: RUN chown -R node:node /app && USER node after the npm ci step.
  • Scheduler reads/writes the same SQLite DB as backend via /data bind mount. If backend uses UID 1000, scheduler must use the same UID to share file ownership.
  • Confirm Docker socket mount stays :ro (it should — Vector also mounts :ro). No change to compose volumes needed.

Prior Reports

  • docs/security-reports/cso-2026-04-05.md:33 — original finding (MEDIUM, 10/10, VERIFIED)
  • docs/security-reports/cso-2026-04-05.md:151 — trend table marks this PERSISTENT
  • docs/security-reports/cso-2026-05-17.md — re-verified today, still all 4 files, no user: override in compose

Why Bundle as One PR

All four Dockerfiles share the same UID-1000 strategy (consistent with docker/base-image/Dockerfile). Splitting per-file creates 4× the review overhead with no isolation benefit — these images are independent at runtime, so the only shared risk is "did the dev test the stack came up cleanly". One PR with one ./scripts/deploy/start.sh smoke test covers all of them.

Activity

  1. added
    priority-p3
    type-refactor
    security
    severity-low
    on May 17, 2026
  2. AndriiPasternak31 commented on May 17, 2026

    @AndriiPasternak31
    ContributorAuthor

    /claim

  3. added
    status-in-progress
    on May 17, 2026
  4. github-actions commented on May 17, 2026

    @github-actions

    Claimed by @AndriiPasternak31. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/874-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #874
  5. added
    status-in-dev
    and removed
    status-in-progress
    on May 23, 2026
  6. added a commit that references this issue on May 28, 2026
  7. vybe commented on Jun 1, 2026

    @vybe
    Contributor

    Shipped in v0.6.0 (released via #998).

  8. added
    theme-security
    and removed
    status-in-dev
    on Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

priority-p3securityseverity-lowtheme-securitytype-refactor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions