Skip to content

fix(catalog): catalog eval receipts keep the group and mode of the files a rerun replaces - #1348

Open
antonwolfy wants to merge 1 commit into
intel:mainfrom
antonwolfy:fix/catalog-receipts-owner
Open

antonwolfy wants to merge 1 commit into
intel:mainfrom
antonwolfy:fix/catalog-receipts-owner

Conversation

@antonwolfy

Copy link
Copy Markdown
Contributor

Summary

#1220: catalog eval --out-dir stages its four receipts (catalog.json, plan.json, result.json, report.md) as new files and _replace_receipts renames them over the previous ones, so a rerun loses their group and mode, the same cause as #1067 and #1165. On main, with out/catalog.json at 0o640 in a supplementary group (4) and a newly staged catalog.json, _replace_receipts(out, staged, backup) leaves group 1000 and mode 0o644 (the staged file's mode); a 0o600 report.md comes back 0o644.

Fix option taken: the first one the issue lists. The old receipt's stat is read once it is moved aside, and the staged file gets its mode and, where the user may, its group before the rename. This keeps the move-aside rollback, which the existing tests pin, and the directory is still never renamed or removed. Staging with write_text_atomic straight into out would change what those rollback tests observe and what _write_error leaves behind.

Closes #1220

Changes

  • dokimi/atomic_write.py: new keep_owner_group_mode(path, old). It gives a file the user owns the group (root: also the owner) of old through _keep_owner_and_group, then its mode with os.chmod, last, as _publish does. A user who may not give the group keeps the file's own group without an error.
  • dokimi/commands/catalog_eval.py: _replace_receipts calls it through a small _keep_previous_owner for each staged name whose previous receipt was moved aside. A name with no previous file, or a previous one that is not a regular file, is placed as before (umask mode).
  • Tests in tests/test_catalog_eval_cli.py: group and mode kept (skips when the user has no supplementary group), mode alone, a refused group change publishes in the user's own group with the mode kept, and a first run keeps the staged mode.
  • Changelog fragment (Fixed).

Risk

A rerun now leaves the receipts with the group and mode the previous ones had, not the umask mode: a receipt made read-only or group-private stays so. The rollback is unchanged: it restores the old inodes, which kept their group and mode, and _RollbackFailed still names the kept backup. As root, a refused chown or chmod of a staged receipt raises and the previous receipts are put back, where the file was left in the root's group before. Open PRs #1190, #1247 and #1324 also change catalog_eval.py, but only its InputError import line and _emit; this PR adds one import line and _keep_previous_owner, so a merge with them is trivial.

Testing

On Linux, at f2bc2921 (on main 16738246):

  • make ci-local: 38 passed, 0 failed, 15 skipped (docker smoke, pinned Claude Code, jobs conditional on a PR, matrix legs, conda-forge, atheris fuzz, semgrep). 30116 tests passed, 23 skipped; changed-line coverage 100% (16 lines); mutation of changed lines: 1 of 1 killed.
  • On main's code the new tests fail: 3 failed, 2 passed of the selected five; the first assertion is (1000, '0o644') == (4, '0o640') for the issue's repro.
  • Mutants: dropping the os.chmod fails the group and mode, mode and refused-group tests; dropping the group change fails the group and mode test ((1000, '0o640') == (4, '0o640')); making a first run chmod the staged file to 0o600 fails the first-run test.
  • Not run: the other Python versions (3.11, 3.12, 3.14), Windows, and a run as root.

Open questions

None.

Revert

A plain revert restores the old behaviour (rerun receipts get the umask mode and the user's primary group). Nothing is stored.

Checklist

  • Tests added/updated and pytest passes
  • ruff, mypy, and schema checks pass
  • Docs updated (or N/A) — see the docs-drift checklist in CONTRIBUTING.md
  • Self-reviewed per "Before opening a PR" in AGENTS.md
  • Each new raise/exit and test names the doc, contract or issue it traces to
  • No ad-hoc run artifacts committed
  • New files carry the SPDX license header (or are exempt in .github/.licenserc.yaml)

…les a rerun replaces (intel#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 <antonwolfy@gmail.com>
@antonwolfy
antonwolfy requested a review from a team as a code owner November 27, 2026 18:34
@antonwolfy antonwolfy self-assigned this Nov 27, 2026
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 93.75000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 96.19%. Comparing base (1673824) to head (f2bc292).

Files with missing lines Patch % Lines
dokimi/dokimi/commands/catalog_eval.py 90.00% 0 Missing and 1 partial ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1348   +/-   ##
=======================================
  Coverage   96.19%   96.19%           
=======================================
  Files         499      499           
  Lines       66465    66481   +16     
  Branches    11542    11543    +1     
=======================================
+ Hits        63933    63949   +16     
+ Misses       1563     1559    -4     
- Partials      969      973    +4     
Files with missing lines Coverage Δ
dokimi/dokimi/atomic_write.py 98.75% <100.00%> (+0.04%) ⬆️
dokimi/dokimi/commands/catalog_eval.py 98.40% <90.00%> (-0.48%) ⬇️

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@exai-bot

exai-bot commented Nov 27, 2026 •

Copy link
Copy Markdown

🔍 OpenCodeReview found 2 issue(s) in this PR.

CI at f2bc292: 21 passing, 4 skipped.

By label: 2 should fix

Label Location Finding
should fix catalog_eval.py:122 Impact: on Windows, a failed swap can leave a partial set of new receipts instead of restoring the previous set.
should fix atomic_write.py:273 Impact: a rerun can grant an unrelated named user read access to a previously group-private receipt, despite preserving its numeric m...
Test notes (2)
  • dokimi/dokimi/atomic_write.py:270: With _keep_owner_and_group(fd, old) changed to pass, the tests still pass: dokimi/tests/test_catalog_eval_cli.py. If this condition matters, a test should pin it.
  • dokimi/dokimi/atomic_write.py:272: With finally: os.close(fd) changed to finally: pass, the tests still pass: dokimi/tests/test_catalog_eval_cli.py. If this condition matters, a test should pin it.
Review details
  • Commit reviewed: f2bc292 against merge base 1673824
  • Coverage: 4 files in 1 groups
  • Engine: OpenCodeReview v1.13.5-exai.4.8f73eada, us.openai.gpt-6.1-sol
  • Usage: 338k tokens (224k from cache), 2m13s
  • Tool calls: 28 (17 file_read, 5 code_search, 3 ci_status, 2 code_comment, 1 local_check_results)
  • Preserve group mode for catalog evaluation receipts: 4 files

_keep_owner_and_group(fd, old)
finally:
os.close(fd)
os.chmod(path, stat.S_IMODE(old.st_mode))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security · should fix
Impact: a rerun can grant an unrelated named user read access to a previously group-private receipt, despite preserving its numeric mode and group.

Trigger: an old 0640 receipt has no access ACL, while the output directory has a default ACL containing user:12345:r-x with mask::---. New staging directories/files inherit that ACL; the staged file initially masks off the named user's access, but this chmod changes its ACL mask to r--, activating that user's read permission when published.

Evidence: _publish explicitly removes system.posix_acl_access before its corresponding chmod to prevent this exact permission expansion; this helper omits that step.

Workaround: remove the output directory's default ACL before rerunning. Remove the inherited access ACL before restoring the mode, as _publish does, and add a real-ACL regression test.

Should fix: A run shows it; it was not judged to be met in ordinary use.

Shown by running: exit 1 (at the head) · exit 0 (at the merge base) · exit 0 (at the head with the fix)

python -m pytest -q -p no:randomly -p no:cacheprovider tests/test_candidate_c2_inherited_acl.py

What the proof does: Creates a real Linux default ACL using extended attributes, calls the real receipt staging and replacement code, and checks that the published receipt does not grant the inherited named user effective read permission.

The problem shows as the output INHERITED_ACL_GRANTED_NAMED_USER_READ.

Script: dokimi/tests/test_candidate_c2_inherited_acl.py (50 lines).

        assert previous.read_text() == 'new receipt'
>       assert not named_user_can_read(previous), 'INHERITED_ACL_GRANTED_NAMED_USER_READ'
E       AssertionError: INHERITED_ACL_GRANTED_NAMED_USER_READ
E       assert not True
E        +  where True = named_user_can_read(PosixPath('/tmp/pytest-of-root/pytest-0/test_receipt_does_not_activate0/out/catalog.json'))

tests/test_candidate_c2_inherited_acl.py:50: AssertionError
=========================== short test summary info ============================
FAILED tests/test_candidate_c2_inherited_acl.py::test_receipt_does_not_activate_inherited_named_user
1 failed in 0.50s

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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug · should fix
Impact: on Windows, a failed swap can leave a partial set of new receipts instead of restoring the previous set.

Trigger: mark the previous catalog.json read-only, then encounter a publication failure for a later receipt (for example, deny the plan.json rename).

Evidence: this call now makes the staged catalog read-only before publishing it; Windows rejects (out / name).unlink() for that published file in the rollback loop, so restoration stops and raises _RollbackFailed. Previously the staged replacement remained writable, allowing that rollback to proceed.

Workaround: remove the read-only attribute from previous receipts before rerunning. Clear the read-only attribute on newly placed files before removing them during Windows rollback, and add a regression test with a later rename failure.

Should fix: A run shows it; it was not judged to be met in ordinary use.

This proof replaces part of the code with stand-ins (monkeypatch): it shows the code path, not the real dependency or platform.

Shown by running: exit 1 (at the head) · exit 0 (at the merge base) · exit 0 (at the head with the fix)

python -m pytest -q -p no:randomly -p no:cacheprovider tests/test_candidate_c1_windows_rollback.py

What the proof does: Calls the real receipt swap with a later rename failure and simulated Windows read-only unlink semantics, then checks that the original exception and all previous receipt contents are restored.

The problem shows as the output WINDOWS_ROLLBACK_DID_NOT_RESTORE_RECEIPTS.

Script: dokimi/tests/test_candidate_c1_windows_rollback.py (44 lines).

            caught = exc
        actual = {name: (out / name).read_bytes() if (out / name).exists() else None for name in before}
>       assert caught is failure and actual == before, 'WINDOWS_ROLLBACK_DID_NOT_RESTORE_RECEIPTS'
E       AssertionError: WINDOWS_ROLLBACK_DID_NOT_RESTORE_RECEIPTS
E       assert (_RollbackFailed('cannot restore previous receipts, kept at /tmp/pytest-of-root/pytest-0/test_read_only_publication_can0/old: simulated Windows read-only unlink refusal') is OSError('injected plan publication failure'))

tests/test_candidate_c1_windows_rollback.py:44: AssertionError
=========================== short test summary info ============================
FAILED tests/test_candidate_c1_windows_rollback.py::test_read_only_publication_can_be_rolled_back
1 failed in 0.54s

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

catalog eval: the receipts it rewrites lose their group and mode (_replace_receipts renames new files over them)

3 participants