Skip to content

fix(approve): grant replacement source key for replaced dependencies - #3742

Merged
Daniel Meppiel (danielmeppiel) merged 7 commits into
microsoft:mainfrom
MohammedAlkindi:fix/approve-replaced-dependency
Nov 19, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 7 commits into
microsoft:mainfrom
MohammedAlkindi:fix/approve-replaced-dependency

Conversation

@MohammedAlkindi

@MohammedAlkindi Mohammed Alkindi (MohammedAlkindi) commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Description

With a replace: rule, the executable gate keys consent by the selected with: source, but apm approve acme/hooky wrote a grant for the original identity, so hooks stayed parked. This PR (contributed by Mohammed Alkindi (@MohammedAlkindi), reworked in place by a maintainer with credit) makes apm approve, apm deny and apm policy explain find a replaced dependency by its original or selected-source name and write the key the gate checks.

Root cause and owner: the locked-row trust principal is now one function, security/executables.locked_dependency_approval, which returns the whole answer (LockedApproval: identity, version, original identity). A replaced row routes through dependency_approval_key, the same owner the install gate uses. The approve scanner and the MCP/LSP views (locked_dependency_approval_keys) share it, so an upstream grant never unlocks a replacement anywhere. The three duplicated declaration lookups in commands/approve.py are now one typed helper, _find_declaration, which tries lockfile-derived names before a package's self-declared apm.yml name. The install-deployment-executable-trust-context guard requires the new routing and forbids a parallel derivation in approve.py; mutation tests reject both.

User-visible: apm approve acme/hooky on a dependency replaced by ../local-hooky writes ../local-hooky (for a fork, my-org/hooky-fork#patched). The next apm install deploys the hooks. An existing acme/hooky grant still does not unlock the replacement. apm deny acme/hooky denies both the selected source and acme/hooky, so removing the replace: rule does not revive an old grant.

Issue and approved scope

Issue: Fixes #3710

Human scope-approval comment: #3710 (comment)

Does this PR complete the issue, or what remains? It completes the issue. The reporter's scenario is covered by tests/integration/test_executable_approval_replacement_lifecycle.py for a local path and a fork.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally
  • All existing tests pass
  • Added tests for new functionality (if applicable)

Validation evidence (main merged at 420f937; head 1b2c3d9):

  • Real CLI repro on main (executables: {}, acme/hooky replaced by ../local-hooky): after apm approve acme/hooky the grant was acme/hooky#1.0.0 and the next apm install still printed "1 package(s) have executable primitives parked". On this branch the grant is ../local-hooky and the next install prints "1 hook(s) integrated -> .claude/settings.json".
  • APM_BINARY_PATH=.venv/bin/apm pytest tests/integration/test_executable_approval_replacement_lifecycle.py -p no:xdist: 2 passed (local path and fork; approve, explain, install, then deny re-gates).
  • pytest tests/unit/commands/test_approve_deny.py tests/unit/security/test_executables.py tests/integration/test_architecture_lsp_plugin.py -p no:xdist: 178 passed.
  • Mutation-break: each new trap (lookalike name, upstream deny, upstream allow drop, versioned original name, fork key tuple, parallel-derivation guard) fails with its guard removed.
  • uvx [email protected] check and format --check on src/ tests/ and the scripts paths: passed. pylint R0801: 10.00/10. bash scripts/lint-auth-signals.sh: clean. bash scripts/lint-architecture-boundaries.sh: exit 0.
  • CI at 1b2c3d9: 31 pass, 3 skipping.

Spec conformance (OpenAPM v0.1)

  • Spec edit: docs/src/content/docs/specs/openapm-v0.1.md updated
    (new/changed <a id="req-XXX"></a> anchor + prose + Appendix C
    row).
  • Manifest edit: docs/src/content/docs/specs/manifests/openapm-v0.1.requirements.yml
    updated.
  • Test edit: a @pytest.mark.req("req-XXX") test under
    tests/spec_conformance/ added or extended.
  • CONFORMANCE.{md,json} regenerated via
    uv run --extra dev python -m tests.spec_conformance.gen_statement
    and committed.
  • N/A -- this PR does not change OpenAPM-observable behaviour.

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

…ey owner

apm approve / deny / policy explain now resolve a dependency redirected by
replace: by its original or selected-source name and write the selected
source key the executable gate checks. The locked-row trust principal moves
into security/executables.locked_dependency_approval_identity, which the
approve scanner and the MCP/LSP views share, so an upstream grant never
unlocks the replacement anywhere. Consolidates the three duplicated
declaration lookups into one helper, extends the executable-trust guard,
and adds an installed-CLI lifecycle test (local path and fork).

Refs microsoft#3710

apm-spec-waiver: executable approval key routing for replaced dependencies, no spec contract change

Co-authored-by: Mohammed Alkindi <[email protected]>
Co-authored-by: Copilot App <[email protected]>
@danielmeppiel Daniel Meppiel (danielmeppiel) added this to the 0.37.0 milestone Nov 19, 2026
main's changelog rule (microsoft#3886) asks for one short line per change, ideally
25 words or fewer; the merged entry was about 40.

Co-authored-by: Copilot App <[email protected]>
…tream

Folds the lean panel on microsoft#3742:
- security/executables.locked_dependency_approval now returns the whole
  trust principal (identity, version, original_identity), so approve.py no
  longer re-decides whether a row is replaced (python-architect FU).
- The install-deployment-executable-trust-context guard forbids a parallel
  locked-row derivation in approve.py and drops brittle owner-body tokens;
  a new mutation test proves it (python-architect FU).
- _find_declaration is typed and tiered: lockfile-derived names (approval
  key, replaced original identity) beat a package's self-declared apm.yml
  name, so a lookalike cannot capture approve/deny (supply-chain FU).
- apm deny <original> on a replaced dependency also denies the original
  identity, so removing the replace: rule cannot revive an old grant
  (supply-chain FU); the lifecycle test gains a deny leg and the fork
  key tuple gets a unit test (test-coverage FU).

Refs microsoft#3710

Co-authored-by: Copilot App <[email protected]>
Folds the delta test-coverage findings on microsoft#3742: apm deny <original> on a
replaced dependency must remove an old upstream allow, and apm approve
acme/hooky#1.0.0 must still resolve the replaced row. Both tests fail when
the guarded line is removed. Documents the owner's legacy version override.

Refs microsoft#3710

Co-authored-by: Copilot App <[email protected]>
@danielmeppiel

Copy link
Copy Markdown
Collaborator

APM Review Panel: ship_with_followups

Fixes #3710: apm approve, deny and policy explain now resolve a replaced dependency by its original name and write the selected-source key the install gate checks, through one owner in security/executables.py.

panel-mode=lean -> delta; personas=supply-chain-security-expert,python-architect,test-coverage-expert,apm-ceo

cc Mohammed Alkindi (@MohammedAlkindi) Sergio Sisternes (@sergio-sisternes-epam) -- a fresh advisory pass is ready for your review.

This is a scoped govern-pillar bug fix from community contributor Mohammed Alkindi (@MohammedAlkindi), reworked in place by a maintainer with credit. The lean pass at 4d99780 agreed on the core change: grants now go to the key the gate actually enforces. Each persona still found gaps. supply-chain-security-expert found two. First, _find_declaration let a package's self-declared apm.yml name compete equally with trusted lockfile identities. Second, apm deny <original> left the upstream identity undenied (proven by the passing assertion assert set(stored) == {"./local-hooky"} in test_deny_replaced_dependency_by_original_identity). python-architect found that approve.py re-derived whether a row was replaced, that the linter guard only checked presence, and that the lookup helper was untyped and loosely prefixed. test-coverage-expert found no installed-CLI trap for deny re-gating the replacement and no pin on the fork key tuple.

Every one of those findings was folded with mutation-break evidence. Fold summary:

  • The owner returns a frozen LockedApproval, and the guard forbids a second derivation in approve.py.
  • The lookup is typed and tiered: lockfile key or original_identity first, then package_name, then '#'-prefix forms, with the bare prefix last.
  • Deny on a replaced dependency also denies the original identity and removes its allow entry. The lifecycle test gained a deny leg, and a fork key-tuple test was added.

In the delta pass at d5f4321, sc2 returned no findings. tc2 named two untrapped fold behaviors. Iteration 2 (head 1b2c3d9) folded both: test_deny_replaced_dependency_drops_upstream_allow fails when the upstream allow.pop is removed, and test_approve_replaced_dependency_by_versioned_original_name fails when _original_prefix is removed. Every test-evidence finding from both passes is now closed with a test, not an opinion.

What remains are four deferrals, each outside the scope record:

  1. arch2's version parameter on the owner. Dropping it would change the grant key for non-replaced dependencies, which the scope record excludes. It fails closed and is now documented as the legacy approve override.
  2. The --user local-path grant policy. Path-keyed user trust predates this PR.
  3. Bare package_name prefix narrowing for non-replaced dependencies. This is pre-existing lookup behavior, now ranked last so it cannot beat an exact or lockfile match.
  4. The deployed-hook revocation gap. The new deny leg showed that for ANY package, deny plus reinstall sets exec_status denied but leaves already-deployed hooks in .claude/settings.json. This predates the PR, and the PR's own test is what found it. It is the highest-signal follow-up and deserves its own issue.

Dissent. No dissent. arch2 classified the version-parameter gap as recommended and agreed it is pre-existing and fails closed. Deferring it at the scope boundary matches that.

Aligned with: secure by default: Upstream grants still do not unlock a replacement. Deny now covers both the original and selected-source identity and removes stale allow entries. Lockfile identities outrank self-declared names. governed by policy: Approve, deny and policy explain now agree with the gate, through the single owner locked_dependency_approval. A linter guard with a mutation test stops a second derivation from coming back. portable by manifest: Replacing a dependency in apm.yml with a local path or fork no longer silently breaks executable trust. Users keep naming the dependency as declared in the manifest. oss community driven: A community fix was reworked in place with contributor credit, not replaced. This keeps the contributor relationship intact. pragmatic as npm: Users approve the name they declared, including versioned forms like acme/hooky#1.0.0, and the tool maps it to what actually installs.

Panel summary

Persona B R N Takeaway
Python Architect 0 1 0 All three prior findings are resolved: the owner returns a frozen LockedApproval, the guard blocks a second derivation, and _find_declaration is typed and tiered. One leftover gap in the owner API.
Supply Chain Security Expert 0 0 0 Folds resolve both prior findings: lookup now prefers lockfile names, and deny also covers the upstream identity. Deferring the --user path grant is reasonable. No new risk; ship.
Test Coverage Expert 0 2 0 Folds close both prior gaps: the lifecycle test has a deny leg and the fork key tuple is pinned. Two new fold behaviors have no trap: deny popping the upstream allow, and the '#'-prefix tier.

B = blocking-severity findings, R = recommended, N = nits.
Counts are signal strength, not gates. The maintainer ships. Test Coverage Expert's two delta findings were folded in 1b2c3d9.

Top 4 follow-ups

  1. [Test Coverage Expert] Open a dedicated issue: apm deny plus reinstall sets exec_status denied but leaves already-deployed hooks in .claude/settings.json, for ANY package (seen on a plain non-replaced package). -- This is a real revocation gap on a secure-by-default promise: a user who denies a package expects its hooks to stop running. It predates this PR and affects every package, not just replaced ones, so it is outside the scope record. The new deny lifecycle leg found it, and that leg should become the regression test for the fix.
  2. [Supply Chain Security Expert] Decide a policy for apm approve --user when the resolved grant key is a local path (for example ./local-hooky): refuse it, or keep replacement grants project-scoped. -- A path-keyed user-scope grant can unlock any project's dependency at the same relative path. Path-keyed local trust predates this PR for plain local dependencies. The scope record covers resolving replaced dependencies, not user-scope trust policy.
  3. [Python Architect] Narrow the bare package_name prefix match for non-replaced dependencies (require a '#' boundary or refuse ambiguous matches, for example acme/hook vs acme/hooky). -- This lookup behavior predates the PR. The scope record covers resolving replaced dependencies only. The fold already ranks this match last so it cannot beat an exact or lockfile match, which limits the risk until a separate change tightens it.
  4. [Python Architect] Remove the caller-supplied version parameter from locked_dependency_approval so the owner alone decides the full trust key. -- Removing it changes the grant key for non-replaced dependencies (approve currently binds the package apm.yml version). The scope record excludes 'approval key format for non-replaced deps'. It fails closed and is documented as the legacy approve override, so it can ship as its own key-format change with a CHANGELOG migration line.

Recommendation

Merge once CI on 1b2c3d9 is green; it was 31/31 at 4d99780 and the lint chain is silent. Every in-scope finding from both passes is folded with mutation-break evidence, and the Done-when is covered by an installed-CLI lifecycle test for local and fork replacements, including the deny leg. Before or alongside the merge, open a dedicated issue for the deployed-hook revocation gap on deny plus reinstall; it affects every package. Track the other three deferrals as post-merge follow-ups.

Folded in this run

  • (panel) Owner returns the whole trust principal (LockedApproval); approve.py no longer re-decides 'replaced' -- resolved in d5f4321.
  • (panel) Trust guard forbids a parallel locked-row derivation in approve.py; brittle owner-body tokens dropped; mutation test added -- resolved in d5f4321.
  • (panel) _find_declaration typed and tiered: lockfile key / original identity beat a self-declared apm.yml name -- resolved in d5f4321.
  • (panel) apm deny <original> on a replaced dependency also denies the original identity; lifecycle deny leg; fork key-tuple unit test -- resolved in d5f4321.
  • (panel) Trap for deny dropping an upstream allow; trap for apm approve acme/hooky#1.0.0 on a replaced row -- resolved in 1b2c3d9.
  • (panel) CHANGELOG entry shortened to main's one-line rule (docs(changelog): make 0.36.0 onwards legible and concise #3886) after merging main -- resolved in adf234d.

Copilot signals reviewed

  • review 5909401294 -- NOT-LEGIT: Copilot could not review (requester quota); no inline items.

Deferred (out-of-scope follow-ups)

These cross the stated scope of this PR. Each suggests a separate follow-up.

  • (panel) apm deny + reinstall leaves already-deployed hooks in .claude/settings.json (exec_status is denied), for any package -- scope boundary: pre-existing revocation gap for every package, not replaced-dependency key routing; suggested follow-up: new type/bug issue.
  • (panel) apm approve --user writing a local-path key to user scope -- scope boundary: pre-existing path-keyed user trust policy, not replaced-dependency resolution.
  • (panel) Narrow the bare package_name prefix lookup for non-replaced packages -- scope boundary: pre-existing lookup semantics for non-replaced deps; now ranked last.
  • (panel) Drop the caller-supplied version from locked_dependency_approval -- scope boundary: changes the approval key for non-replaced dependencies, listed out of scope in the scope record.

Regression-trap evidence (mutation-break gate)

  • test_approve_original_identity_beats_lookalike_package_name -- deleted the lockfile-first tier; test FAILED as expected; guard restored.
  • test_deny_replaced_dependency_by_original_identity -- deleted the upstream deny write; test FAILED as expected; guard restored.
  • test_approve_parallel_replacement_derivation_is_rejected -- deleted the forbidden-token check; test FAILED as expected; guard restored.
  • test_locked_fork_replacement_keys_name_the_fork -- deleted the version-stripped key; test FAILED as expected; guard restored.
  • test_deny_replaced_dependency_drops_upstream_allow -- deleted the upstream allow.pop; test FAILED as expected; guard restored.
  • test_approve_replaced_dependency_by_versioned_original_name -- deleted _original_prefix; test FAILED as expected; guard restored.

Lint contract

ruff check and ruff format --check (ruff 0.15.12 per uv.lock) silent on src/ tests/ and the scripts paths; pylint R0801 10.00/10; scripts/lint-auth-signals.sh clean; scripts/lint-architecture-boundaries.sh exit 0.

CI

gh pr checks 3742: 31 pass, 3 skipping, 0 failing at 1b2c3d9 (after 0 CI fix iteration(s)).

Exact-head functional evidence: APM_BINARY_PATH=.venv/bin/apm pytest tests/integration/test_executable_approval_replacement_lifecycle.py -p no:xdist 2 passed; approve/deny + executables unit + LSP architecture tests 178 passed.

Mergeability status

PR head SHA CEO stance iters folds defers Copilot rounds CI mergeable mergeStateStatus notes
#3742 1b2c3d9 ship_with_followups 2 6 4 1 green MERGEABLE BLOCKED pending required review

Convergence

2 outer iteration(s); 1 Copilot round(s). Final panel verdict: ship_with_followups.

Ready for maintainer review.


Full per-persona findings

Python Architect

  • [recommended] locked_dependency_approval still takes a version from the caller, so approve.py can still decide part of the trust key for a row that was not replaced at src/apm_cli/security/executables.py:551
    approve.py passes the version from the package's own apm.yml while locked_dependency_approval_keys uses the lockfile version. Pre-existing and fails closed (a mismatch does not match), but the PR writes this parallel input into the owner's public signature. Deferred (see Deferred above).
    Suggested: Drop the version parameter, or document it as a legacy override and forbid other callers.

Supply Chain Security Expert

No findings.

Test Coverage Expert

  • [recommended] No test checks that apm deny on a replaced dep removes an existing upstream allow entry. at tests/unit/commands/test_approve_deny.py:509
    Deleting the upstream allow.pop in deny_cmd passes every test; a leftover upstream allow would come back into force if the deny entry is later edited away. Resolved in 1b2c3d9.
    Suggested: Seed allow with acme/hooky and ./local-hooky, deny acme/hooky, assert allow empty.
    Proof (missing at): tests/unit/commands/test_approve_deny.py -- proves: Denying the declared name of a replaced dependency removes an old upstream grant. [secure-by-default,governed-by-policy]
  • [recommended] No test covers the versioned-original lookup tier: apm approve/deny acme/hooky#1.0.0 on a replaced dep. at tests/unit/commands/test_approve_deny.py:552
    The docstring promises acme/hooky#1.0.0 finds a replaced acme/hooky; dropping _original_prefix would fail no test. Resolved in 1b2c3d9.
    Suggested: Add a test approving acme/hooky#1.0.0 and asserting the ./local-hooky grant.
    Proof (missing at): tests/unit/commands/test_approve_deny.py -- proves: Approving a replaced dependency by its versioned declared name targets the selected source. [secure-by-default,devx]

This panel is advisory. It does not block merge. Re-apply the
panel-review label after addressing feedback to re-run.


Generated by autopilot-pr-review-worker. This comment is AI-generated and may contain errors.

@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit d547a5b into microsoft:main Nov 19, 2026
35 checks passed
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.

[BUG] apm approve <pkg> on a replaced dependency grants the original identity, which the executable gate refuses

3 participants