From f2bc2921e9dc4caf4be74e585e8818a57297d57d Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 27 Nov 2026 17:56:33 +0000 Subject: [PATCH 1/3] fix(catalog): catalog eval receipts keep the group and mode of the files a rerun replaces (#1220) _replace_receipts renamed freshly staged files over the previous catalog.json, plan.json, result.json and report.md, so a 0o640 receipt in a supplementary group came back 0o664 in the user's primary group. The staged file now gets the previous receipt's mode and, where the user may, its group (root also the owner) before the rename, through the new dokimi.atomic_write.keep_owner_group_mode, which reuses _keep_owner_and_group and sets the mode last. The move-aside rollback is unchanged, a receipt with no previous file keeps the umask mode, and a group the user may not give is skipped without an error. Signed-off-by: Anton Volkov --- ...0_catalog-eval-receipts-keep-group-mode.md | 8 +++ dokimi/dokimi/atomic_write.py | 13 ++++ dokimi/dokimi/commands/catalog_eval.py | 13 ++++ dokimi/tests/test_catalog_eval_cli.py | 60 +++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 dokimi/changelog.d/20261127_140000_catalog-eval-receipts-keep-group-mode.md diff --git a/dokimi/changelog.d/20261127_140000_catalog-eval-receipts-keep-group-mode.md b/dokimi/changelog.d/20261127_140000_catalog-eval-receipts-keep-group-mode.md new file mode 100644 index 0000000000..0ce91cfe40 --- /dev/null +++ b/dokimi/changelog.d/20261127_140000_catalog-eval-receipts-keep-group-mode.md @@ -0,0 +1,8 @@ + + +### Fixed + +- **`catalog eval --out-dir` keeps the group and mode of the receipts a rerun replaces** — `catalog.json`, `plan.json`, `result.json` and `report.md` were staged as new files and renamed over the previous ones, so a `0o640` receipt in a supplementary group came back `0o664` in the user's primary group (the cause of #1067 and #1165). The staged file now gets the previous receipt's mode and, where the user may give it, its group (root also the owner) before the rename, through `dokimi.atomic_write.keep_owner_group_mode`. A user who may not give the group keeps the receipt published in their own group, without an error; a receipt with no previous file keeps the umask mode as before. The previous receipts are still moved aside first and put back as they were when the swap fails. diff --git a/dokimi/dokimi/atomic_write.py b/dokimi/dokimi/atomic_write.py index 2980084473..267d2f27f5 100644 --- a/dokimi/dokimi/atomic_write.py +++ b/dokimi/dokimi/atomic_write.py @@ -260,6 +260,19 @@ def _keep_owner_and_group(fd: int, old: os.stat_result, *, keep_group: bool = Fa raise _OwnerRefused(exc.errno, "root may not give the new file the owner or group of the file it replaces") from exc +def keep_owner_group_mode(path: Path, old: os.stat_result) -> None: + """Give the file at ``path`` (the user's own, not yet published) the group, for root the owner, and the mode of ``old``. + + For a file a caller renames over another, where :func:`write_text_atomic` cannot make it. The group and owner follow :func:`_keep_owner_and_group`: a user who is not root and may not give the group keeps the one the file has, without an error, and root raises ``PermissionError`` when it may not chown. The mode is set last, as a change of owner may clear set-user-ID and set-group-ID. + """ + fd = os.open(path, os.O_RDONLY) + try: + _keep_owner_and_group(fd, old) + finally: + os.close(fd) + os.chmod(path, stat.S_IMODE(old.st_mode)) + + def _existing_stat(path: Path) -> os.stat_result | None: try: # A link is replaced by the rename, so it is the file replaced: its target's mode and group are not kept. diff --git a/dokimi/dokimi/commands/catalog_eval.py b/dokimi/dokimi/commands/catalog_eval.py index 1fd7d599ad..34fe627700 100644 --- a/dokimi/dokimi/commands/catalog_eval.py +++ b/dokimi/dokimi/commands/catalog_eval.py @@ -12,12 +12,14 @@ import os import platform import shutil +import stat import tempfile from collections.abc import Iterator from pathlib import Path from typing import Any from dokimi import __version__ +from dokimi.atomic_write import keep_owner_group_mode from dokimi.artifacts import save_artifact from dokimi.utils import InputError @@ -98,6 +100,16 @@ def _workdir(out: Path) -> Iterator[Path]: shutil.rmtree(work, ignore_errors=True) +def _keep_previous_owner(new: Path, previous: Path) -> None: + """Give the staged receipt the group and mode of the one moved aside to *previous*, if there was one.""" + try: + old = previous.lstat() + except FileNotFoundError: + return + if stat.S_ISREG(old.st_mode): + keep_owner_group_mode(new, old) + + def _replace_receipts(out: Path, staged: Path, backup: Path) -> None: """Swap the four receipt files within *out*; the directory itself is never renamed or removed.""" placed: list[str] = [] @@ -107,6 +119,7 @@ def _replace_receipts(out: Path, staged: Path, backup: Path) -> None: os.replace(out / name, backup / name) for name in sorted(path.name for path in staged.iterdir()): placed.append(name) + _keep_previous_owner(staged / name, backup / name) os.replace(staged / name, out / name) except BaseException as exc: try: diff --git a/dokimi/tests/test_catalog_eval_cli.py b/dokimi/tests/test_catalog_eval_cli.py index 95352bab5d..be6b6e3920 100644 --- a/dokimi/tests/test_catalog_eval_cli.py +++ b/dokimi/tests/test_catalog_eval_cli.py @@ -460,6 +460,66 @@ def flaky(src, dst): assert kept and str(kept[0]) in done.stderr +def _rerun_receipts(tmp_path, name, mode, group=None): + """``out/`` of *mode* (and *group*), then a rerun that stages a new file of that name; the replaced file's stat.""" + from dokimi.commands.catalog_eval import _replace_receipts + + out, staged, backup = tmp_path / "out", tmp_path / "new", tmp_path / "old" + for directory in (out, staged, backup): + directory.mkdir() + (out / name).write_text("old", encoding="utf-8") + os.chmod(out / name, mode) # after creation, so the umask does not hide a loss + if group is not None: + os.chown(out / name, -1, group) + (staged / name).write_text("new", encoding="utf-8") + os.chmod(staged / name, 0o644) + _replace_receipts(out, staged, backup) + assert (out / name).read_text(encoding="utf-8") == "new" + return (out / name).stat() + + +@pytest.mark.skipif(not hasattr(os, "getgroups"), reason="needs POSIX groups") +def test_a_rerun_keeps_the_group_and_mode_of_the_receipt_it_replaces(tmp_path): + other = [g for g in os.getgroups() if g != os.getegid()] + if not other: + pytest.skip("the user has no supplementary group") + info = _rerun_receipts(tmp_path, "catalog.json", 0o640, other[0]) + assert (info.st_gid, oct(info.st_mode & 0o7777)) == (other[0], "0o640") + + +@pytest.mark.skipif(sys.platform == "win32", reason="POSIX modes") +def test_a_rerun_keeps_the_mode_of_the_receipt_it_replaces(tmp_path): + info = _rerun_receipts(tmp_path, "report.md", 0o600) + assert oct(info.st_mode & 0o7777) == "0o600" + + +@pytest.mark.skipif(not hasattr(os, "getgroups"), reason="needs POSIX groups") +def test_a_rerun_publishes_with_the_users_group_when_the_group_cannot_be_kept(tmp_path, monkeypatch): + other = [g for g in os.getgroups() if g != os.getegid()] + if not other or os.geteuid() == 0: + pytest.skip("needs a user who is not root and has a supplementary group") + + def denied(fd, uid, gid): + raise PermissionError(1, "not permitted") + + monkeypatch.setattr(os, "fchown", denied) + info = _rerun_receipts(tmp_path, "report.md", 0o640, other[0]) + assert (info.st_gid, oct(info.st_mode & 0o7777)) == (os.getegid(), "0o640") + + +@pytest.mark.skipif(sys.platform == "win32", reason="POSIX modes") +def test_a_first_run_keeps_the_mode_of_the_staged_receipt(tmp_path): + from dokimi.commands.catalog_eval import _replace_receipts + + out, staged, backup = tmp_path / "out", tmp_path / "new", tmp_path / "old" + for directory in (out, staged, backup): + directory.mkdir() + (staged / "report.md").write_text("new", encoding="utf-8") + os.chmod(staged / "report.md", 0o644) + _replace_receipts(out, staged, backup) + assert oct((out / "report.md").stat().st_mode & 0o7777) == "0o644" + + def test_deeply_nested_skill_tree_is_bundled(tmp_path): root = _catalog(tmp_path) deep = root / "skills" / "alpha" From a7fcdf9dce430140a227a5567f6f43477c04da8c Mon Sep 17 00:00:00 2001 From: Alexander Rybkin Date: Sat, 28 Nov 2026 02:26:48 -0800 Subject: [PATCH 2/3] fix(catalog): a rerun receipt gets no ACL entry of the directory default The staged receipt inherits an access ACL from the output directory's default ACL; the chmod that restores the old mode widened its mask, so a named user the old receipt never granted could read it. Drop the inherited access ACL before the chmod, as _publish does. Signed-off-by: Alexander Rybkin --- ...0_catalog-eval-receipts-keep-group-mode.md | 2 +- dokimi/dokimi/atomic_write.py | 3 +++ dokimi/tests/test_catalog_eval_cli.py | 27 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/dokimi/changelog.d/20261127_140000_catalog-eval-receipts-keep-group-mode.md b/dokimi/changelog.d/20261127_140000_catalog-eval-receipts-keep-group-mode.md index 0ce91cfe40..9a275fe6e0 100644 --- a/dokimi/changelog.d/20261127_140000_catalog-eval-receipts-keep-group-mode.md +++ b/dokimi/changelog.d/20261127_140000_catalog-eval-receipts-keep-group-mode.md @@ -5,4 +5,4 @@ SPDX-License-Identifier: Apache-2.0 ### Fixed -- **`catalog eval --out-dir` keeps the group and mode of the receipts a rerun replaces** — `catalog.json`, `plan.json`, `result.json` and `report.md` were staged as new files and renamed over the previous ones, so a `0o640` receipt in a supplementary group came back `0o664` in the user's primary group (the cause of #1067 and #1165). The staged file now gets the previous receipt's mode and, where the user may give it, its group (root also the owner) before the rename, through `dokimi.atomic_write.keep_owner_group_mode`. A user who may not give the group keeps the receipt published in their own group, without an error; a receipt with no previous file keeps the umask mode as before. The previous receipts are still moved aside first and put back as they were when the swap fails. +- **`catalog eval --out-dir` keeps the group and mode of the receipts a rerun replaces** — `catalog.json`, `plan.json`, `result.json` and `report.md` were staged as new files and renamed over the previous ones, so a `0o640` receipt in a supplementary group came back `0o664` in the user's primary group (the cause of #1067 and #1165). The staged file now gets the previous receipt's mode and, where the user may give it, its group (root also the owner) before the rename, through `dokimi.atomic_write.keep_owner_group_mode`. A user who may not give the group keeps the receipt published in their own group, without an error; a receipt with no previous file keeps the umask mode as before. An access ACL the staged file inherited from the output directory's default ACL is dropped before the mode is set, so the receipt grants no named user more than the one it replaces. The previous receipts are still moved aside first and put back as they were when the swap fails. diff --git a/dokimi/dokimi/atomic_write.py b/dokimi/dokimi/atomic_write.py index 267d2f27f5..04d56e960c 100644 --- a/dokimi/dokimi/atomic_write.py +++ b/dokimi/dokimi/atomic_write.py @@ -270,6 +270,9 @@ def keep_owner_group_mode(path: Path, old: os.stat_result) -> None: _keep_owner_and_group(fd, old) finally: os.close(fd) + # As in _publish: an access ACL inherited from the directory's default would stay effective through the chmod's mask. + with contextlib.suppress(AttributeError, OSError): + os.removexattr(path, "system.posix_acl_access") os.chmod(path, stat.S_IMODE(old.st_mode)) diff --git a/dokimi/tests/test_catalog_eval_cli.py b/dokimi/tests/test_catalog_eval_cli.py index be6b6e3920..9851f0e82b 100644 --- a/dokimi/tests/test_catalog_eval_cli.py +++ b/dokimi/tests/test_catalog_eval_cli.py @@ -10,6 +10,7 @@ import io import os import re +import struct import subprocess import sys from pathlib import Path @@ -507,6 +508,32 @@ def denied(fd, uid, gid): assert (info.st_gid, oct(info.st_mode & 0o7777)) == (os.getegid(), "0o640") +def _acl(*entries): + """A ``system.posix_acl_*`` value: version 2, then (tag, perm, id) per entry.""" + return struct.pack(" Date: Sat, 28 Nov 2026 11:54:14 +0000 Subject: [PATCH 3/3] fix(catalog): a failed receipt swap removes a read-only new receipt before restoring the old ones A staged receipt now takes the mode of the one it replaces, so a read-only previous receipt makes a read-only new one. Windows refuses to unlink a read-only file, so the rollback of a later failed rename stopped with _RollbackFailed and left a partial set. The rollback now makes the placed receipt writable before it removes it. Settles the review thread on catalog_eval.py:122. Signed-off-by: Anton Volkov --- ...0_catalog-eval-receipts-keep-group-mode.md | 2 +- dokimi/dokimi/commands/catalog_eval.py | 11 +++- dokimi/tests/test_catalog_eval_rollback.py | 65 +++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 dokimi/tests/test_catalog_eval_rollback.py diff --git a/dokimi/changelog.d/20261127_140000_catalog-eval-receipts-keep-group-mode.md b/dokimi/changelog.d/20261127_140000_catalog-eval-receipts-keep-group-mode.md index 9a275fe6e0..a96e7c5fae 100644 --- a/dokimi/changelog.d/20261127_140000_catalog-eval-receipts-keep-group-mode.md +++ b/dokimi/changelog.d/20261127_140000_catalog-eval-receipts-keep-group-mode.md @@ -5,4 +5,4 @@ SPDX-License-Identifier: Apache-2.0 ### Fixed -- **`catalog eval --out-dir` keeps the group and mode of the receipts a rerun replaces** — `catalog.json`, `plan.json`, `result.json` and `report.md` were staged as new files and renamed over the previous ones, so a `0o640` receipt in a supplementary group came back `0o664` in the user's primary group (the cause of #1067 and #1165). The staged file now gets the previous receipt's mode and, where the user may give it, its group (root also the owner) before the rename, through `dokimi.atomic_write.keep_owner_group_mode`. A user who may not give the group keeps the receipt published in their own group, without an error; a receipt with no previous file keeps the umask mode as before. An access ACL the staged file inherited from the output directory's default ACL is dropped before the mode is set, so the receipt grants no named user more than the one it replaces. The previous receipts are still moved aside first and put back as they were when the swap fails. +- **`catalog eval --out-dir` keeps the group and mode of the receipts a rerun replaces** — `catalog.json`, `plan.json`, `result.json` and `report.md` were staged as new files and renamed over the previous ones, so a `0o640` receipt in a supplementary group came back `0o664` in the user's primary group (the cause of #1067 and #1165). The staged file now gets the previous receipt's mode and, where the user may give it, its group (root also the owner) before the rename, through `dokimi.atomic_write.keep_owner_group_mode`. A user who may not give the group keeps the receipt published in their own group, without an error; a receipt with no previous file keeps the umask mode as before. An access ACL the staged file inherited from the output directory's default ACL is dropped before the mode is set, so the receipt grants no named user more than the one it replaces. The previous receipts are still moved aside first and put back as they were when the swap fails, and a new receipt that took a read-only mode is made writable before that rollback removes it, as Windows refuses to unlink a read-only file. diff --git a/dokimi/dokimi/commands/catalog_eval.py b/dokimi/dokimi/commands/catalog_eval.py index 34fe627700..d634e5a00e 100644 --- a/dokimi/dokimi/commands/catalog_eval.py +++ b/dokimi/dokimi/commands/catalog_eval.py @@ -110,6 +110,15 @@ def _keep_previous_owner(new: Path, previous: Path) -> None: keep_owner_group_mode(new, old) +def _unlink_receipt(path: Path) -> None: + """Remove a receipt this swap placed; a read-only one (the mode of a read-only previous receipt) is made writable first, as Windows refuses to unlink it.""" + try: + path.unlink(missing_ok=True) + except PermissionError: + os.chmod(path, stat.S_IREAD | stat.S_IWRITE) + path.unlink(missing_ok=True) + + def _replace_receipts(out: Path, staged: Path, backup: Path) -> None: """Swap the four receipt files within *out*; the directory itself is never renamed or removed.""" placed: list[str] = [] @@ -124,7 +133,7 @@ def _replace_receipts(out: Path, staged: Path, backup: Path) -> None: except BaseException as exc: try: for name in placed: - (out / name).unlink(missing_ok=True) + _unlink_receipt(out / name) for name in _OUTPUTS: if (backup / name).exists(): os.replace(backup / name, out / name) diff --git a/dokimi/tests/test_catalog_eval_rollback.py b/dokimi/tests/test_catalog_eval_rollback.py new file mode 100644 index 0000000000..356d618c72 --- /dev/null +++ b/dokimi/tests/test_catalog_eval_rollback.py @@ -0,0 +1,65 @@ +# Copyright (C) 2026 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +"""The receipt swap of catalog eval: its rollback and a previous receipt that is a link.""" + +from __future__ import annotations + +import os +import sys +from pathlib import Path + +import pytest + + +@pytest.mark.skipif(sys.platform == "win32", reason="POSIX modes and links") +def test_a_rerun_replaces_a_linked_receipt_without_taking_its_targets_mode(tmp_path): + from dokimi.commands.catalog_eval import _replace_receipts + + out, staged, backup = tmp_path / "out", tmp_path / "new", tmp_path / "old" + for directory in (out, staged, backup): + directory.mkdir() + target = tmp_path / "target.md" + target.write_text("target", encoding="utf-8") + os.chmod(target, 0o600) + (out / "report.md").symlink_to(target) + (staged / "report.md").write_text("new", encoding="utf-8") + os.chmod(staged / "report.md", 0o644) + _replace_receipts(out, staged, backup) + assert not (out / "report.md").is_symlink() + assert (out / "report.md").read_text(encoding="utf-8") == "new" + assert oct((out / "report.md").stat().st_mode & 0o7777) == "0o644" + + +@pytest.mark.skipif(sys.platform == "win32", reason="POSIX modes") +def test_a_failed_swap_restores_receipts_when_the_unlink_of_a_read_only_one_is_refused(tmp_path, monkeypatch): + from dokimi.commands.catalog_eval import _replace_receipts + + out, staged, backup = tmp_path / "out", tmp_path / "new", tmp_path / "old" + for directory in (out, staged, backup): + directory.mkdir() + for name in ("catalog.json", "plan.json"): + (out / name).write_text(f"old {name}", encoding="utf-8") + (staged / name).write_text(f"new {name}", encoding="utf-8") + os.chmod(out / "catalog.json", 0o444) + real_replace, real_unlink = os.replace, Path.unlink + failure = OSError("injected plan publication failure") + + def replace(src, dst): + if Path(dst) == out / "plan.json" and Path(src).parent == staged: + raise failure + return real_replace(src, dst) + + def unlink(self, missing_ok=False): + if self.exists() and not self.stat().st_mode & 0o200: # Windows refuses a read-only file + raise PermissionError(13, "read-only", str(self)) + return real_unlink(self, missing_ok=missing_ok) + + monkeypatch.setattr("dokimi.commands.catalog_eval.os.replace", replace) + monkeypatch.setattr(Path, "unlink", unlink) + with pytest.raises(OSError) as raised: + _replace_receipts(out, staged, backup) + assert raised.value is failure + assert {p.name: p.read_text(encoding="utf-8") for p in out.iterdir()} == { + "catalog.json": "old catalog.json", "plan.json": "old plan.json"} + assert oct((out / "catalog.json").stat().st_mode & 0o7777) == "0o444"