Repository navigation
fix(catalog): catalog eval receipts keep the group and mode of the files a rerun replaces - #1348
antonwolfy wants to merge 1 commit into
Conversation
…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>
|
Codecov Report❌ Patch coverage is
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
🚀 New features to boost your workflow:
|
|
🔍 OpenCodeReview found 2 issue(s) in this PR. CI at By label: 2 should fix
Test notes (2)
Review details
|
| _keep_owner_and_group(fd, old) | ||
| finally: | ||
| os.close(fd) | ||
| os.chmod(path, stat.S_IMODE(old.st_mode)) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
Summary
#1220:
catalog eval --out-dirstages its four receipts (catalog.json,plan.json,result.json,report.md) as new files and_replace_receiptsrenames them over the previous ones, so a rerun loses their group and mode, the same cause as #1067 and #1165. Onmain, without/catalog.jsonat0o640in a supplementary group (4) and a newly stagedcatalog.json,_replace_receipts(out, staged, backup)leaves group 1000 and mode0o644(the staged file's mode); a0o600report.mdcomes back0o644.Fix option taken: the first one the issue lists. The old receipt's
statis 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 withwrite_text_atomicstraight intooutwould change what those rollback tests observe and what_write_errorleaves behind.Closes #1220
Changes
dokimi/atomic_write.py: newkeep_owner_group_mode(path, old). It gives a file the user owns the group (root: also the owner) ofoldthrough_keep_owner_and_group, then its mode withos.chmod, last, as_publishdoes. A user who may not give the group keeps the file's own group without an error.dokimi/commands/catalog_eval.py:_replace_receiptscalls it through a small_keep_previous_ownerfor 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/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.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
_RollbackFailedstill 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 changecatalog_eval.py, but only itsInputErrorimport 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(onmain16738246):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.main's code the new tests fail:3 failed, 2 passedof the selected five; the first assertion is(1000, '0o644') == (4, '0o640')for the issue's repro.os.chmodfails 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 to0o600fails the first-run test.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
pytestpassesruff,mypy, and schema checks passAGENTS.mdraise/exit and test names the doc, contract or issue it traces to.github/.licenserc.yaml)