Repository navigation
fix(hashing): use explicit SHA-2 policy digests and synthetic fixture IDs - #2872
Conversation
… IDs Preserve policy pin bytes, normalization and cache compatibility while routing verification through the existing digest owner. Replace benchmark-only SHA-1 IDs with deterministic nonzero 40-character hex IDs. Co-authored-by: Copilot App <[email protected]>
Docs sync advisoryVerdict: no_change * Pages affected: 0 additional * LLM calls: 2/15 * Took: not recorded No further documentation changes are needed. SHA-256/384/512 support, raw-byte pins, normalization, fail-closed outcomes, and cache identities are unchanged. The PR already clarifies the existing exhaustive allowlist in the policy reference. Synthetic benchmark IDs and the internal owner guard have no additional user-facing docs impact. README and the shipped governance resource remain accurate. |
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
A new unmarked test in the benchmarks module will run in the default suite (benchmarks are deselected by marker), and compute_policy_hash contains redundant/unreachable error handling that should be simplified for maintainability.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review tier: Lite
Findings: 2
New issues introduced by this change (2)
| Severity | Finding |
|---|---|
src/apm_cli/policy/project_config.py — The final ProjectPolicyConfigError raise is currently unreachable because algorithm is already… |
|
tests/benchmarks/test_git_and_compiler_benchmarks.py — This new fixture-shape test is unmarked, but the repo's default pytest selection is `-m 'not… |
What changed in this PR
This PR hardens policy hashing by eliminating dynamic hashlib.new(...) calls in policy verification and standardizing SHA-2 digest computation through the existing compute_policy_hash owner, while also removing benchmark-only SHA-1 usage for synthetic fixture IDs.
Changes:
- Route policy pin verification and cache digest computation through explicit SHA-256/384/512 constructors in
compute_policy_hash. - Replace benchmark fixture SHA-1 generation with deterministic synthetic 40-hex IDs and add regression assertions around parsed refs.
- Register and enforce an architecture guard ensuring discovery hashing delegates to the canonical owner; expand unit/integration coverage and clarify the SHA-2 allowlist in docs.
| File | Description |
|---|---|
| src/apm_cli/policy/project_config.py | Expand compute_policy_hash to accept `str |
| src/apm_cli/policy/discovery.py | Delegate _verify_hash_pin digest computation to compute_policy_hash. |
| tests/unit/policy/test_policy_hash_pin.py | Add unit coverage for explicit SHA-2 behavior, raw-bytes semantics, and cache compatibility. |
| tests/benchmarks/test_git_and_compiler_benchmarks.py | Remove SHA-1 fixture hashing; add synthetic ID generation and stronger ref parsing assertions. |
| .apm/architecture/owners/contracts-tooling.json | Register policy-content-hash ownership and its guard. |
| scripts/architecture_linter/checks/contracts_test_taxonomy.py | Add guard enforcing delegation to compute_policy_hash and disallow direct hashlib.* in discovery hashing paths. |
| tests/integration/test_architecture_owner_rule_mutations.py | Add mutation proving the new guard catches local hashing bypass. |
| tests/unit/scripts/test_architecture_runner.py | Update expected architecture-rule catalog list to include the new guard. |
| docs/src/content/docs/enterprise/policy-reference.md | Clarify the exhaustive SHA-2 allowlist for policy hash pins. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| raise ProjectPolicyConfigError( | ||
| f"Refusing to compute policy hash with unsupported algorithm '{algorithm}'" | ||
| ) |
| def test_synthetic_object_ids_preserve_format_and_uniqueness() -> None: | ||
| """Fixture IDs stay deterministic and distinct across all benchmark ranges.""" |
Stay current with main (microsoft#2872, microsoft#2873, microsoft#2874). Co-authored-by: Cursor <[email protected]>

fix(hashing): use explicit SHA-2 policy digests and synthetic fixture IDs
TL;DR
Remove benchmark-only SHA-1 and replace dynamic policy digest construction with explicit SHA-256/384/512 APIs in the existing hash owner. This addresses surviving source patterns associated with
SM02167/py/weak-hasheswithout changing accepted policy algorithms, pins, cache identities, or real Git object hashing.Important
Policy hashing already rejected weak algorithms. This is not evidence of an exploitable weak-policy hash, and the private Microsoft CodeQL query pack was not rerun.
Problem (WHY)
_make_shaon current main still used SHA-1 only to manufacture synthetic reference/lockfile IDs.compute_policy_hashstill calledhashlib.new(variable), despite restricting that variable to SHA-256/384/512. Explicit constructors remove this dynamic-API ambiguity without broadening the allowlist.discovery.pylocations (lines 35/89) do not identify hashing calls even at the report's recorded commit,0ae9a78c6129e41caf87cb75130f9006b31ed98d. The current dynamic sites arediscovery._verify_hash_pinandproject_config.compute_policy_hash; exact attribution of those two old findings cannot be established from their locations alone.Source inspection and executable regressions, rather than alert-record state, are the evidence here: "do the work, run a validator (a script, a reference checklist, or a self-check), fix any issues, and repeat until validation passes."
Approach (WHAT)
compute_policy_hashowner for both verification and cache-metadata digests, with explicit SHA-2 constructors.Implementation (HOW)
src/apm_cli/policy/project_config.pysrc/apm_cli/policy/discovery.pytests/benchmarks/test_git_and_compiler_benchmarks.pytests/unit/policy/test_policy_hash_pin.py.apm/architecture/owners/contracts-tooling.jsonscripts/architecture_linter/checks/contracts_test_taxonomy.pytests/integration/test_architecture_owner_rule_mutations.pytests/unit/scripts/test_architecture_runner.pydocs/src/content/docs/enterprise/policy-reference.mdDiagrams
Both discovery digest paths now share the same explicit SHA-2 implementation; dashed nodes identify the changed implementation.
flowchart LR subgraph Discovery V["_verify_hash_pin"] C["_compute_hash_normalized"] end subgraph Owner["project_config.py"] H["compute_policy_hash"] S["Explicit SHA-256 / SHA-384 / SHA-512"] end V --> H C --> H H --> S classDef changed stroke-dasharray: 5 5; class H,S changed;Trade-offs
Benefits
hashlib.newcalls remain in the three affected source/test files.Validation
Local validation on
d8a9bd0d2plus this commit; this is not a claim about remote CI or the private query pack.Focused regression and lint evidence
uv run --frozen --extra dev pytest -q -m 'not live'with the selectors below explicitly includes benchmarks:Full canonical lint mirror after merging latest main: Ruff check/format over
src/,tests/, and architecture-linter sources; pylint R0801; YAML I/O, 2100-line, portable-relative-path guards; auth signals; architecture boundaries. All exited 0. The three grep-style guards used equivalent Python regex/line scans on macOS, not an unavailablergexecutable.uv run --frozen python scripts/check_test_assertions.py:uv run --frozen python scripts/check_exact_test_duplicates.py:AST inspection confirmed no
hashlib.new,hashlib.sha1, orhashlib.md5calls in the three affected files. Mermaid validated withmmdc11.15.0.uv.lockis unchanged.Scenario Evidence
tests/unit/policy/test_policy_hash_pin.py::TestVerifyHashPin::test_explicit_sha2_preserves_raw_bytestests/unit/policy/test_policy_hash_pin.py::test_existing_sha2_cache_digest_remains_compatibletests/unit/policy/test_policy_hash_pin.py::TestVerifyHashPin::test_unsupported_algorithms_still_fail_closedHow to test
uv run --frozen --extra dev pytest -q tests/unit/policy/test_policy_hash_pin.py tests/unit/policy/test_cache_merged_effective.py; matching pins/cache entries should pass and mismatches should fail closed.uv run --frozen --extra dev pytest -q -m 'not live' tests/benchmarks/test_git_and_compiler_benchmarks.py; all fixture and benchmark cases should pass.bash scripts/lint-architecture-boundaries.shanduv run --frozen --extra dev pytest -q tests/integration/test_architecture_owner_rule_mutations.py -k policy-content-hash; the current source should pass and the in-memory delegation mutation should be detected.Co-authored-by: Copilot [email protected]