From a706a7d811612bf1dd67b029a70f9b2e6755482e Mon Sep 17 00:00:00 2001 From: Marius Ciepluch <11855163+norandom@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:36:59 +0100 Subject: [PATCH] build(image): the app layers name the runtime user, so the chown layer is gone By the maintainer's decision of 2026-11-25, recorded in the handover. The option as offered: "Drop the duplicate chown layer", as offered: "`COPY --chown` on the app file layers instead of a final `chown -R`. Every file under `/app` stays owned by uid 1000, compared file by file in the rebuilt image." `RUN chown -R ousast /app` ran after the last COPY, so it copied every file of `/app` into a second layer: 194 MB beside the 189 MB `benchmarks` layer in the local image, and the build of 2026-11-25 spent 311 of its 470 seconds in it. The line is removed, and the copies it covered take `--chown=1000` as `benchmarks` already did: `pyproject.toml`, `README.md` and `LICENSE` in one, `src` in the other. Nothing after the last COPY writes under `/app`: the pip install runs with the uv_build backend and left nothing there in the image built on 2026-11-25. Two things are expected to change besides the layer. `/app` itself, which `WORKDIR` creates, is owned by root, where `chown -R` made it uid 1000. And the files that were gid 0 take gid 1000. No tracked use writes into `/app` as the runtime user: compose runs in `/work` and writes under `/reports`, the wrapper `ops/ousast-docker` works in the mounted repository, and the workflows build and push. Tests: the new `tests/test_task_images.py::test_app_layers_carry_their_owner` fails on a `RUN chown` in the final stage and on a `COPY` into it that does not start with `COPY --chown=1000 `. It failed before the change (1 failed, 15 passed) and passes after; with `tests/test_search_image.py`, 17 passed. On scratch copies it fails when the chown line is put back and when the flag is dropped from `COPY src ./src`. `test_digest_required_and_code_last` found its code line by `COPY src `; it accepts the new prefix, and its ordering check is unchanged. ruff, mypy, the three gates and compileall pass, and the gates print what they printed before the change. An independent reviewer approved the change in one round. Not claimed here: the rebuilt image. It follows in this step's next part, with the proof that every file under `/app` is owned by uid 1000 and that `/app` holds the tracked files only. --- Dockerfile | 6 +++--- tests/test_task_images.py | 13 ++++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 59deac80..6c5f4448 100644 --- a/Dockerfile +++ b/Dockerfile @@ -36,12 +36,12 @@ RUN if getent passwd 1000 > /dev/null; then \ usermod --login ousast --home /home/ousast --move-home "$(getent passwd 1000 | cut -d: -f1)"; \ else useradd --create-home --uid 1000 ousast; fi WORKDIR /app -COPY pyproject.toml README.md LICENSE ./ -COPY src ./src +# Each layer under /app names the runtime user itself; a later `chown -R /app` would store every file twice. +COPY --chown=1000 pyproject.toml README.md LICENSE ./ +COPY --chown=1000 src ./src RUN --mount=type=cache,target=/root/.cache/pip \ /venv/bin/pip install --no-deps --no-cache-dir . COPY --chown=1000 benchmarks ./benchmarks -RUN chown -R ousast /app USER ousast ENTRYPOINT ["python", "-m", "openultrasast.cli"] CMD ["--help"] diff --git a/tests/test_task_images.py b/tests/test_task_images.py index 4687aaee..eb7191a2 100644 --- a/tests/test_task_images.py +++ b/tests/test_task_images.py @@ -37,7 +37,7 @@ def test_digest_required_and_code_last(name, variant): assert f"ARG TASK_{variant}_DIGEST" in lines ref = f"ghcr.io/norandom/ousast-task-base:${{TASK_{variant}_TAG}}@${{TASK_{variant}_DIGEST}}" assert any(line.startswith(f"FROM {ref}") for line in lines) - code = next(i for i, line in enumerate(lines) if line.startswith("COPY src ")) + code = next(i for i, line in enumerate(lines) if line.startswith(("COPY src ", "COPY --chown=1000 src "))) for i, line in enumerate(lines): if "apt-get" in line or "--from=joern-build" in line or "frontend-retention-install.py" in line: assert i < code @@ -215,3 +215,14 @@ def test_search_pilot_files_stay_out_of_the_image(): assert private in patterns, f"{private} is not excluded from the build context" # Docker cleans each line, so `!benchmarks/search/private` lets the directory back in as well. assert not any(line.startswith(f"!{private}".rstrip("/")) for line in patterns), f"{private} is let back in" + + +def test_app_layers_carry_their_owner(): + """`COPY --chown` on every layer under /app: a final `chown -R` would store each of its files twice.""" + lines = instructions("Dockerfile") + final_stage = lines[max(i for i, line in enumerate(lines) if line.startswith("FROM ")) :] + assert not any(line.startswith("RUN chown") for line in final_stage), "a chown over /app stores its files twice" + copies = [line for line in final_stage if line.startswith("COPY ") and "--from=" not in line] + assert copies + for line in copies: + assert line.startswith("COPY --chown=1000 "), f"{line} does not name the runtime user"