Repository navigation
fix(import): read the staged cron store once and replace it, never write through it - #18240
Conversation
UX Review (Opus 5.5) — ⏭️ skippedRevision |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Check that Windows CI's existing portability import tests pass. They are the only run of the [DESIGN-REVIEWED] 2fea5a3 Description read[DESCRIPTION-READ] 08ca8f5343dffd984d05f0df4eb4be748c7e2de626a61e404de195b13200b3d6 That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
Opus 5.5 Review — ✅ no blocking findingsReviewed Review detailsNo findings. FINDING — src/kiro_crew/portability.py:1200 — [OPUS-REVIEWED] 2fea5a3 Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS The Windows fallback ( What this change shipsInventory (4 items) — 4 justifiedIntent: make the settings import's cron sanitizer rewrite only the staged
[FIRST-PRINCIPLES-REVIEWED] 2fea5a3 Description read[DESCRIPTION-READ] 6f6fff2d04dcf88fecdc9f84bf197d97c7c08a32a0544fdb4e03dbbc78bb76cd That sha256 names the PR description this verdict read. Recompute it with |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
d871a8f to
2fea5a3
Compare
|
CrysisDeu
left a comment
There was a problem hiding this comment.
Approving at 2fea5a3. Read the full diff plus the owning spec; nothing blocking. Non-blocking notes, worth a follow-up or a quick fix before merge:
- [MINOR] src/kiro_crew/portability.py:1138-1141 --
pin_parent(..., refusal=OSError)can raise (ELOOP/ENOTDIR on a component, or any otherOSError) and nothing in_sanitize_imported_cronscatches it, so the wholeapply_import_zipaborts instead of taking the documented "unreadable store ->{"jobs": []}+_UNREADABLE_STORE" arm. Fail-closed on a compromised staging tree is the right direction (the parent is the import's own temp dir), but the docstring ("a missing file, or a directory at that name, is left alone") and the new cli.md paragraph do not say that a refused PARENT now fails the import. One sentence in each, or catch and route to the empty-store arm, so the behaviour is stated rather than incidental. - [NIT] src/kiro_crew/portability.py:1193 (
_publish_staged_cron_store) --from kiro_crew.atomic_write import pinned_parent_replace_supportedis function-local whileatomic_writefrom the same module is already imported at the top (line 33); there is no import cycle to justify the deferral, so hoist it. - [NIT] test/test_portability.py (new class) -- all new tests are
skipif(win32), so thedir_fd is Nonebranch (open_file_no_reparse(nonblocking=True)+ by-nameatomic_write) is exercised on Windows only by pre-existing import tests, none of which plant a reparse point. Windows shards are green on this head, so the branch at least runs; amonkeypatch.setattr(pinned_fs, "supports_pinned_walk", lambda: False)variant of the FIFO/alias-at-read-time test on POSIX would pin the fallback's refusal logic cheaply. Design lane already asked the author to watch Windows CI; this is the follow-up that makes it a test rather than a watch.
Problem / Motivation
Goal: The settings import's cron sanitizer writes the sanitized store only to the staged
crons.jsonit read, and never through a link or hardlink alias that appears at that name.portability._sanitize_imported_cronsread the stagedcrons.jsonwithread_text()by name and rewrote it withwrite_text()by name, at three call sites. Each call resolved the path again. A link or hardlink alias swapped in between the read and a rewrite made the rewrite follow it and truncate whatever file it named. A link already present at read time was followed and its target judged as the store.Why it matters
The staged directory is the import's own owner-only temporary directory, so only a same-UID process that can predict it and race the import can do this. That makes the exposure smaller than the merge-restore sites #18090 fixes. It is the same validate-then-reopen pattern, though, and it lets a write land outside the staging tree.
Not a goal
snapshot._merge_crons) still reads it by name. fix(restore): resolve merge core-file and cron-store names once #18090 changes that reader, and this PR does not touch it.crons.jsonname is still left alone, as it was before.What changed (motivation → approach → change)
Symptom: a rewrite could land in a file the import never pointed at. Root cause: the read and every write resolved the path separately. Change, all in
src/kiro_crew/portability.py:_sanitize_imported_cronspins the staged directory once (pinned_fs.pin_parent) where the platform supports it._read_staged_cron_storeopens the store once withO_NOFOLLOW|O_NONBLOCK|O_CLOEXECrelative to that descriptor (platform_compat.open_file_no_reparsewhere pinning is unavailable). It checks the descriptor is one regular file with one link and reads through it as UTF-8. A link, FIFO, hardlink alias or undecodable bytes count as an unreadable store, so the existing recovery arm replaces it with{"jobs": []}and reports_UNREADABLE_STORE. This followssnapshot_merge._read_cron_store_oncein fix(restore): resolve merge core-file and cron-store names once #18090, which is not on main yet._publish_staged_cron_storedoes every rewrite withatomic_write(..., parent_dir_fd=<pinned dir>). A rename replaces the directory entry and never follows it, so a link swapped in after the read is replaced, and its target is left untouched.notification_settings.jsonwas rewritten by name in_vet_archive_settings(the one sibling of this pattern inportability.py, raised by the First Principles lane). It now usesatomic_write, as theui-prefs.jsonbranch beside it already does.docs/system-specs/modules/cli.mdgets a paragraph next to the dashboard-import staging paragraph.Backwards compatibility
Compatible: the zip extraction already skips link entries and cannot create a hardlink or FIFO, so every store a real archive produces is one regular file and takes the same path as before.
Reader: src/kiro_crew/portability.py:apply_import_zip -- entry: crew page (dashboard import) -- it uses the returned
(dropped, paused)and the store at the same name. Both are unchanged for a regular file. For a store that is not one regular file, it now gets the existing unreadable-store result:test_an_alias_at_read_time_is_refused_and_replaced_with_an_empty_store.Tests
New class
TestTheCronSanitizerResolvesTheStoreOnceintest/test_portability.py, POSIX only:test_an_alias_swapped_in_before_the_rewrite_is_replaced_not_written_through[symlink|hardlink]swaps the staged store for an alias of a victim file from inside_vet_shell_command, after the read and before the rewrite. It checks the victim is byte-identical and the store is a regular single-link file holding the paused job.test_an_alias_at_read_time_is_refused_and_replaced_with_an_empty_store[symlink|hardlink]checks that a well-formed store behind an alias is not read or modified.test_a_fifo_at_the_name_does_not_hang_the_importandtest_a_missing_store_is_left_alone.test_the_staged_notification_settings_are_replaced_not_written_through[symlink|hardlink]swaps the staged settings from insideparse_imported_settings; both cases fail when the by-namewrite_textis restored.Red on base: 5 of 6 fail on
origin/main(the missing-store guard passes on both). Mutations, each run against the worktree's ownsrc(PYTHONPATHset and checked withkiro_crew.__file__):st_nlinkcheck: 1 fails (hardlink at read time)O_NOFOLLOW: 1 fails (symlink at read time)O_NONBLOCK: the FIFO test hangs untiltimeouttest/test_portability.pyandtest/test_mcp_cron_security.pytogether: 707 passed, 5 skipped (with the notification-settings tests). The existing UTF-8 code-page tests still pass, becauseatomic_writeencodes UTF-8 explicitly.Manual verification
N/A — unit coverage is sufficient: the change is a file-resolution contract that the tests drive directly. The Windows fallback (
open_file_no_reparseplus by-nameatomic_write) was checked against source only and was not run on Windows.Related Issues
Closes #18213
Refs #8725, #18090
Pattern harvest
Rule candidate: semgrep
Pattern:
p.read_text(...)followed byp.write_text(...)on the samePathin one function, which is a validate-then-rewrite by name.Decision for the owner
The decision is whether the import's cron sanitizer should resolve the staged
crons.jsononce and publish by atomic replace, as the issue asks. This draft does that and treats a staged store that is not one regular file as unreadable (replaced by an empty store) rather than following it, because no archive the extractor accepts can produce one.Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)Contribution License Agreement
Contributions are accepted under the project's Apache-2.0 license; see the Licensing section of CONTRIBUTING.md.