Skip to content

fix(cloud): stage the launch source tarball in a launcher-owned directory - #13640

Merged
bolichen97 merged 2 commits into
mainfrom
fix/source-tarball-private-stage-13370
Sep 28, 2026
Merged

bolichen97 merged 2 commits into
mainfrom
fix/source-tarball-private-stage-13370

Conversation

@chenmingwei23

@chenmingwei23 chenmingwei23 commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A kirocrew cloud launch uploads the source tree the launcher actually packaged, or it fails — it never silently ships bytes someone else substituted.

kirocrew cloud launch packages the local checkout into a tarball and uploads it
with aws s3api put-object --body <path>. Passing a path means the AWS CLI
re-opens the file by name after the builder has written and closed it, so
there is a window in which the bytes on disk are not the bytes that were
packaged. Nothing downstream compares them.

All three tarball builders in src/kiro_crew/cloud/source.py left the file in the
stdlib temp root:

tempfile.NamedTemporaryFile(prefix="kirocrew-src-", suffix=".tar.gz", delete=False)

with no dir=. tempfile takes that root from the environment (TMPDIR /
TEMP / TMP), so where the tarball lands is decided by the launcher's
environment, and nothing guarantees the directory is owner-only or sticky. A
staging root another account can write makes the closed file swappable in that
window.

Every s3api put-object call site, with its verdict

Site How the body is passed Verdict
src/kiro_crew/cloud/source.py, upload_source --body str(tarball) BY NAME. Fixed here — staging moved, and the bytes are now pinned by checksum
src/kiro_crew/apps/builtins/aws_control/backend/storage.py, put_file --body local_path BY NAME. Deferred, see Other suggestions
src/kiro_crew/publish/personal_drive.py, _put --body path BY NAME. Deferred, see Other suggestions
src/kiro_crew/apps/builtins/aws_control/backend/storage.py, create_folder no --body at all N/A. Zero-byte folder marker, nothing to re-resolve
website/electron/scripts/sign-windows.js --body filePath BY NAME, out of scope. Release signing host, and a sha256 compare either side fails it closed
.github/workflows/publish-cli.yml, publish-installer.yml, publish-linux.yml, publish-windows.yml, sign-and-notarize.yml --body <file> BY NAME, out of scope. CI runners, not shipped product code

Why it matters

The launch tarball is the entire source tree the cloud instance runs. The box
downloads it with its own IAM role and executes what is inside. If the file the
CLI uploads is not the file the launcher built, the operator gets a running
KiroCrew instance whose code they never packaged, and nothing in the launch
output says so: the upload succeeds, the owner pin passes, the stack comes up.

The exposure needs no unusual privilege: only write access to whatever directory
the launcher's temp root points at.

Not a goal

  • Not a general S3 upload hardening. The two other by-name product sites
    (put_file, personal_drive.py::_put) are named in the table above and
    deferred onto the run_aws descriptor seam that open PR fix(aws-control): bind the backup archive's bytes to one descriptor from build to upload (#13259) #13272 is already
    building. This PR does not add a second descriptor parameter.
  • Not an exhaustive ancestor-trust model. The ancestor walk here refuses the
    cases it can name from ownership and mode. It does not enumerate POSIX ACL
    entries, Windows DACLs, mount options or immutable flags, and it is no longer
    the check that stops substitution — see the GPT disposition on ancestor
    substitution rights.
  • Not a sandbox visibility decision. Whether the cloud-src-staging leaf
    should be masked from in-sandbox agents the way aws-control-staging is
    touches security/paths.py and sandbox.py and needs a ruling on whether a
    sandboxed agent may launch at all. Left open and stated under Review
    dispositions.
  • Not a boto3 migration. boto3 has zero matches in install_requires (it
    is the optional voice-aws extra), so passing a file object instead of a path
    would be a new core dependency riding a security fix.
  • Not a change to the run_aws chokepoint. Every AWS call in the product
    goes through it, carrying the agent-session guard, the sandbox wrapper and the
    cgroup ceiling. This diff stays inside cloud/source.py.

What changed (symptom -> root cause -> change)

The symptom is "the CLI may upload bytes we did not package". Walking it back:

  1. The CLI re-opens the body by name, so the file must survive unchanged between
    our close() and its open().
  2. Every builder put that file in a shared, environment-chosen directory.
  3. A shared directory is exactly where another writer can replace a name.

That gives two independent changes, and they are not alternatives — the first
narrows who can reach the file, the second makes a swap detectable no matter who
reached it.

1. The bytes are pinned, and the pin is verified downstream of the CLI's open

Every check this process can run sits before the CLI opens the body, so any
of them can be defeated by acting after it returns. The one verifier that cannot
be is S3 itself:

"--checksum-sha256", staged.sha256,

S3 hashes what it receives and refuses the object when the digest does not match.
So a substituted or rewritten tarball ends the launch with a refused upload
instead of shipping.

For that to be sound the digest must describe the bytes we produced, not
bytes we read back. A digest computed by re-opening the finished file would only
move the window — from "our close until the CLI's open" to "our close until our
own re-open". So _write_tar_gz is now the single place a final tarball's bytes
are written, and it digests them on the way through:

fh.seek(0)
fh.truncate()
sink: Any = fh if digest is None else _HashingWriter(fh, digest)
with tarfile.open(fileobj=sink, mode="w:gz") as tar:
    fill(tar)
fh.flush()

fh is the handle its caller already holds on the temp it minted, not a fresh
open of that temp's pathname. The descriptor is the security boundary, not the
name. The staging leaf sits under the data home, which a same-uid process can
write, so between a mint that closes its handle and a write that resolves the
name again, the name can be rebound — to a link at a governance file such as
security_policy.json, or a hard link to it, which a check on the final
component does not catch. The whole source tarball would then land on that
target while the launch still reported success, and an absent or unparseable
ceiling resolves to the permissive default. mkstemp creates the temp with
O_CREAT|O_EXCL, so the handle names an inode nothing can substitute.

git archive writes through the same handle as its stdout rather than to
-o <path>, because a pathname handed to a child is a name that child opens
itself.

All three builders (_use_git_archive, _refilter_archive, _tar_fallback) go
through it and take the accumulator as a keyword argument, so a future writer
that bypasses it is a test failure rather than a silent hole.
build_source_tarball returns a StagedSource NamedTuple carrying the path and
the Base64 digest together, because the two must not be derived independently.

Two details are deliberate. The digest is Base64, not hex — that is what
--checksum-sha256 takes. And it is passed without --checksum-algorithm,
which asks the CLI to compute a checksum itself and would defeat the point of
supplying one.

2. The staging location narrows who can reach the file at all

  • New _staging_dir() resolves a cloud-src-staging leaf directly under the
    data home and checks the whole chain ROOT-FIRST, so each check runs only after
    everything above it is known good.
  • The chain is walked resolved, not lexically. config_dir() is the trust
    anchor atomic_write._link_trust_anchor already uses: at or above it a link is
    the operator's layout -- a symlinked $HOME, a data home on another disk -- and
    below it every directory is one Kiro Crew created itself. Resolving first is
    also what puts the real chain in scope, which a lexical walk cannot see:
    stat'ing a relocated home follows the link and reads the target's own sound
    0o700, so a world-writable directory holding that target never appears.
  • A link at the leaf still refuses the build, and is re-checked after
    mkdir(exist_ok=True), because exist_ok=True happily accepts a pre-existing
    link. The leaf is the one node this module creates, so a link there is never
    the operator's layout.
  • _first_replaceable() refuses when a directory in scope is unsafe, and its mode
    is only one of the two ways it can be. Ownership first: a directory's owner
    can replace what is inside it whatever the mode says, so a directory owned by
    neither this process nor root is unsafe at 0755 exactly as much as at 0777.
    Root is trusted because a root that wanted to substitute the tarball does not
    need a directory to do it. Then the write bits, with the sticky exemption
    they carry: under the sticky bit an entry may be renamed by the entry's own
    owner, by the directory's owner, or by root -- so sticky makes a directory
    safe only once its owner is already trusted, which is why the two tests are
    ordered and not alternatives.
  • Others-WRITABLE refuses on the bit alone, because "others" is every account by
    definition. GROUP-writable asks one more question first, because the bit cannot
    answer it: the same 0o775 means a foreign writer on a shared group and nobody
    at all on a user-private one. _group_shared_with_another_account() settles which
    from the MEMBERSHIP, and both halves are needed -- gr_mem lists supplementary
    members only, so a group shared by PRIMARY membership presents an empty gr_mem
    and would read as private. It FAILS CLOSED: a group whose privacy cannot be
    proven, including on a host whose passwd database will not enumerate, counts as
    shared. Sticky exempts as it does for others-writable, and the lookup is skipped
    when it does.
  • _chain_the_launcher_owns() says which directories are in scope. The data home
    and everything up to and including the operator's own home, when the home sits
    inside it. A directory above that belongs to the administrator, or in a
    container to a uid not mapped into this namespace -- on a sandboxed host even /
    reads as an unmapped owner (measured: /, /home, /local, /local/home all
    uid 65534). If one of those is hostile then every program the account runs is
    already substituted and a check on one tarball buys nothing, while refusing there
    would reject an ordinary container. A data home the operator moved outside
    their own account gets the whole chain instead, because that premise does not
    cover it: a relocated home is exactly the shared-host case where an ancestor can
    belong to a local peer.
  • The data home belongs to the operator, so it is verified, never rewritten.
    Only the leaf, which this module owns, is locked down.
  • _lock_to_owner() locks the leaf and refuses unless it actually is owner-only.
    Two steps, because neither stands alone:
    platform_compat.restrict_dir_to_owner is the owner-only lockdown for a
    directory on both platforms and is fail-loud by contract, so a mount that
    rejects the change stops the launch instead of warning and staging anyway; and
    applying a mode is not the same as having it, since a filesystem with a fixed
    permission mask (FAT, exFAT, a CIFS mount with a permissive dir_mode) accepts
    the call, keeps its own mode and reports success. So the mode is read back.
    Windows mode bits and uids carry no ACL information, so there the lockdown's own
    success is the guarantee and every mode and ownership read stands down.
  • Every builder removes its own half-written tarball when the build raises, and
    _use_git_archive and _tar_fallback do so on an INTERRUPT as well, which is
    not a fallback case and so propagates straight past the fallback path's own
    cleanup. This is part of the same change, not a spare fix: the data home is not
    cleared by a reboot the way a tmpfs temp root is, so a leak there is permanent,
    and every cancelled launch would otherwise leave a copy of the whole checkout.

What the walk is for now. It is no longer the gate — the checksum is. It
stays because it fails the launch early and actionably: an operator whose data
home sits under a directory a peer can write gets a message naming
chmod go-w <dir>, rather than an opaque S3 digest mismatch after the upload
attempt. Defence in depth, with the diagnosis in the cheap layer.

Backwards compatibility

Breaking, narrowly, and only for one host shape. config_dir()'s
mkdir(parents=True) leaves ~/.kiro at 0o775 under umask 002. On a host with
umask 002 and a shared primary group, that directory is now refused where it
previously passed.

That refusal is ACTIONABLE, which is what separates it from the one this PR
removes: the ownership test has already established the node belongs to this
account or root, so the message names chmod go-w <dir> and one command clears
it. A user-private group on the same host is still accepted and needs nothing —
that is the case the earlier 0o022 form got wrong, and the membership lookup is
what tells the two apart.

Writer sweep: _staging_dir is new in this PR and has no callers outside it.
build_source_tarball's return type changed from Path to the StagedSource
NamedTuple; every caller is in this repository and in this diff, and the sibling
suites that exercise them (test_cloud_cli.py, test_cloud_ec2.py,
test_launch_engine_ref.py) pass unchanged.

--checksum-sha256 needs a reasonably modern AWS CLI. This package pins no CLI
version, though --expected-bucket-owner on the same call already assumes one, so
the floor moves little if at all. I did not measure the exact minimum.

Tests

TestTarballStagingDirectory in test/test_cloud_source.py. Each staging case
pins the data home and the process temp root at two different directories, so
a builder honouring the temp root and one resolving the data home cannot both
pass.

The checksum pins, which are the load-bearing ones:

  • the digest each of the two writing paths reports matches the bytes on disk,
    computed independently
  • the digest is Base64, not hex — a hex digest is what a plausible
    implementation produces and S3 would reject it
  • a tarball rewritten in place after staging no longer matches its pin
  • the writer resolves the staged name zero times — it writes through the
    handle the caller already holds — and reading the file back is never attempted.
    This is the only way the no-window property can be pinned: a read-back yields
    the same digest on a quiet host, so no value comparison can tell a sound
    implementation from an unsound one — only the absent name resolution can
  • a link planted at the staged name in the window between the mint and the
    write takes no tarball bytes: the file it points at is byte-identical
    afterwards. Mutating the writer to resolve the name again fails this pin, so it
    discriminates rather than passing vacuously
  • every tarball writer goes through the hashing writer, so a fourth producer
    cannot bypass it silently

The staging pins:

  • each of the three builders stages under the data home, and the temp root is
    left completely empty (the git-archive case captures both names it creates,
    since _refilter_archive stages a second file)
  • the staging directory is 0o700, and a pre-existing leaf left 0o755 is
    re-restricted
  • a lockdown that raises refuses the build; one that succeeds but silently
    does not take
    also refuses (the fixed-permission-mask case)
  • a link at the leaf refuses the build, while a link at the data home and a link
    at an ancestor above the home are followed -- the default ~/.kiro/crew is
    lexical, so refusing those would refuse any host whose $HOME is itself a
    symlink
  • a regular file at the leaf refuses the build as an AWSError, not a bare
    FileExistsError: mkdir(exist_ok=True) forgives an existing directory and
    nothing else, and cli_cloud catches only AWSError, ValidationError and
    CloudActionDenied, so an unguarded mkdir reaches the operator as a traceback
    naming no remedy. Restoring the unguarded call fails this pin, so it
    discriminates rather than passing vacuously
  • a world-writable parent of a relocated home refuses, which a lexical walk
    cannot reach at all: that is the pin proving resolving widened coverage rather
    than narrowing it
  • a sticky world-writable ancestor this account owns is accepted (the /tmp
    shape), so the check does not reject every ordinary host
  • a foreign-owned directory is refused whatever its mode, and sticky does
    not exempt one -- that case makes exactly one node foreign, because a blanket
    euid change would let a sticky-first short-circuit pass on a later node's refusal
  • a group-writable ancestor is accepted when the group is private and
    refused when it is shared; sticky exempts one without asking the group at
    all; privacy fails closed on a host that will not enumerate; and both
    halves of the membership lookup are load-bearing, pinned separately because on
    a shared-group host both fire at once and either alone would look sufficient
  • a world-readable but unwritable 0o755 home is accepted, which is what a plain
    mkdir leaves under umask 022
  • the scope branch is pinned on _chain_the_launcher_owns directly: a home inside
    the account starts the walk at that home and excludes the filesystem root, a home
    relocated outside it starts at the root
  • the data home is verified and not rewritten: the mode the launch found is
    still there after the refusal
  • a build that raises mid-write, and a git archive that is interrupted, each
    leave no tarball behind

The link cases use conftest.make_dir_link, not symlink_to: a directory
symlink needs SeCreateSymbolicLinkPrivilege, which an unelevated Windows shell
lacks, so a symlink would skip on exactly the host whose junction branch of
is_link_or_junction the leaf refusal depends on.

python -m pytest -n0 test/test_cloud_source.py -q          -> 84 passed
python -m pytest -n0 test/test_link_screen_hold_pin.py -q  -> 17 passed
every test file referencing the changed module             -> 346 passed

The sibling files run because build_source_tarball's return type changed:
test_cloud_cli.py, test_cloud_ec2.py, test_launch_engine_ref.py. None broke.

The third file in this diff is one generated line. test/test_link_screen_hold_pin.py
landed on main after this branch was cut, and it enumerates functions that screen a
path for links and then resolve or read the same name. _staging_dir is such a
function and is new here, so that gate had no way to know it:
test_every_site_is_declared reddened on both python and Windows naming
cloud/source.py::_staging_dir. Per the gate's own split, a site that refuses every
link it screens takes a baseline entry and not a HELD_SITES hold -- _staging_dir
raises on a linked leaf rather than vetting one and continuing -- so the remedy is a
declaration. Recorded with test/gen_link_screen_sites.py, never hand-edited, which
is that file's own requirement.

Mutation-verified, restoring the pre-fix form at every site that maintains
the state, each from a pristine copy outside the repository and each restored to a
byte-identical sha256. Each mutation was confirmed to redden its own pin by
node id, not merely to produce a non-zero exit:

Mutation Result
remove the digest tee from _write_tar_gz both digest-matches pins fail
compute the digest by re-reading the staged file test_the_digest_never_re_reads_the_staged_file fails
drop --checksum-sha256 from the upload test_upload_source fails
add --checksum-algorithm alongside it test_upload_source fails
report the digest as hex instead of Base64 test_digest_is_base64_not_hex fails
let a third writer bypass the shared writer test_every_tarball_writer_goes_through_the_hashing_writer fails
remove dir= at all 3 builder sites 4 failed
remove the failed-build cleanup 1 failed
remove the leaf link + resolve guards 1 failed
restore the fail-soft chmod_safe form 3 failed
drop the leaf mode read-back 1 failed
drop the leaf lockdown entirely 5 failed
drop the linked-leaf guard 1 failed
make the chain walk lexical (base.resolve() -> base) 2 failed
drop the replaceable-chain guard 2 failed
drop the sticky exemption 1 failed
make the chain test read bits (0o077) instead of write bits (0o022) 12 failed
drop the ownership test 2 failed
let sticky short-circuit before ownership is judged 1 failed
make the scope always the full chain 16 failed
make the scope always trimmed to the account home 1 failed
drop the group branch from the mode test (back to S_IWOTH only) 1 failed
fail OPEN when passwd will not enumerate 1 failed
drop the supplementary (gr_mem) half of the membership check 1 failed
drop the primary-gid half of the membership check 1 failed
drop _use_git_archive's interrupt cleanup arm 1 failed
let the fallback reuse the failed archive attempt's digest both per-attempt pins fail
hand the fallback a fresh digest object carrying the failed attempt's state (.copy()) both per-attempt pins fail
read the archive attempt's digest for the fallback's StagedSource both per-attempt pins fail

Two harness defects are worth naming, because both produced a green-looking
result that proved nothing. The sticky-ordering mutation did not redden at
first: a blanket euid change made every node foreign and a later node refused
anyway, so the pin was rewritten to make the sticky node the only problem. And
the checksum harness's first run scored six of six CAUGHT while naming no pin —
-q suppresses the failure summary and ANSI codes broke the parse — so it had
only established that something failed. Re-run with -rf --color=no.

Two cases also had to stop reading the operator's real ~. They asserted against
an unisolated Path.home(), which passes on a host whose chain happens to hold a
foreign-uid directory and fails on a runner whose / and /home are both
root-owned, so each now pins Path.home() under tmp_path and CONSTRUCTS the
foreign-owned ancestor rather than probing for one. That is a defect in the tests,
not a behaviour change: the scope bound they cover is unchanged, and the
full-chain mutation still reddens both.

Also green on the changed files: black --check, isort --check-only, flake8,
mypy, the comment-history gate, and semgrep scan --config p/python --config p/security-audit at 0 findings (the deliberate loose-mode test fixtures carry
nosemgrep with a stated reason; removing one takes the scan back to 1 blocking
finding, which is how the suppressions were checked to be doing only that).

Manual verification

End-to-end on a host that is the affected layout (/home/u -> /local/home/u),
which is the layout bolichen97's finding named:

source.py under test = <worktree>/src/kiro_crew/cloud/source.py
HOME is a link       = True
RESULT: BUILT        = <data home>/.kiro/crew/cloud-src-staging/kirocrew-src-*.tar.gz
under data home      = True
checksum pinned      = <Base64 sha256>

The reported digest was then recomputed independently with
openssl dgst -sha256 -binary <file> | base64 and is identical.

One correction to method, since an earlier run of this repro was cited in a PR
comment. kiro_crew is installed editable against a different checkout on
this host, so a script with no sys.path pin imported that tree rather than this
branch, and its verdict described whichever branch that tree was on. The script
now pins sys.path and refuses to run against any other tree; the output above is
from the re-run, and the conclusion is unchanged. The test evidence was never
affected — setup.cfg sets pythonpath = src, so pytest loads this worktree.

Any other suggestions on the work

The other two by-name product sites are deferred on purpose, not overlooked.
put_file and personal_drive.py::_put both need a descriptor parameter on the
same engine.run_aws that open PR #13272 is extending with stdin_fd. Doing
them now would mean a second descriptor parameter and a second verified-open
helper on a function that is already growing one, so both halves should follow on
#13272's seam once it merges. That is sequencing, not a dead end. Either of them
could take the checksum pin from this PR instead, which needs no seam at all.

A refused upload currently reads as an opaque CLI error. When S3 rejects the
digest, what the operator sees is whatever aws s3api prints, not a sentence
naming substitution as the cause. Mapping that into a launch-level message is
worth doing and is deliberately not in this diff.

The issue's own premise is corrected on the issue. #13370 states that
put_file already avoids the by-name re-resolution and that two other sites do
not. On current main put_file passes --body by name as well, and no
descriptor seam (body_fd, stdin_fd, a verified-open helper) exists anywhere
under src/kiro_crew/. The full six-site table above is posted as a comment on
the issue so the record is right.

sign-windows.js and the publish-* / sign-and-notarize workflows are left
alone.
They run on the release signing host and on CI runners, not in shipped
product code, and the signing path compares a sha256 either side of the upload,
which fails it closed.

Pattern harvest

Rule candidate: semgrep
Pattern: a file staged with tempfile.NamedTemporaryFile(...) or
tempfile.mkstemp(...) with no dir=, whose path is then handed to a subprocess
as an argument.

That shape is the whole defect class here, and it is mechanically detectable: the
staging root comes from the environment, and passing the path means an external
process re-opens the name after the writer closed it. The rule would want a
matching allowance for a path that never leaves the process. All three builders in
this file matched it, and the two deferred product sites in the table above match
it too, so the rule has live subjects the moment it lands.

Review dispositions

GPT 5.6, BLOCKING (fenced), a non-directory staging entry crashes the CLI --
TAKEN.
_staging_dir refused a link at cloud-src-staging but let a plain
regular file fall through to staging.mkdir(exist_ok=True), which forgives an
existing directory and nothing else. cli_cloud catches AWSError,
ValidationError and CloudActionDenied only, so the FileExistsError reached
the operator as a traceback naming no remedy. The mkdir is now guarded and
raises the module's own staging refusal, which names the path and the fix. This is
the other half of the leaf refusal added in the same function, not a new site.

Fixed rather than overridden. The adjudication is right that the required state is
out-of-band and the outcome is a loud fail-closed crash with nothing staged or
uploaded, so the fence's own class does not fit -- but the fix is four lines and
one pin, and an override would have left a real traceback on a supported path
behind a correct-looking clearance.
test_a_regular_file_at_the_staging_leaf_is_refused_as_an_awserror asserts the
exception type, which is the whole contract; restoring the unguarded mkdir
fails it with the exact FileExistsError the finding named.

GPT 5.6, BLOCKING, failed archive attempts contaminate the fallback checksum --
TAKEN, and it was a real defect introduced by this PR's own checksum work.

build_source_tarball minted ONE hashlib.sha256() and handed that same
accumulator to each tarball attempt in turn. _use_git_archive deliberately
SWALLOWS a mid-write _refilter_archive failure so it can fall through to the
tarfile fallback, and by then the gzip header has already passed through
_HashingWriter. The fallback's checksum therefore covered bytes that were not in
the file it named, and S3 would have refused a perfectly good tarball. Each
attempt now owns its accumulator, and no digest is read except for the attempt
that filled it.

This finding was machine-fenced as security-class with a pre-drafted override
rationale. It was fixed rather than overridden, and the fence is not the reason:
the mechanism is reachable on a supported path, and the adjudication that
accompanied it put the real fix cost at "small". An override would have left a
live defect behind a correct-looking clearance. The adjudication is also right on
the narrower point that this never weakened the substitution pin -- a digest
carrying extra bytes can only make S3 refuse, never match a substituted file --
so the harm was a failed launch, not a shipped one. That is what made it a
correctness bug rather than a hole, and it is fixed on those terms.

Two pins cover it, because one alone would not.
test_each_tarball_attempt_gets_its_own_digest states the invariant as object
identity, so it holds however a future producer fails.
test_a_failed_archive_attempt_does_not_contaminate_the_fallback_digest runs the
real _use_git_archive and the real _refilter_archive, stubbing only
git archive itself and stubbing it to succeed with a corrupt archive -- which
is what makes the re-filter raise after it has begun writing. That the mid-write
failure genuinely dirties the digest is not asserted by inspection: the mutation
restoring the shared accumulator reddens that pin, which it could only do if bytes
had reached the digest.

GPT 5.6, BLOCKING, ancestor validation misses real substitution rights
(symlink-side components, POSIX ACL writers, Windows FILE_DELETE_CHILD) --
ANSWERED, by removing the ancestor walk from the load-bearing path rather than
extending it.
The finding is right that the walk does not see those classes, and
right that it could be extended to see two of them on POSIX. It was not extended,
because the enumeration has no natural end: three accepted rounds on this one
function each closed a class and each drew the next one (0o022 refused a
supported host; S_IWOTH alone left shared groups; membership left ACLs and
DACLs). The question the walk was trying to answer -- can anyone swap this
directory before the upload reads it -- is now answered downstream instead, by
--checksum-sha256, which S3 verifies after the CLI's open and which no ancestor
right can get in front of. Every class the finding names, including the Windows
one, produces a refused upload rather than a substituted one.

A file-descriptor hold was investigated first and rejected on measurement, not
preference: no filesystem path names an inherited Windows HANDLE, so --body
cannot reference one, and Windows is a supported launcher platform. A POSIX-only
fd hold would have retired two of this finding's three classes and left the
Windows one, which is the class it names explicitly. An fd is also strictly
weaker against an in-place rewrite -- it pins the inode, and the rewrite changes
that inode's contents -- where the checksum refuses it.

GPT 5.6, BLOCKING, group-writable ancestors still permit tarball substitution --
TAKEN, and it was a real hole in the residual argument.
Narrowing the mode test to
S_IWOTH left the case a shared GROUP can write, and the reasoning offered for
following a link at or above the data home leaned on exactly that gap: it said a
parent another account can write is refused by the ownership and mode tests, and a
shared-group parent is writable by another account and was not refused. Both
reviewers were right and the mode bit cannot serve both of them, because 0o775
means a foreign writer on a shared group and nobody at all on a private one. The
membership settles it instead, which is what this finding asked for ("unless their
group is proven private") and what the walk's own docstring had already named as the
only thing that could. Measured obtainable rather than assumed: on a corporate host
pwd.getpwall() enumerated and reported 23 accounts sharing the primary gid, and
the helper fails closed where it does not enumerate.

That round also found a mislabelled pin, unprompted:
test_a_group_writable_ancestor_is_accepted claimed to represent "a group holding
only the operator" while inheriting tmp_path's gid, which on this host is shared
by 23 accounts. It asserted the permissive behaviour unconditionally and its
verdict depended on the test host. It now states the membership explicitly and is
host-independent.

bolichen97, CHANGES REQUESTED, the guard refuses a default data home reached
through a symlinked $HOME -- TAKEN, and the cause was mine.
Verified live on a
host that is the affected layout (/home/u -> /local/home/u): pre-fix
build_source_tarball() returned REFUSED -- an ancestor of the data home is a link, and the same launch succeeds on origin/main, so this PR introduced the
refusal rather than exposing an older one. The two blanket refusals were removed
and the walk now resolves at config_dir(). They bought nothing:
_chain_the_launcher_owns already opened with resolved = base.resolve(), so the
ownership and mode tests were never reading the lexical chain -- the refusals
propped up only that function's own docstring sentence, which is corrected in the
same commit.

GPT 5.6, BLOCKING, Windows stands the ancestor walk down entirely -- NOT taken
as asked, and now moot.
The finding is right about the mechanism:
_first_replaceable returns None on Windows. Its remedy is to enumerate every
ancestor's owner plus its substitution-capable ACL rights across SIDs and group
memberships, and platform_compat has no reader for a directory's owner or DACL
at all, so that is a new Windows-only mechanism -- which the finding's own
adjudication calls "genuinely large". This repository's own blocking
Cross-Platform Portability gate also asks for precisely the platform stand-down
the finding wants removed. The checksum pin closes the gap the finding is about
on both platforms without it.

Opus 5, BLOCKING, two new cases assert against the operator's real
Path.home() -- FIXED.
Correct and reproduced. _pin_home_and_temp_root pins
the data home under tmp_path while Path.home() stays the runner's real home,
so _chain_the_launcher_owns takes its whole-chain branch and chain[0] is the
filesystem root; the sibling case finds no foreign-uid ancestor at all on a runner
whose / and /home are both root-owned. Both now pin Path.home() under
tmp_path and construct the foreign ancestor.

Opus 5, if mode & 0o022 also refuses a GROUP-writable ancestor -- FIXED, then
SUPERSEDED.
This one refused a supported host: config_dir()'s
mkdir(parents=True) leaves ~/.kiro at 0o775 under umask 002 and only
~/.kiro/crew is tightened, so every cloud launch on a user-private-group host
aborted with "is writable by other accounts" for a group containing only the
operator. The mode test was narrowed to S_IWOTH only, which was too wide; the
group bit is a refusal again, decided on membership rather than on the bit, so the
host this finding protected is still accepted. The residual trade is stated under
Backwards compatibility rather than left implicit.

Opus 5, _use_git_archive cleans up only (OSError, SubprocessError, TarError)
-- FIXED.
An interrupt during the 120s git archive propagated past the
fallback path's own cleanup and left a full source tarball in the data home, which
no reboot clears, so repeated cancelled launches accumulated copies of the whole
checkout. It now carries the same BaseException arm _tar_fallback has, with its
own pin.

Opus 5, the cloud-src-staging leaf is visible while the identical-shape
aws-control-staging is hidden -- DEFERRED, the same open question as Design's
below.
Opus names the exact constants (sandbox._CREW_HIDDEN_LEAVES,
_CREW_PRECREATE_HIDDEN_DIR_LEAVES, security.paths._CREW_SECRET_LEAVES) and a
closer precedent than the one recorded below, and marks it advisory because those
files are untouched here. Two independent reviewers now name the same residual. It
is the single disposition this PR does not settle, for the reason under Design's
entry.

GPT 5.6, BLOCKING, foreign-owned ancestors remain replaceable -- FIXED, and the
finding caught a genuine gap in this PR's own stated premise.
The sticky
exemption was justified here as "only an entry's own owner may rename it", which is
incomplete: under the sticky bit a directory's own owner may rename entries
inside it too, so a foreign-owned 1777 ancestor reached the same swap. And a
foreign-owned 0755 ancestor never met an ownership test at all. Both are now
closed by the ownership predicate, ordered before the write-bit test.

One measured limit on the finding's literal remedy, disclosed rather than
skipped. It asks to "reject ancestors owned by a non-root UID other than the
launcher". Applied to the full chain that refuses every containerised host: inside
this repository's own agent sandbox, /, /home, /local and /local/home are
all owned by uid 65534, the unmapped owner, so a literal full-chain ownership
test refuses before reaching anything the operator could fix. The scope bound above
is the reconciliation -- the ownership test applies to the chain the launcher is
answerable for, and a data home relocated outside the account still gets the whole
chain, which is the shared-host case the finding describes.

GPT 5.6, BLOCKING, Windows skips the link/junction invariant (earlier head) --
FIXED as named.
The three link cases use conftest.make_dir_link, which creates
a junction on Windows and needs no privilege. This also mattered beyond the skip:
is_link_or_junction short-circuits on os.path.islink, so its junction branch
was exercised on no host at all.

GPT 5.6, BLOCKING, a replaceable data-home parent (earlier head) -- FIXED.
Nothing above the data home was mode- or link-checked, so a home under a shared
writable parent could be replaced wholesale after _staging_dir() returned. Closed
by the chain walk over the resolved chain, and now also by the checksum pin. Its
suggested remedy was "upload through a pinned descriptor instead of reopening the
staged pathname"; that is the fd route measured and rejected above, and the
checksum reaches the same goal on both platforms.

GPT 5.6, BLOCKING, _staging_dir permission failure leaves staging swappable
(earlier head) -- FIXED.
chmod_safe swallows every OSError with a warning, so
a mount that refused the mode change left the leaf at whatever the umask gave
mkdir while the build carried on. It is now restrict_dir_to_owner (fail-loud)
plus a mode read-back.

First Principles, item 3, the IS_POSIX branch respells
restrict_dir_to_owner -- SUBTRACTION TAKEN.
The branch is gone and the
existing helper is called unbranched, which also removed the POSIX fail-soft /
Windows fail-loud asymmetry the review named.

Design, _lock_to_owner(base, ...) silently chmods the operator's data home --
SUGGESTION TAKEN.
The home is verified and never rewritten, and a pin asserts the
mode the launch found is still there afterwards. Narrowing the mode test to
others-writable came out of the same review: the swap needs the write bit, so a
0o755 home -- what a plain mkdir leaves under umask 022 -- is accepted rather
than refused.

Design, the new cloud-src-staging leaf has no stated sandbox disposition --
DISPOSITION OPEN, and the review's premise checks out.
The leaf is VISIBLE: it
is not in security.sensitive_home_dirs(), so an in-sandbox agent can write it,
resting on the tool gate alone. The temp root it replaces is masked per session
(sandbox.py:360, masked "as a WHOLE so one session tree cannot open another's"),
so this trade is real rather than theoretical -- it closes the cross-account swap
and opens an in-sandbox one for a launcher packaging a checkout the agent cannot
reach. The owner-only lock and the chain rule are filesystem-permission
boundaries and do not substitute for a mask against a same-UID agent.

The repository already has the exact shape for the remedy:
_MD_NOTEBOOK_STAGING_LEAF is a staging directory masked as a whole directory,
with its owning writer getting it back through extra_visible_dirs because the
mask alone would break that app. Applying it here means touching
security/paths.py, sandbox.py and the leaf pin test, and it needs a decision
on whether a sandboxed agent may run kirocrew cloud launch at all -- if it may,
the mask needs the same extra_visible_dirs route. That is wider than this PR's
scope, so it is called out here rather than decided quietly inside it.

CI notes

Backend Tests (Windows) (2) failed on the first head and is base-owned, not
in this diff
. The failing case is
test/test_subagent_reap_attribution.py::test_a_user_stop_is_reported_as_a_stop_not_a_runtime_death
and its assertion is an asyncio teardown warning ("Task was destroyed but it is
pending!"). This branch does not touch that file, and the test arrived in commit
4d4e184d11, which is already an ancestor of this branch's base. CI evaluates the
merge ref, so the branch inherits it. It has not recurred on a later head.

Backend Tests (Windows) (3) carried
test_history_consolidation_retry.py::TestRotationReleasesTheBudgetForNewContent::test_unstamped_accounting_keeps_the_cap
on one head and went green on the next with no change touching history
consolidation. Its failure is PermissionError: [WinError 5] inside
atomic_write.replace_with_retry, whose retry is bypassed on a running event
loop; #14212 is open on that exact mechanism.

Several review lanes also failed on API rate limit exceeded for installation,
which is the GitHub App budget rather than anything in the diff. Those runs
published no marker naming the head they ran on.

Related Issues

Refs #13370

@chenmingwei23
chenmingwei23 requested review from a team and patrigao September 25, 2026 02:18
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Sep 25, 2026
@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5) — 🟡 CONCERNS

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

Design-Verdict: CONCERNS

Unmeasured AWS CLI floor for --checksum-sha256 and the new staging leaf's undecided sandbox disposition are the residuals riding an otherwise sound checksum-gated design.

Watch

  • The PR states "--checksum-sha256 needs a reasonably modern AWS CLI… I did not measure the exact minimum." An operator on an older CLI gets an argument-parse failure on every launch — loud and remediable by upgrading, but currently an unstated floor for a shipped feature.
    Clears when: the minimum CLI version is measured and documented (or probed with a clear error naming the upgrade).
  • The fail-closed group-privacy lookup refuses when passwd will not enumerate, which is the default on directory-backed hosts (SSSD, enumerate=false). Combined with the disclosed umask-002 break, a domain-joined Linux fleet with group-writable ~/.kiro gets every launch refused until chmod go-w — actionable, but potentially fleet-wide.
    Clears when: this host shape is confirmed acceptable by a maintainer, or the unprovable-privacy case warns instead of refuses (the checksum remains the gate either way).
  • cloud-src-staging is VISIBLE to in-sandbox agents where the temp root it replaces was session-masked; the PR defers the mask decision. A same-uid agent can now read the packaged checkout and DoS a launch via digest mismatch, though it can no longer substitute silently.
    Clears when: a follow-up settles the leaf's sandbox disposition (mask + extra_visible_dirs, per the _MD_NOTEBOOK_STAGING_LEAF precedent) or a maintainer records VISIBLE as accepted.

[DESIGN-REVIEWED] a970a3e

@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5) — 🟡 CONCERNS

Premise-level review of a970a3e1da41e4847c46cd61e3b993a66e1d256d — 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: CONCERNS

The ancestor walk (_first_replaceable + group enumeration) survives the checksum that, by the author's own words, "is no longer the check that stops substitution."

Not justified as shipped

  • Item 3 — oversized: the description concedes the pin is what stops substitution ("no matter who reached it"), leaving the walk's residual value at converting an S3-refused upload into an earlier message — bought with ~140 lines, two host-database enumerations per launch (grp.getgrgid/pwd.getpwall, deliberately fail-closed on hosts that won't enumerate), and a new refusal surface the staging move + leaf lockdown don't need. The repo's three sibling walks (github_runner.py:234, dashboard/handlers/terminal.py:881, browser_cli/install.py:407) all decide from ownership+mode alone; none reads the group databases.
    Subtraction: delete _chain_the_launcher_owns, _first_replaceable, _group_shared_with_another_account and their tests; keep the staging move, leaf lockdown, descriptor writes, and checksum.
    Clears when: the deletion is made, or a concrete case is named that the checksum refusal does not already fail closed on.
  • Item 7 — one consumer, generalized: digest: Optional = None on all three builders and _write_tar_gz; all 4 production call sites pass a non-None digest (grepped digest= in source.py), so the None branch has zero production consumers.
    Subtraction: make digest required; drop the sink = fh if digest is None branch.
    Clears when: the branch is removed or a production None caller exists.

What this change ships

Inventory (8 items) — 6 justified

Intent: make cloud launch upload exactly the bytes the launcher packaged or fail — a FIX (TOCTOU between the builder's close and the AWS CLI's re-open of --body by name).

  1. Tarballs staged in an owner-only cloud-src-staging leaf under the data home, not $TMPDIR — justified
  2. Upload carries a write-time --checksum-sha256; a substituted tarball is a refused upload — justified
  3. Launch now refuses when any directory from the operator's home down is foreign-owned, loose-writable, or its group unprovably private — oversized (the checksum already fails the swap closed; see above)
  4. git archive writes through the held descriptor via stdout instead of -o <path> — justified
  5. Failed or interrupted builds delete their half-written tarball from the persistent data home — justified
  6. build_source_tarball returns path+digest together (StagedSource) — justified
  7. digest parameter optional-None on every builder — one consumer, generalized (all 4 real callers pass one)
  8. Cleanup of the unfiltered archive moved from _refilter_archive to its caller, leak still pinned — justified

[FIRST-PRINCIPLES-REVIEWED] a970a3e

@chenmingwei23
chenmingwei23 force-pushed the fix/source-tarball-private-stage-13370 branch from a08642f to f8649ed Compare September 25, 2026 02:39
@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review — ✅ no blocking findings

GPT 5.6 completed its review of a970a3e1da41e4847c46cd61e3b993a66e1d256d and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] a970a3e

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

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 25, 2026
@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5 Review — ✅ no blocking findings

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

Review details

Nothing blocks: the staging relocation, the descriptor-only write path and the build-time checksum pin all hold under falsification; three advisory issues remain.

FINDING — src/kiro_crew/cloud/source.py:352 — every cleanup for the relocated staging temps (_discard_staged in the except BaseException arms, finally: staged.path.unlink()) is a Python-level handler, so a cloud launch killed by SIGTERM/SIGKILL/OOM mid-build leaves a complete kirocrew-src-*.tar.gz copy of the checkout in config_dir() / "cloud-src-staging" — a directory nothing in the repo ever sweeps (cloud-src-staging appears only in source.py and its tests), unlike the stdlib temp root it replaced → Fix: unlink pre-existing kirocrew-src-*.tar.gz entries in _staging_dir() after the lockdown and before returning, since the docstring already asserts "nothing else picks names inside it".

FINDING — src/kiro_crew/cloud/source.py:414 — the single refusal message appends "Drop the write bit ('chmod go-w {node}')" to all four causes from _first_replaceable, but for "is owned by another account (uid ...)" the named command cannot succeed (chmod requires owner or root, and the refusal condition is that the node is neither) and for "cannot be read, so it cannot be cleared" the write bit is not what was measured → Fix: return the remedy as a third element of _first_replaceable's tuple so the ownership and unreadable causes emit only the "move the data home" clause.

FINDING — docs/system-specs/modules/cloud.md:620 — the "Secret hygiene in source shipping" section is the owning spec for build_source_tarball (it names the function) and cloud.md also documents the s3api put-object argv, yet the PR adds a new data-home leaf (cloud-src-staging), five new cloud launch refusal paths, the --checksum-sha256 pin and a Path → StagedSource return-type change without touching it, leaving the spec silent on the launch's new failure modes → Fix: extend that section in this commit with the staging location, the ancestor-safety refusals and their remedies, and the checksum pin (AGENTS.md: update the owning spec in the SAME commit).

[OPUS-REVIEWED] a970a3e

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

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

@chenmingwei23
chenmingwei23 force-pushed the fix/source-tarball-private-stage-13370 branch from f8649ed to 6f3402b Compare September 25, 2026 03:00
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 25, 2026
@chenmingwei23
chenmingwei23 force-pushed the fix/source-tarball-private-stage-13370 branch from 6f3402b to a20b4fd Compare September 25, 2026 04:07
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 25, 2026
@chenmingwei23
chenmingwei23 force-pushed the fix/source-tarball-private-stage-13370 branch from a20b4fd to 98b14ae Compare September 25, 2026 04:44
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 25, 2026
@chenmingwei23
chenmingwei23 force-pushed the fix/source-tarball-private-stage-13370 branch from 98b14ae to 5e55500 Compare September 25, 2026 06:08
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention labels Sep 27, 2026
@chenmingwei23

Copy link
Copy Markdown
Contributor Author

@bolichen97 the symlinked-$HOME refusal you found is fixed, and all four steps of your "smallest fix" are in the branch. Your review was recorded against 5e555004bf, an earlier head, so the line numbers have moved.

Head: d04f248a6b59e2c017bb34a36ba29cccba15820a

Your step Where it landed
1. Treat a link at or above the data home as the operator's own layout _staging_dir() follows it and says so, drawing the same anchor split as atomic_write._link_trust_anchor
2. Resolve base once and run _first_replaceable on the resolved chain _chain_the_launcher_owns (src/kiro_crew/cloud/source.py:164) resolves base and places the account boundary at the resolved home; _first_replaceable (:246) walks that chain
3. Refuse a link only at the cloud-src-staging leaf src/kiro_crew/cloud/source.py:418-419 before the mkdir, and again at :431 after it, because exist_ok=True accepts a pre-existing link
4. Change the test so a linked ancestor is accepted test_a_linked_ancestor_above_the_data_home_is_followed (test/test_cloud_source.py:524)

Two details worth flagging, both agreeing with your reading:

Resolving widened coverage rather than narrowing it. A world-writable parent of a relocated home is refused now, which the lexical walk could not reach at all. There is a pin for exactly that, and another accepting a sticky world-writable ancestor this account owns, so the check does not reject an ordinary host.

The error message you called unactionable now names the path and the command that fixes it, since the data home is the operator's own setting.

This head also carries a separate fix for a finding on the staging write path: every staged tarball is written through the descriptor its minter holds, so a name rebound between the mint and the write cannot redirect tarball bytes onto a governance file. git archive streams through that handle rather than taking an -o pathname.

test/test_cloud_source.py is 83 passed locally, single process.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Sep 27, 2026
@chenmingwei23
chenmingwei23 force-pushed the fix/source-tarball-private-stage-13370 branch from d04f248 to 8b99ae7 Compare September 27, 2026 16:15
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 27, 2026
@chenmingwei23
chenmingwei23 force-pushed the fix/source-tarball-private-stage-13370 branch from 8b99ae7 to fb87189 Compare September 27, 2026 17:35
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Sep 27, 2026
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 27, 2026
chenmingwei23 and others added 2 commits September 27, 2026 21:56
…tory

The tarball is handed to `s3api put-object` as `--body <path>`, so the AWS CLI re-opens it by name after the builder has closed it. All three builders left it in the stdlib temp root, whose location tempfile takes from TMPDIR/TEMP/TMP with no guarantee it is owner-only or sticky -- a staging root another account can write makes the closed file swappable before the CLI opens it, and the launch would ship a tree that was never packaged.

Resolve the staging directory from the data home instead (0700, re-checked after mkdir) and pass it as `dir=` at every builder. The body still passes by name; no descriptor seam and no new shared helper. A failed fallback build now removes its own half-written tarball, which matters because the data home is not cleared by a reboot.

A link at or above the data home is followed, not refused. That is where this module's trust anchor sits -- the same split atomic_write._link_trust_anchor draws and workflows.md records for the data home -- so a symlinked $HOME, a dotfile-managed ~/.kiro and a data home moved onto another disk all keep working. They have to: the DEFAULT data home is lexical (`Path.home() / ".kiro" / "crew"`), only a KIROCREW_HOME override is resolved, so on a host whose $HOME is a link the guard fired on the path every user gets and aborted `cloud launch` on grounds they could not satisfy.

Following it gives up no coverage, because the hazard was never the link: it is an ancestor of the REAL directory being swappable, and _first_replaceable already walks the RESOLVED chain. The ownership and mode tests therefore land on the directories that actually hold the tarball rather than on a lexical chain that may not -- a world-writable parent of a relocated home is invisible to a lexical walk and refused by this one. Below the anchor nothing is followed: a link at the `cloud-src-staging` leaf this module creates is still refused before the mkdir and again after it.

Refs #13370
test/link_screen_sites.py records cloud/source.py::_staging_dir. That gate is new on main since this branch was cut, and _staging_dir is a screen-then-resolve site it had no way to know about: it screens the leaf with is_link_or_junction and then mkdirs and resolves the same name. It refuses every link it screens rather than vetting one and continuing, so per the gate's own split it takes a baseline entry and not a HELD_SITES hold. Generated with test/gen_link_screen_sites.py, never hand-edited.
The ancestor mode test refused S_IWOTH only, so a GROUP-writable ancestor passed it. That left the residual argument for following a link at or above the data home wrong as stated: it claimed a parent another account can write is refused, and a shared-group parent is writable by another account and was not. _group_shared_with_another_account settles the membership instead of the bit -- a group holding this account alone is private and stays accepted, which is the user-private-group host the bit alone would have refused -- and it fails closed, so a group whose privacy cannot be proven counts as shared. Both halves of membership are needed: gr_mem lists supplementary members only, so a group shared by PRIMARY membership presents an empty gr_mem and would read as private. Sticky still exempts, and the lookup is skipped when it does. The leaf never had this hole: _lock_to_owner already refuses any group or other bit.

The ancestor walk is no longer what stops a substitution, because it cannot be: every check it can run lands BEFORE the AWS CLI re-opens `--body` by name, which is the wrong side of the window. Three review rounds bore that out, each naming a class the previous walk missed -- world-writable, then shared-group-writable, then symlink-side components, POSIX ACLs and Windows DACLs -- and the enumeration has no natural end. `upload_source` now pins the BYTES instead: `_write_tar_gz` is the single place a final tarball is produced, it digests each byte on its way to the file, and that digest goes to S3 as `--checksum-sha256`. S3 compares it against what it actually received, so a swapped or rewritten tarball is a refused upload rather than a stored one. The check therefore sits downstream of the CLI's open, the one place a pre-flight inspection cannot reach, and it covers an in-place rewrite of the staged file that no ancestor test can see.

The digest is taken at the write, never from a read-back: hashing the finished file would only shrink the window from "our close until the CLI's open" to "our close until our own re-open", and a window one layer down is the same hole. It is deliberately NOT paired with `--checksum-algorithm`, which asks the CLI to compute the digest itself and would hash whatever it re-opened, certifying a substitution instead of catching it. The uid, mode and membership tests stay as a pre-flight that names an actionable `chmod` instead of surfacing as an opaque S3 digest error, but they are defence in depth now, not the gate.
… its own descriptor

Two holes on the staging path, both found in review.

A shared checksum accumulator spans two tarball attempts. _use_git_archive
SWALLOWS a mid-write _refilter_archive failure so it can fall through to the
tarfile fallback, and by then the gzip header has already gone through
_HashingWriter. One shared accumulator folds those discarded bytes into the
fallback's checksum, so the pin describes bytes the named file does not
contain and S3 refuses a good tarball. Each attempt now owns its accumulator.

Resolving a staged temp's name a second time is a window. The staging leaf
sits under the data home, which a same-uid process can write, so between a
mint that closes its handle and a write that reopens the name, that name can
be rebound -- to a link at a governance file such as security_policy.json, or
a hard link to it, which a check on the final component does not catch. The
tarball would land on that target while the launch reported success, and an
absent or unparseable ceiling resolves to the permissive default.

_write_tar_gz takes the open handle its caller holds instead of a path, and
_use_git_archive streams git archive through that handle as stdout rather
than handing the child an -o pathname. mkstemp creates the temp with
O_CREAT|O_EXCL, so the handle names an inode nothing can substitute.
_discard_staged closes before unlinking, the order Windows requires.

Tests: a link planted at the staged name takes no tarball bytes, and the
writer resolves that name zero times. test/test_cloud_source.py is 83 passed.

@bolichen97 bolichen97 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.

Changes-requested (5316588407) is addressed on a970a3e: reproduced locally with HOME -> symlink, ~/.kiro -> link, ~/.kiro/crew -> link, and combined -- _staging_dir() succeeds under the real config_dir(); only the cloud-src-staging leaf is refused when it is a link. test/test_cloud_source.py 84 passed, link-screen gate 17 passed. Two Low nits for a follow-up, not blocking: (1) _staging_dir refusal text always says chmod go-w even for the foreign-owner / unreadable reasons (source.py ~405-415) -- suggest 'set KIROCREW_HOME under your home' there; (2) _use_git_archive success path: if _discard_staged(out) raises after _refilter_archive returned, the filtered tarball leaks (source.py ~559-563) -- try/finally. Approving.

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

Labels

posix-only-approved Cross-Platform Portability findings reviewed and accepted as intentionally POSIX-only

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants