Repository navigation
fix: trust and integrity consistency hardening (bundle admission, update integrity) - #4157
Conversation
Trust and integrity consistency hardening (#4080). A new bundle admission owner (install/bundle_admission.py) runs the org policy preflight and the require_hashes integrity decision for every bundle deploy entry point. apm install <bundle> and apm unpack both route through it. apm unpack also holds the lifecycle lock and records the files it deploys into an APM project through DeploymentLedgerCodec.record_local_bundle_files. A non-project output directory stays untracked and says so. Guard: install-deployment-bundle-admission-owner, with a mutation case. Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…cked hint Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Trust and integrity consistency hardening. apm update now treats a registry package as its name plus exact version: when the re-resolved version equals the locked version, the downloaded archive must match the locked resolved_hash, and a mismatch fails closed with exit 1, leaving the lockfile and deployed files unchanged. A moved version re-locks and reports its new hash. Both registry download sites (the resolve callback and the fresh materialization source) route through install/source_helpers.py download_registry_package and its locked_version_unchanged predicate, next to the git commit predicates of the update-content-integrity owner. A closed-world architecture rule (install-deployment-update-registry-integrity) refuses direct registry downloads elsewhere, with one MutationCase. Installed-CLI regression tests, a bug-ledger row and a CHANGELOG line are included. Closes #4125 Co-authored-by: copilot-swe-agent[bot] <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved defects allow compilation-target policy bypasses, break cross-target hash verification, and overwrite project files before validating the destination lockfile.
5 open issues
Check destination lockfile writability before copying files · New Reject unknown pack targets during deployment · New Remap lockfile hash keys for cross-target pack paths · New Inventory only files actually copied into the destination · New Preserve configured registry names during bundle preflight · New
What changed in this PR
Aligns legacy apm unpack deployments with the shared policy, integrity, locking, and ownership mechanisms used by bundle installation, addressing #4080.
Changes:
- Centralizes bundle admission and adds policy and hash checks before unpacking.
- Serializes unpack operations and records deployed files in APM project lockfiles.
- Adds regression coverage, architecture guards, and updated command documentation.
| File | Description |
|---|---|
tests/utils/unpack_admission.py |
Adds an accepting admission helper for mechanics tests. |
tests/unit/test_unpacker.py |
Tests admission ordering and bundle hash checks. |
tests/unit/test_unpack_security.py |
Adapts security tests to required admission. |
tests/unit/test_packer.py |
Updates the pack/unpack test helper. |
tests/unit/scripts/test_architecture_runner.py |
Registers the new guard in the expected inventory. |
tests/unit/bundle/test_tar_windows_absolute_path.py |
Adapts archive path-safety coverage. |
tests/quality/complexity_baseline.json |
Removes the resolved statement-count exemption. |
tests/integration/test_wave4_pure_logic_coverage.py |
Updates unpack tests for required admission. |
tests/integration/test_trust_integrity_consistency_lifecycle.py |
Adds policy, integrity, ownership, and locking scenarios. |
tests/integration/test_architecture_owner_rule_mutations.py |
Adds an admission-bypass mutation case. |
tests/fixtures/lifecycle_bug_ledger.json |
Records #4080 regression coverage. |
src/apm_cli/install/local_bundle_handler.py |
Routes bundle installation through shared admission. |
src/apm_cli/install/bundle_admission.py |
Introduces admission, integrity, and recording helpers. |
src/apm_cli/commands/pack.py |
Connects unpack to locking, admission, and recording. |
src/apm_cli/bundle/unpacker.py |
Requires admission and reports written files. |
scripts/architecture_linter/checks/install_trust_integrity.py |
Adds the bundle admission ownership guard. |
scripts/architecture_linter/checks/install_policy_gitlab_and_bundle.py |
Retargets policy checks to the shared owner. |
scripts/architecture_linter/checks/install_deployment_analyzers.py |
Registers the admission analyzer. |
packages/apm-guide/.apm/skills/apm-usage/commands.md |
Updates unpack usage guidance. |
docs/src/content/docs/reference/cli/unpack.md |
Documents policy, tracking, locking, and exit behavior. |
CHANGELOG.md |
Announces the unpack behavior changes. |
.apm/architecture/owners/install-deployment.json |
Registers the canonical bundle admission owner. |
Review effort: Balanced
apm-spec-waiver: CLI-side admission and update integrity consistency; no OpenAPM format, manifest or registry contract change. Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…copy-validity Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…ntegrity lines Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Unpack keeps existing lock entries, applies the org target allow-list, and deploys an intact bundle under require_hashes; update accepts an unchanged registry version whose bytes match the lock. Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…mapped hashes, copied files and registry names Admission now evaluates the bundle pack target (unknown targets and files outside the target's paths are refused; a bundle without a target is checked as all targets), confirms the project lockfile can be updated before any write, and forwards registry names and the project registries map to the policy preflight. Pack projects deployed_file_hashes onto cross-target mapped paths. The unpacker reports only the files it copied. Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: Copilot App <[email protected]>
Krish Khinchi (krishkhinchi)
left a comment
There was a problem hiding this comment.
Good architectural improvement, but there are still important edge cases around bundle admission and deployment integrity. In particular, unknown pack targets must fail closed, cross-target hash keys need correct remapping, and destination lockfile writability must be validated before copying any files. The registry preflight should also preserve configured registry names, and deployment records should include only files actually written. Recommendation: Request Changes until these cases are fixed and the required CI checks pass.
Co-authored-by: Copilot App <[email protected]> # Conflicts: # tests/fixtures/lifecycle_bug_ledger.json # tests/integration/test_trust_integrity_consistency_lifecycle.py
Co-authored-by: Copilot App <[email protected]>
…onal conformance recorder unpack_cmd is serialized on main (#4157), so it leaves the pending list. The #4188 frozen hook-pin tip renders from frozen-hook-pins-stale. The root conftest loads the conformance recorder only when a req marker or collect-only run needs it, so bounded mutation candidates collect. Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…APM 0.37 Since APM 0.37, 'apm unpack' into an APM project records the files it deploys in the project's apm.lock.yaml (microsoft/apm#4157). That dirties a tracked lockfile after restore and breaks the gh-aw pull_request_target checkout of the PR ref (#26). The action now snapshots apm.yml and apm.lock.yaml in the output directory before 'apm unpack' (single bundle and bundles-file) and puts them back byte for byte afterwards (in a finally, so a failed unpack restores them too); a file unpack created where none existed is removed. apm unpack still runs against the real project, so its project-level admission checks are unchanged. Regression coverage: - unit: extractBundle and restoreMultiBundles keep both files byte-identical when unpack rewrites them, remove a created lockfile, and restore on failure (fail without the fix); - CI test-restore-clean-workspace: a simulated PR commit changes apm.lock.yaml, and after restore a branch-style 'git checkout <pr-sha>' must succeed (the path checkout '-- .' it already ran never fails on local changes). Co-authored-by: Copilot App <[email protected]>


Description
TL;DR. Trust and integrity consistency hardening.
apm unpacknow uses the same shared install owners asapm install <bundle>. A new bundle admission owner (install/bundle_admission.py) runs the org policy preflight and therequire_hashesintegrity decision for every bundle deploy entry point.apm unpackalso holds the lifecycle lock, and it records the files it deploys when the output is an APM project. It also hardensapm update(#4125). When a registry dependency re-resolves to its locked version,apm updatechecks the archive against the lockedresolved_hashand fails closed on a mismatch; a moved version re-locks.Problem (WHY)
apm install <bundle>andapm unpackbehave the same.apm unpack <bundle> -o <dir>, so the command must stay and keep its surface.Approach (WHAT)
apm install <bundle>apm unpackadmit_bundle->run_policy_preflightregistries:resolved from the output dir; an unknown pack target or a file outside the pack target's paths is refused, and a bundle with no pack target is checked as all targetsrequire_hashesadmit_bundle+ a per-format checkpack.bundle_files(unchanged)deployed_file_hashes: refused underrequire_hashes, otherwise a warningserialized_lifecycle_unless("dry_run")DeploymentLedgerCodec.record_local_bundle_filesenrich_lockfile_for_packdeployed_file_hashesare projected onto mapped bundle pathsExecutable content is still refused by the unpacker before admission runs (#4051 behavior, unchanged).
Implementation (HOW)
src/apm_cli/install/bundle_admission.py(new)admit_bundle(preflight +require_hashesdecision),plugin_bundle_integrity(moved verbatim from the handler),apm_bundle_integrity, the unpack adapterapm_bundle_admission, and the record seam (unpack_record_root+record_unpacked_files).src/apm_cli/install/local_bundle_handler.pyadmit_bundle(...)call. Behavior is unchanged.src/apm_cli/bundle/unpacker.pyadmitcallback (anUnpackCandidate) runs before the dry-run return and the first copy.UnpackResult.written_fileslists the regular files written.src/apm_cli/commands/pack.pyunpack_cmd:@serialized_lifecycle_unless("dry_run"),InstallLogger, admission, the record seam,render_summary()(so warn-mode violations print), and a policy refusal exits 1.scripts/architecture_linter/...install-deployment-bundle-admission-owner. The existinginstall-deployment-local-bundle-policy-preflightrule is retargeted to the new owner file;require_hashesbanned paths include it..apm/architecture/owners/install-deployment.jsonbundle-admission-owner.MutationCase, a bug-ledger row, and a complexity-baseline shrink (install_local_bundleno longer trips PLR0915).reference/cli/unpack.md(policy, tracked output, lock, exit codes),apm-usage/commands.md, CHANGELOG.#4125:
apm updateverifies unchanged registry versionsapm update; registry dependencies now get the same treatment through one shared download helper used by both registry download sites (phases/resolve.pycallback andsources.py).install/source_helpers.py, next to the gitlocked_commit_unchanged/update_accepts_new_contentpredicates (ownerupdate-content-integrity), gainslocked_version_unchangedanddownload_registry_package. Both download sites route throughdownload_registry_package. For an unchanged version, aresolved_hashmismatch removes the download and raises the sameContent hash mismatcherror as the git path, naming the package, version, expected hash and actual hash. Lockfile and deployed files stay byte-identical.install-deployment-update-registry-integrity(closed world: any other registry-resolverdownload_packagecaller insrc/apm_cli/fails). MutationCase of the same id. Ledger row: issue 4125.Design note: record rule
apm.yml, the deployed files go into<output>/apm.lock.yamlthroughDeploymentLedgerCodec.record_local_bundle_files, the same owner callapm install <bundle>uses. The ownership result for an overlapping path (owner.becomes[".", "local-bundle"]with active ownerlocal-bundle) is therefore the owner's decision, not an unpack rule. Any other output dir writes no lockfile and prints[i] unpack output is not tracked: the output directory has no apm.yml. Unpack into an APM project to record the deployed files.unpack_record_root()is the single decision point. Changing the rule (for example to "never record") is a one-line change there.apm auditcontent-integrity reads locklocal_deployed_file_hashes, and the materialization receipts only support files the lock already claims. A sidecar would be write-only..github/workflows/pr-review-panel.lock.yml). The bundle was packed frommicrosoft/apm#main(target copilot,--archive --format apm) and unpacked withapm unpack <bundle> -o <checkout>into a fresh clone of this repo, with tokens stripped asmultibundle.tsdoes. On origin/main code the checkout is already dirty: 8 tracked.github/files are overwritten by the bundle. With this PR,apm.lock.yamlis modified too. No later step in that workflow fails on a dirty tree: the only checkout runs before the restore, "Audit pre-agent workspace" iscontinue-on-error, safe outputs areadd_commentonly (no patch), and the detection job checks out fresh. The workflow also pinsapm-version: 0.28.0. The action'sworking-directorydefaults to.(the job checkout).Trade-offs
unpack_bundletakes a requiredadmitkeyword rather than an optional one. No caller can skip admission. Unpacker mechanics tests use a small accepting helper (tests/utils/unpack_admission.py).require_hashesis a warning, not a refusal.apm pack --format apmcopies on-disk bytes, so a producer whose files drifted after install would otherwise break existing restores.apm.yml; the printed remediation hint namesapm unpackcorrectly. Rewording the shared messages is left out of scope.Benefits
fetch_failure_default: block, or an integrity mismatch underrequire_hashesexits 1 with the output tree byte-identical.apm audit --cicontent-integrity and survive a laterapm install.apm unpackwaits for other APM operations.Issue and approved scope
Fixes #4080
Fixes #4125
Human scope-approval comments:
Relates to #4051.
This PR completes #4080 and #4125.
Type of change
Testing
Lifecycle evidence (installed CLI,
APM_BINARY_PATH=.venv/bin/apm, moduletests/integration/test_trust_integrity_consistency_lifecycle.py):test_unpack_applies_org_dependency_policytest_unpack_applies_org_target_policytest_unpack_reports_warn_policy_violations_and_deploysrequire_hashesan intact bundle deploys with recorded hashes; altered bytes are refused and the snapshot is unchangedtest_unpack_refuses_altered_bytes_when_policy_requires_hashestest_unpack_fails_closed_when_policy_is_unavailable_under_blockinstallkeeps them;audit --cisees altered bytestest_unpack_records_deployed_files_in_the_project_lockfiletest_unpack_into_a_project_keeps_unapproved_executables_outtest_unpack_into_a_non_project_dir_announces_and_writes_no_lockfiletest_unpack_waits_for_the_lifecycle_locktest_unpack_refuses_files_its_target_policy_cannot_place[bogus,claude,None]test_unpack_refuses_an_unwritable_project_lockfile_before_writing[malformed,symlink,newer]require_hashes; altered mapped bytes are refusedtest_unpack_verifies_cross_target_mapped_files_under_require_hashes#4125 evidence (same module):
test_update_refuses_altered_bytes_for_an_unchanged_registry_versiontest_update_accepts_a_new_registry_versionOwner unit tests (
tests/unit/test_unpacker.py):TestUnpackWrittenFiles::test_skipped_non_content_files_are_not_claimed,TestApmBundleAdmission::test_registry_dependency_keeps_its_configured_registry_name-- passed.Guards:
install-deployment-bundle-admission-owner(routes extended:admit->prepare_unpack_record,_bundle_target_decision;prepare_unpack_record->ensure_lockfile_writable;_bundle_target_decision->pack_target_prefixes),install-deployment-update-registry-integrity. MutationCaseinstall-deployment-bundle-admission-owner(replaces unpack's admission with a no-op) is caught by only that rule. Ledger rows: issues 4080 and 4125.Commands and results (head 0b82daa, merged with current main)
apm-spec-waiver: CLI-side admission consistency for the existing unpack command; no OpenAPM format, manifest or registry contract change.
Changelog
## [Unreleased]inCHANGELOG.md.Spec conformance (OpenAPM v0.1)
Not applicable: no
req-XXXbehavior changes (see the waiver line above).How to test
APM_BINARY_PATH=$PWD/.venv/bin/apm uv run --extra dev pytest tests/integration/test_trust_integrity_consistency_lifecycle.py -k unpack -q--format apmbundle, runapm unpack <bundle>: exit 1, nothing written. With--dry-run: exit 0 and a "Would be blocked by policy" line.apm.yml: the[i] unpack output is not trackedline prints and noapm.lock.yamlappears.Co-authored-by: Copilot [email protected]