diff --git a/bin/docker_command.py b/bin/docker_command.py index eea0a460..3c683612 100644 --- a/bin/docker_command.py +++ b/bin/docker_command.py @@ -71,6 +71,12 @@ def unlimited(arguments: list[str]) -> bool: return subcommand in STARTS_CONTAINER and not limited and not helped(arguments) +def helped(arguments: list[str]) -> bool: + """Whether docker only prints its help: a --help before the subcommand or as the first word after it, not one the container's own command takes.""" + after = subcommand_at(arguments)[-1] + 1 + return "--help" in arguments[:after + 1] + + def capped(arguments: list[str], share: str) -> list[str]: """docker's arguments, with each limit a container they make lacks added right after the subcommand. @@ -109,9 +115,3 @@ def option_at(arguments: list[str], options: tuple[str, ...], fits: typing.Calla if word == option and fits(following): return Option(at, 2, following) return None - - -def helped(arguments: list[str]) -> bool: - """Whether docker only prints its help: a --help before the subcommand or as the first word after it, not one the container's own command takes.""" - after = subcommand_at(arguments)[-1] + 1 - return "--help" in arguments[:after + 1] diff --git a/bin/rule_hook/rules.py b/bin/rule_hook/rules.py index 6f3d6f36..f0ad6703 100644 --- a/bin/rule_hook/rules.py +++ b/bin/rule_hook/rules.py @@ -6,7 +6,7 @@ import typing from round_titles import CAP, lacks_counts, past_cap from git_command import commits from rule_hook.heavy_command import heavy -from shell_command import launched, Simple +from shell_command import launched, Simple, tool_name from review_folder import REPORTS from docker_command import unlimited @@ -32,7 +32,7 @@ RULES: dict[RuleName, Rule] = { ), "docker-run-memory": Rule( "a container made with docker run or docker create has a memory limit and a swap limit, as large as its work needs, such as `--memory 4g --memory-swap 4g`, so a runaway container fails alone instead of starving the machine; a swap limit equal to the memory limit keeps it out of swap", - "docker run, docker create, docker container run and docker container create, whether direct or through a launcher such as nice or timeout. One under run-heavy passes, as run-heavy's docker adds the limits, unless it names docker by its path or sets its own PATH. A container a script or docker compose starts is not checked", + "docker run, docker create, docker container run and docker container create, whether direct or through a launcher such as nice or timeout. One under run-heavy passes, as run-heavy's docker adds the limits, unless it names docker by its path, sets its own PATH, or runs through sudo or doas. A container a script or docker compose starts is not checked", ), "ignorefile-sorted": Rule( "each block of an ignore file is sorted in code-point order, as LC_ALL=C sort does, since its order carries no meaning; where order matters, such as a re-include after its exclude, split the block with a blank line or a comment", @@ -156,8 +156,9 @@ def unlimited_container(words: list[str]) -> bool: docker = launched(words, {"docker"}) if docker is None or not unlimited(docker.arguments): return False - # On Linux, run-heavy's docker adds the limits a container lacks, where PATH finds it: docker named bare, with no PATH of the command's own. - capped = "run-heavy" in docker.launchers and words[len(words) - len(docker.arguments) - 1] == "docker" and not any(word.startswith("PATH=") for word in words) + # On Linux, run-heavy's docker adds the limits a container lacks, where PATH finds it: docker named bare, with no PATH of the command's own, and no sudo or doas, which reset PATH. + before = words[:len(words) - len(docker.arguments) - 1] + capped = "run-heavy" in docker.launchers and words[len(before)] == "docker" and not any(word.startswith("PATH=") or tool_name(word) in ("doas", "sudo") for word in before) return not capped diff --git a/bin/rule_hook/test_rule_hook.py b/bin/rule_hook/test_rule_hook.py index bd0f4265..6c36d098 100644 --- a/bin/rule_hook/test_rule_hook.py +++ b/bin/rule_hook/test_rule_hook.py @@ -117,7 +117,7 @@ class CommandBreaches(unittest.TestCase): self.assertEqual(checked(command), []) def test_a_docker_run_or_create_without_a_memory_limit_breaks_docker_run_memory(self): - for command in ["docker run --rm node:24.22.1-alpine3.24 node x.js", "cd /w && docker run -v /w:/w alpine:3.22.2 true", "docker create alpine:3.22.2", "docker container run alpine:3.22.2 true", "docker container create alpine:3.22.2", "nice -n 19 docker run alpine:3.22.2 true", "docker -H unix:///d.sock run alpine:3.22.2 true", "docker run --memory-swap 4g alpine:3.22.2 true", "docker run -m 4g alpine:3.22.2 true", "docker run --memory=4g alpine:3.22.2 sh -c 'x --memory-swap 4g'", "docker run -m 4g alpine:3.22.2 tool --memory-swap", "docker run -m 4g --memory-swap -1 alpine:3.22.2 true", "docker run -m 4g --memory-swap=0 alpine:3.22.2 true", "docker run --rm python:3.13.1 python -m pytest", "docker run alpine:3.22.2 git commit -m x", "timeout 60 docker run alpine:3.22.2 true", "docker run node:24.22.1-alpine3.24 node --help", "docker run alpine:3.22.2 --help", "docker run -m 4g alpine:3.22.2 sleep 60", "~/.claude/bin/run-heavy /usr/bin/docker run alpine:3.22.2 true", "docker run --memory-swap 4g alpine:3.22.2 grep -m 1 x", "run-heavy env PATH=/usr/bin docker run alpine:3.22.2 true"]: + for command in ["docker run --rm node:24.22.1-alpine3.24 node x.js", "cd /w && docker run -v /w:/w alpine:3.22.2 true", "docker create alpine:3.22.2", "docker container run alpine:3.22.2 true", "docker container create alpine:3.22.2", "nice -n 19 docker run alpine:3.22.2 true", "docker -H unix:///d.sock run alpine:3.22.2 true", "docker run --memory-swap 4g alpine:3.22.2 true", "docker run -m 4g alpine:3.22.2 true", "docker run --memory=4g alpine:3.22.2 sh -c 'x --memory-swap 4g'", "docker run -m 4g alpine:3.22.2 tool --memory-swap", "docker run -m 4g --memory-swap -1 alpine:3.22.2 true", "docker run -m 4g --memory-swap=0 alpine:3.22.2 true", "docker run --rm python:3.13.1 python -m pytest", "docker run alpine:3.22.2 git commit -m x", "timeout 60 docker run alpine:3.22.2 true", "docker run node:24.22.1-alpine3.24 node --help", "docker run alpine:3.22.2 --help", "docker run -m 4g alpine:3.22.2 sleep 60", "~/.claude/bin/run-heavy /usr/bin/docker run alpine:3.22.2 true", "docker run --memory-swap 4g alpine:3.22.2 grep -m 1 x", "run-heavy env PATH=/usr/bin docker run alpine:3.22.2 true", "run-heavy sudo docker run alpine:3.22.2 true"]: with self.subTest(command): self.assertEqual(checked(command), ["docker-run-memory"]) diff --git a/bin/run_heavy/capped_docker.py b/bin/run_heavy/capped_docker.py index ac89ef2e..d9d9d199 100644 --- a/bin/run_heavy/capped_docker.py +++ b/bin/run_heavy/capped_docker.py @@ -32,4 +32,4 @@ def main(argv: list[str], env: typing.Mapping[str, str]) -> int: def without_path_entry(path: str) -> str: """path less PATH_ENTRY, so the real docker is the one found.""" - return os.pathsep.join(entry for entry in path.split(os.pathsep) if pathlib.Path(entry or ".").resolve() != PATH_ENTRY) + return os.pathsep.join(entry for entry in path.split(os.pathsep) if pathlib.Path(entry).resolve() != PATH_ENTRY) diff --git a/bin/run_heavy/test_run_heavy.py b/bin/run_heavy/test_run_heavy.py index 1519072a..f028d6b7 100644 --- a/bin/run_heavy/test_run_heavy.py +++ b/bin/run_heavy/test_run_heavy.py @@ -115,6 +115,16 @@ class RunHeavy(unittest.TestCase): result = self.run_tool("sh", "-c", "command -v docker || echo none", env={**self.env, "PATH": folder.name}) self.assertEqual((result.returncode, result.stdout), (0, "none\n")) + def test_the_capped_docker_passes_a_call_on_unchanged_without_a_share(self): + env = self.fake_docker() + env.pop("RUN_HEAVY_MEMORY", None) + shim = pathlib.Path(TOOL).parent / "run_heavy" / "path_entry" / "docker" + result = subprocess.run([str(shim), "run", "img"], capture_output=True, text=True, env=env, timeout=30) + self.assertEqual((result.returncode, result.stdout), (0, "run img\n")) + + def test_no_cap_where_the_memory_is_unknown(self): + self.assertEqual(main.capping(None, 4, self.fake_docker()), {}) + def test_a_capped_docker_that_finds_no_docker_exits_127_saying_so(self): folder = tempfile.TemporaryDirectory() self.addCleanup(folder.cleanup)