bazel icon indicating copy to clipboard operation
bazel copied to clipboard

Proposal: fold the lockfile's generatedRepoSpecs section into the extension entry digest (defense-in-depth for MODULE.bazel.lock)

Open alencheung opened this issue 2 months ago • 1 comments

This is a hardening proposal, not a bug report. Bazel's documented model treats the workspace author — anyone controlling files in the repo being built — as fully trusted (the discussion on #30439 records the same conclusion for a strictly stronger workspace-file vector), and the lockfile docs promise reproducibility and stability ("preventing unexpected updates or breaking changes"), not integrity against a malicious author. Given that model, what follows is harmless by design; the suggestion is purely about making the lockfile's structure match the role it plays in review.

Observation. In SingleExtensionEvalFunction.tryGettingValueFromLockFile (src/main/java/com/google/devtools/build/lib/bazel/bzlmod/SingleExtensionEvalFunction.java:319-378), a lockfile extension entry's generatedRepoSpecs are used as-is — the extension's Starlark never runs — when all of these hold:

  1. bzlTransitiveDigest matches the currently loadable .bzl files (:336-342),
  2. the usages digest matches (hashForEvaluation, :345-350),
  3. the recorded inputs are unchanged against current machine state (:351-356).

None of the three covers the generatedRepoSpecs section itself. Mechanically: editing only that section in a committed MODULE.bazel.lock — keeping the digests and recorded inputs intact, all of which are visible in the same file — swaps a repository's URL and integrity attributes, and the next build materializes the edited repo without evaluating the extension that nominally generated it. A repo spec edited this way carries its own (attacker-chosen) integrity value, so every downstream checksum gate is satisfied by construction.

To say the obvious twice: the same edit can be made to MODULE.bazel or a .bzl file directly with less work, which is exactly why this is not a boundary crossing under the trusted-author model. The difference is purely social: lockfile diffs are large, machine-generated churn that human review processes necessarily skim, while MODULE.bazel edits are small and hand-written. Right now the lockfile's most consequential section — the one that decides which bytes get downloaded and executed at build time — is also the only section not bound to anything.

Proposal. Fold a digest of the serialized generatedRepoSpecs section into the entry's validation, e.g. hash the section into the bzlTransitiveDigest computation or add a parallel repoSpecsDigest compared alongside the existing three checks. A lockfile-only edit to any repo spec then invalidates the entry and forces re-evaluation, converting the lockfile from "semi-trusted alternate input channel" into "derived artifact with an integrity binding" — the same shape the recorded-inputs mechanism already gives registry file hashes.

Questions for the maintainers:

  • Is this within the intended lockfile contract, or would it break --lockfile_mode=update workflows (digest churn on semantically neutral serialization changes would need canonical serialization)?
  • Should the same binding apply to other lockfile sections that are consumed without re-derivation, or is generatedRepoSpecs the only section with fetch-and-execute consequences?

Context references: #19026 (the FR that introduced resolved repository attributes into the lockfile) and #30439 (the workspace-author trust model discussion).

alencheung avatar Sep 11 '26 10:09 alencheung

Your agent's suggestion is fundamentally cyclic and can't work.

fmeum avatar Sep 11 '26 13:09 fmeum