Skip to content

fix(dependency-gate): compare against the merge base, not the base branch tip - #4

Merged
ChesterSim merged 2 commits into
masterfrom
fix/dependency-gate-merge-base
Sep 15, 2026
Merged

ChesterSim merged 2 commits into
masterfrom
fix/dependency-gate-merge-base

Conversation

@ChesterSim

@ChesterSim ChesterSim commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

The bug

github.event.pull_request.base.sha is the tip of the base branch at event time, not the merge base. Under pull_request_target the action reads the head lockfiles through the contents API at HEAD_SHA (a real commit on the PR's branch) and diffs them against that tip:

base=$(resolve "$BASE_SHA" || ...)              # resolve step
git show "$BASE:$lock" > "$work/base/$lock"     # collect step

So a pull request that is merely behind its base branch gets the base branch's own lockfile history replayed at it, inverted: an entry the base branch dropped is still present at head, and reads as one the pull request added. The author's only way through is a deps-reviewed label signing off on findings that do not exist.

How it showed up

velocity-v1#511 failed the gate (run) with two bun.lock findings:

Change Package
new package @types/protobufjs
source changed @velocity-exchange/sdk

That PR touches no bun.lock at all. Its files are .changeset/, packages/cli-admin/, packages/sdk/src/adminClient.ts, rust/Cargo.lock.

It was 5 commits behind master, and master had re-resolved the tree in the meantime (@velocity-exchange/common 0.0.1 to 0.10.1). That dropped @types/protobufjs from the lockfile and collapsed the hoisted @velocity-exchange/common/@velocity-exchange/sdk entry from 66 nested keys to 4. The stale branch still carried both, so against master's tip they read as additions.

  • git diff <merge-base> <head> -- bun.lock is empty
  • git diff <base tip> <head> -- bun.lock is 137 insertions, 11 deletions

Verified, not assumed

Running lockfile_packages.py over the real git blobs:

base = master tip af1792c65  ->  new     @types/protobufjs       registry:default
                                 source  @velocity-exchange/sdk  registry:default
base = merge base 8af762adb  ->  (nothing)

The first case reproduces the failing run's findings and its digest 767947ca9e2ca444745af84f byte for byte. The second is clean. The job log confirms the base used: Mode prt, comparing against af1792c65a9c0a6557bc709f53f5a7741c53beae.

The fix

prt mode only. Resolve the merge base through the compare API, not git merge-base.

git merge-base is the obvious move and it is wrong here. On a fork pull request the head commit is not an object in the checkout at all: actions/checkout fetches refs/heads/* and refs/tags/* of the base repository only, no refs/pull/*. git merge-base would exit non-zero and take the step down with it, or fall through to the hard Could not resolve the base revision error, so the gate would become unrunnable on exactly the pull requests it matters most for. velocity-v1#511 would have worked by luck, its head branch lives in the base repo.

repos/{repo}/compare/{base}...{head} needs nothing fetched and only the contents: read the action already requires.

Security is unchanged: the merge base is by definition an ancestor of base.sha, so it is still base-branch content. Nothing of the pull request's is read or executed. An attacker can only make the merge base older, which yields more findings, never fewer.

The old tip candidates stay on as fallbacks behind the merge base, so a shallow checkout, a rewritten base, or an API blip degrades to the old over-reporting rather than failing the run. That degradation now emits a ::warning::, because over-reporting changes the digest, which deletes a correctly-applied sign-off label.

pr mode deliberately keeps the base tip

The first commit changed both modes. That was wrong, and review caught it.

The two modes read the head lockfiles from different places. prt reads them at HEAD_SHA. pr reads them off the workspace, which under pull_request is GitHub's merge commit, confirmed in this PR's own CI: git checkout --progress --force refs/remotes/pull/4/merge -> HEAD is now at 58f2898 Merge 36082f9 into d22dd026. That tree already carries the base branch through base.sha, so comparing it to the merge base would attribute the base branch's own changes to the PR, the same class of false finding in the opposite direction.

pr mode never had the original bug for that reason, and is restored to BASE_SHA. other (push) mode is untouched and never reaches the helper.

Checks

test_parser.py (105 assertions), the action.yml contract check, and shellcheck --severity=warning all pass locally. Both helper paths were exercised against the live API: the happy path returns the merge base, and a 404 warns, rejects gh's error body (which it writes to stdout), and falls back to the tip.

Known gap: no regression test

The dogfood-pr / dogfood-prt jobs point at tests/fixtures/stable/*, which by design never change, so they cannot catch a stale-base inversion in either direction. That is precisely why the first commit's pr-mode regression passed CI green. A real test needs a fixture repository with a deliberately-behind branch; that is infrastructure rather than a line of Python, so it is not in this PR. The failure mode is loud (the gate over-reports and blocks a PR) rather than silent, which bounds the cost.

Follow-ups, not in this PR

  1. velocity-exchange/dependency-gate's default branch carries the same bug and its newest tag is v2.0.0. This repo's absorbed copy is now ahead of it.
  2. velocity-v1 still pins velocity-exchange/dependency-gate@e039f99f (v2.0.0). It needs repointing at this copy, or the standalone repo needs the same fix and a v2.0.1.
  3. dependency-gate/README.md:55 and :83 still tell consumers uses: velocity-exchange/dependency-gate@v2, which is no longer where this copy lives. README.md:297-298 cites .github/workflows/dependency-gate.yml and tests/fixtures/Cargo.lock; the real paths are dependency-gate-selfgate.yml and dependency-gate/tests/fixtures/Cargo.lock. Same stale reference at dependency-gate-test.yml:107. Left alone because the right uses: line depends on follow-up 2.
  4. velocity-v1#511 is unblocked by rebasing onto master. Its two findings are artifacts; deps-reviewed would be signing off on nothing.

…anch tip

`github.event.pull_request.base.sha` is the tip of the base branch at event
time, not the merge base. Diffing a pull request's lockfile against the tip
replays every base-branch commit made since the branch forked, inverted: what
the base branch dropped reads as something the pull request added. A branch
that is merely behind gets findings for packages it never touched, and the
only way through is a `deps-reviewed` label signing off on nothing real.

velocity-v1#511 hit this. It changes no `bun.lock`, but was five commits behind
master, and master had re-resolved the tree: `@types/protobufjs` disappeared
from the lockfile and the hoisted `@velocity-exchange/common/@velocity-exchange/sdk`
entry collapsed. Against the tip those two read as "new package" and "source
changed". Running the parser over the real blobs reproduces the run's findings
and its digest exactly with base = tip, and reports nothing with base = merge
base.

Resolve the merge base through the compare API rather than `git merge-base`.
The head commit is not in the checkout on a fork pull request -- actions/checkout
fetches refs/heads/* and refs/tags/* of the base repository only -- so
`git merge-base` would fail exactly where the gate matters most. The API needs
nothing fetched and only the `contents: read` the action already requires.

The merge base is an ancestor of base.sha, so it is still base-branch content:
the "never trust pull request content" invariant is unchanged. The existing tip
candidates stay on as fallbacks behind it, so a shallow checkout or a rewritten
base degrades to the old over-reporting behaviour instead of failing the run.

Both `prt` and `pr` modes had the bug and both are fixed.
…the API call

Review found that the previous commit was right for `prt` and wrong for `pr`.

The two modes read the head lockfiles from different places. `prt` reads them
through the contents API at `HEAD_SHA`, a real commit on the pull request's
branch, so the merge base is the correct other side. `pr` reads them off the
workspace, which under `pull_request` is GitHub's merge commit
(`refs/pull/N/merge`, "Merge <head> into <base tip>"). That tree already carries
the base branch through `base.sha`, so comparing it to the merge base attributes
the base branch's own dependency changes to the pull request -- the same class of
false finding this branch set out to remove, in the opposite direction.

`pr` mode never had the bug for that reason, and is restored to `BASE_SHA`.

Also harden the helper, both from the same review:

- Validate the compare API's answer with the same regex as its inputs. `gh api`
  writes its error body to *stdout* and exits 1, and `--jq` yields "null" for a
  missing field, so `mb` could hold a non-empty non-SHA and the `${mb:-}` guard
  would never fire. `git rev-parse --verify --quiet` rejected those anyway, so
  this changes no behaviour; it stops the one externally-sourced string in the
  file reaching git unchecked.
- Warn when the API call fails instead of degrading silently. The fallback still
  gates, but it over-reports on a branch that is behind, and over-reporting
  changes the digest, which deletes a sign-off label that was correctly applied.
  A network blip should not do that without saying so.

Document the residual behaviour change under "Known limits": a package the target
branch removed after the fork point, which the branch still carries, is no longer
reported. That is what makes the gate agree with the three-dot diff a reviewer
reads, and the removal wins on merge in any case.
@ChesterSim
ChesterSim merged commit 59625d7 into master Sep 15, 2026
11 checks passed
ChesterSim added a commit to velocity-exchange/velocity-v1 that referenced this pull request Sep 15, 2026
Picks up the merge-base fix. The gate compared each pull request against the
base branch tip rather than the merge base, so a branch that was merely behind
got findings for dependencies it never touched, and the only way through was a
deps-reviewed label signing off on nothing real.

velocity-exchange/shared-workflows#4
ChesterSim added a commit to velocity-exchange/velocity-docs that referenced this pull request Sep 15, 2026
Picks up the merge-base fix. The gate compared each pull request against the
base branch tip rather than the merge base, so a branch that was merely behind
got findings for dependencies it never touched, and the only way through was a
deps-reviewed label signing off on nothing real.

velocity-exchange/shared-workflows#4
ChesterSim added a commit to velocity-exchange/velocity-common that referenced this pull request Sep 15, 2026
Picks up the merge-base fix. The gate compared each pull request against the
base branch tip rather than the merge base, so a branch that was merely behind
got findings for dependencies it never touched, and the only way through was a
deps-reviewed label signing off on nothing real.

velocity-exchange/shared-workflows#4
ChesterSim added a commit to velocity-exchange/velocity-docs that referenced this pull request Sep 15, 2026
Picks up the merge-base fix. The gate compared each pull request against the
base branch tip rather than the merge base, so a branch that was merely behind
got findings for dependencies it never touched, and the only way through was a
deps-reviewed label signing off on nothing real.

velocity-exchange/shared-workflows#4
ChesterSim added a commit to velocity-exchange/velocity-v1 that referenced this pull request Sep 15, 2026
Picks up the merge-base fix. The gate compared each pull request against the
base branch tip rather than the merge base, so a branch that was merely behind
got findings for dependencies it never touched, and the only way through was a
deps-reviewed label signing off on nothing real.

velocity-exchange/shared-workflows#4
ChesterSim added a commit to velocity-exchange/velocity-common that referenced this pull request Sep 15, 2026
Picks up the merge-base fix. The gate compared each pull request against the
base branch tip rather than the merge base, so a branch that was merely behind
got findings for dependencies it never touched, and the only way through was a
deps-reviewed label signing off on nothing real.

velocity-exchange/shared-workflows#4
ChesterSim added a commit to velocity-exchange/velocity-docs that referenced this pull request Nov 3, 2026
* chore: refresh Velocity branding (#126)

* chore: refresh Velocity branding

* fix: use dark logo on docs mobile

* fix: prevent duplicate docs logo

* fix: enforce docs logo breakpoints

* refactor: simplify logo component structure and improve breakpoint handling

---------

Co-authored-by: csadminlc <chestersimadmin@lumencollective.xyz>
Co-authored-by: Noah Prince <chewinglass123@gmail.com>
Co-authored-by: Noah Prince <83885631+ChewingGlass@users.noreply.github.com>
Co-authored-by: 0xahzam <104062587+0xahzam@users.noreply.github.com>

* chore(ci): update dependency-gate to v2.0.1 (#132)

Picks up the merge-base fix. The gate compared each pull request against the
base branch tip rather than the merge base, so a branch that was merely behind
got findings for dependencies it never touched, and the only way through was a
deps-reviewed label signing off on nothing real.

velocity-exchange/shared-workflows#4

* docs: pause bug bounty payouts during audit (#135)

Adds a banner to the bug bounty page saying no bounties are paid while an
audit is in progress. Reports are still accepted at the security address.

---------

Co-authored-by: csadminlc <chestersimadmin@lumencollective.xyz>
Co-authored-by: Noah Prince <chewinglass123@gmail.com>
Co-authored-by: Noah Prince <83885631+ChewingGlass@users.noreply.github.com>
Co-authored-by: 0xahzam <104062587+0xahzam@users.noreply.github.com>
ChesterSim added a commit to velocity-exchange/velocity-docs that referenced this pull request Nov 6, 2026
* chore: refresh Velocity branding (#126)

* chore: refresh Velocity branding

* fix: use dark logo on docs mobile

* fix: prevent duplicate docs logo

* fix: enforce docs logo breakpoints

* refactor: simplify logo component structure and improve breakpoint handling

---------

Co-authored-by: csadminlc <chestersimadmin@lumencollective.xyz>
Co-authored-by: Noah Prince <chewinglass123@gmail.com>
Co-authored-by: Noah Prince <83885631+ChewingGlass@users.noreply.github.com>
Co-authored-by: 0xahzam <104062587+0xahzam@users.noreply.github.com>

* chore(ci): update dependency-gate to v2.0.1 (#132)

Picks up the merge-base fix. The gate compared each pull request against the
base branch tip rather than the merge base, so a branch that was merely behind
got findings for dependencies it never touched, and the only way through was a
deps-reviewed label signing off on nothing real.

velocity-exchange/shared-workflows#4

* docs: pause bug bounty payouts during audit (#135)

Adds a banner to the bug bounty page saying no bounties are paid while an
audit is in progress. Reports are still accepted at the security address.

* docs: remove Velocity Vaults from the developer docs (#137)

Drop the vault-managers and vault-depositors sections, the vaults program
address, and the vaults-sdk and vaults program references. Spot-market and
insurance-fund vault wording is unchanged.

---------

Co-authored-by: csadminlc <chestersimadmin@lumencollective.xyz>
Co-authored-by: Noah Prince <chewinglass123@gmail.com>
Co-authored-by: Noah Prince <83885631+ChewingGlass@users.noreply.github.com>
Co-authored-by: 0xahzam <104062587+0xahzam@users.noreply.github.com>
ChesterSim added a commit to velocity-exchange/velocity-docs that referenced this pull request Nov 17, 2026
* chore: refresh Velocity branding (#126)

* chore: refresh Velocity branding

* fix: use dark logo on docs mobile

* fix: prevent duplicate docs logo

* fix: enforce docs logo breakpoints

* refactor: simplify logo component structure and improve breakpoint handling

---------

Co-authored-by: csadminlc <chestersimadmin@lumencollective.xyz>
Co-authored-by: Noah Prince <chewinglass123@gmail.com>
Co-authored-by: Noah Prince <83885631+ChewingGlass@users.noreply.github.com>
Co-authored-by: 0xahzam <104062587+0xahzam@users.noreply.github.com>

* chore(ci): update dependency-gate to v2.0.1 (#132)

Picks up the merge-base fix. The gate compared each pull request against the
base branch tip rather than the merge base, so a branch that was merely behind
got findings for dependencies it never touched, and the only way through was a
deps-reviewed label signing off on nothing real.

velocity-exchange/shared-workflows#4

* docs: pause bug bounty payouts during audit (#135)

Adds a banner to the bug bounty page saying no bounties are paid while an
audit is in progress. Reports are still accepted at the security address.

* docs: remove Velocity Vaults from the developer docs (#137)

Drop the vault-managers and vault-depositors sections, the vaults program
address, and the vaults-sdk and vaults program references. Spot-market and
insurance-fund vault wording is unchanged.

* docs: replace Terms of Use with updated July 2026 text (#139)

---------

Co-authored-by: csadminlc <chestersimadmin@lumencollective.xyz>
Co-authored-by: Noah Prince <chewinglass123@gmail.com>
Co-authored-by: Noah Prince <83885631+ChewingGlass@users.noreply.github.com>
Co-authored-by: 0xahzam <104062587+0xahzam@users.noreply.github.com>
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.

2 participants