Skip to content

fix(import): read the staged cron store once and replace it, never write through it - #18240

Merged
buluoray merged 1 commit into
mainfrom
fix/import-cron-store-once-18213
Oct 9, 2026
Merged

buluoray merged 1 commit into
mainfrom
fix/import-cron-store-once-18213

Conversation

@buluoray

@buluoray buluoray commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: The settings import's cron sanitizer writes the sanitized store only to the staged crons.json it read, and never through a link or hardlink alias that appears at that name.

portability._sanitize_imported_crons read the staged crons.json with read_text() by name and rewrote it with write_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

  • The merge that consumes the sanitized store (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.
  • A directory at the crons.json name is still left alone, as it was before.
  • No change to the three sanitization rules (drop malformed, vet commands, pause executables).

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_crons pins the staged directory once (pinned_fs.pin_parent) where the platform supports it.
  • _read_staged_cron_store opens the store once with O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC relative to that descriptor (platform_compat.open_file_no_reparse where 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 follows snapshot_merge._read_cron_store_once in fix(restore): resolve merge core-file and cron-store names once #18090, which is not on main yet.
  • _publish_staged_cron_store does every rewrite with atomic_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.
  • The same staged directory's notification_settings.json was rewritten by name in _vet_archive_settings (the one sibling of this pattern in portability.py, raised by the First Principles lane). It now uses atomic_write, as the ui-prefs.json branch beside it already does.
  • docs/system-specs/modules/cli.md gets 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 TestTheCronSanitizerResolvesTheStoreOnce in test/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_import and test_a_missing_store_is_left_alone.
  • test_the_staged_notification_settings_are_replaced_not_written_through[symlink|hardlink] swaps the staged settings from inside parse_imported_settings; both cases fail when the by-name write_text is 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 own src (PYTHONPATH set and checked with kiro_crew.__file__):

  • rewrite by name again: 5 fail
  • drop the st_nlink check: 1 fails (hardlink at read time)
  • drop O_NOFOLLOW: 1 fails (symlink at read time)
  • drop O_NONBLOCK: the FIFO test hangs until timeout

test/test_portability.py and test/test_mcp_cron_security.py together: 707 passed, 5 skipped (with the notification-settings tests). The existing UTF-8 code-page tests still pass, because atomic_write encodes 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_reparse plus by-name atomic_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 by p.write_text(...) on the same Path in 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.json once 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

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributions are accepted under the project's Apache-2.0 license; see the Licensing section of CONTRIBUTING.md.

@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 8, 2026
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 2fea5a37141289cb19301f44763fd92dfe8ad3e7 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Advisory — does not block merge.

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of 2fea5a37141289cb19301f44763fd92dfe8ad3e7 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Check that Windows CI's existing portability import tests pass. They are the only run of the open_file_no_reparse + unpinned atomic_write fallback, which the author checked only against source.

[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 .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed 2fea5a37141289cb19301f44763fd92dfe8ad3e7 — this comment is updated in place on each push.

Review details

No findings.

FINDING — src/kiro_crew/portability.py:1200 — from kiro_crew.atomic_write import pinned_parent_replace_supported is imported inside _publish_staged_cron_store, but line 33 already imports atomic_write from that module at the top, so there is no circular import to justify it. This breaks the advisory top-level-imports rule. → Fix: add pinned_parent_replace_supported to the top-level from kiro_crew.atomic_write import ... line and delete the import inside the function.

[OPUS-REVIEWED] 2fea5a3

Verdict parsed from the review's SHA-scoped output markers for commit 2fea5a37141289cb19301f44763fd92dfe8ad3e7.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 2fea5a37141289cb19301f44763fd92dfe8ad3e7: <one-sentence reason>

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 2fea5a37141289cb19301f44763fd92dfe8ad3e7 — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: PASS

The Windows fallback (open_file_no_reparse plus a by-name atomic_write) was only read in source and never run on Windows, so confirm it there before merge.

What this change ships

Inventory (4 items) — 4 justified

Intent: make the settings import's cron sanitizer rewrite only the staged crons.json it judged, never a file reached through a planted link (FIX, Closes #18213).

  1. A sanitized cron store now replaces the staged crons.json entry atomically and never writes through a swapped-in link or hardlink. — justified
  2. A link, hardlink alias, FIFO or undecodable bytes at the staged crons.json at read time now count as an unreadable store and are replaced with an empty one. — justified
  3. The staged notification_settings.json rewrite is now an atomic replace instead of a by-name write_text. — justified
  4. cli.md describes how the cron sanitizer resolves the store, next to the import-staging paragraph. — justified

[FIRST-PRINCIPLES-REVIEWED] 2fea5a3

Description read

[DESCRIPTION-READ] 6f6fff2d04dcf88fecdc9f84bf197d97c7c08a32a0544fdb4e03dbbc78bb76cd

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 2fea5a37141289cb19301f44763fd92dfe8ad3e7 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 2fea5a3

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 2fea5a37141289cb19301f44763fd92dfe8ad3e7: <one-sentence reason>

@buluoray
buluoray force-pushed the fix/import-cron-store-once-18213 branch from d871a8f to 2fea5a3 Compare October 8, 2026 21:54
@buluoray

buluoray commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author
  • fixed in 2fea5a3: the notification_settings.json sibling at portability.py:1483

Confirmed: _vet_archive_settings read the staged file by name and rewrote it with write_text by name, in the same staged snap/ directory. It now uses atomic_write, like the ui-prefs.json branch next to it, with no mode argument to match its live writer. New test test_the_staged_notification_settings_are_replaced_not_written_through[symlink|hardlink]: both cases fail when the by-name write_text is put back, and pass with the fix. The read is still by name. A swap before the read only changes which bytes are parsed, and those bytes still go through parse_imported_settings. The write-through was the hazard. The branch is also rebased onto current main.

@buluoray
buluoray marked this pull request as ready for review October 8, 2026 23:51
@buluoray
buluoray requested review from a team and KiroCrewCaptain as code owners October 8, 2026 23:51
@buluoray
buluoray requested a review from CrysisDeu October 8, 2026 23:51
@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Oct 8, 2026

@CrysisDeu CrysisDeu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 other OSError) and nothing in _sanitize_imported_crons catches it, so the whole apply_import_zip aborts 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_supported is function-local while atomic_write from 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 the dir_fd is None branch (open_file_no_reparse(nonblocking=True) + by-name atomic_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; a monkeypatch.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.

@buluoray
buluoray enabled auto-merge (squash) October 9, 2026 05:27
@buluoray
buluoray merged commit 27fdac9 into main Oct 9, 2026
106 of 114 checks passed
@buluoray
buluoray deleted the fix/import-cron-store-once-18213 branch October 9, 2026 05:28
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 9, 2026
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.

fix(import): _sanitize_imported_crons reads and rewrites the staged crons.json by name

2 participants