Skip to content

fix(db): extend the out-of-order webhook guard to labels (PRs + issues) and resync on label-only drift - #8812

Merged
JSONbored merged 1 commit into
mainfrom
fix/labels-reorder-guard
Jul 26, 2026
Merged

fix(db): extend the out-of-order webhook guard to labels (PRs + issues) and resync on label-only drift#8812
JSONbored merged 1 commit into
mainfrom
fix/labels-reorder-guard

Conversation

@JSONbored

Copy link
Copy Markdown
Owner

Summary

Round-2 audit fix (#8804): label staleness could break the flag-then-close two-pass state machine in three compounding ways — a reordered webhook wiping a just-applied pending-closure/manual-review label (the reorder guard protected state/headSha/mergedAt but wrote labelsJson unconditionally), issues having no reorder protection at all, and the sweep's live resync discarding label-only drift.

Changes

  • src/db/repositories.tslabelsJson joins the stale-guarded resolved set in upsertPullRequestFromGitHub (return value reflects it, per the function's own persisted-state contract); upsertIssueFromGitHub gains the full guard (state + labels) with the identical fail-open contract — sparse payloads and pre-migration rows apply the write exactly as before. Dead ?? fallbacks avoided (the column is NOT NULL).
  • migrations/0175_issues_github_updated_at.sql + schema — the issues twin of 0172's column; NULL for existing rows, self-backfilling on next sync.
  • src/queue/processors.tsreReviewStoredPullRequest resyncs on a sorted-set label mismatch too, not just head drift, so the live labels the sweep already fetched are persisted instead of discarded (Pass 2 of the flag-then-close machine reads a current label set).

Test plan

  • npm run typecheck — clean
  • 5 new db tests (PR label preserve on stale + newer-applies-removal; issue guard regression + newer-applies + double fail-open) extending the existing #webhook-reorder-clobber describe
  • New sweep test: label-only drift (same head) persists the live labels; the existing no-drift test now also pins the absent-live-labels arm
  • 790 tests green across the six db/queue suites
  • Changed hunks: 0 uncovered statements/branches

Closes #8804

…s) and resync on label-only drift (#8804)

Three compounding staleness gaps around the pending-closure two-pass machine:

- pull_requests: the #webhook-reorder-clobber guard protected state/headSha/
  mergedAt but labelsJson was written unconditionally — a reordered stale
  snapshot silently wiped a JUST-applied disposition label (pending-closure,
  manual-review) even while the protected trio stayed correct. labelsJson
  now joins the resolved set, and the returned record reflects it.
- issues: upsertIssueFromGitHub had no reorder protection at all. New
  github_updated_at column (migration 0175, mirrors 0172) + the same
  JS-side stale resolution for state and labels, same fail-open contract
  (sparse payload / pre-migration row applies the write unchanged).
- reReviewStoredPullRequest resynced the stored row only on HEAD drift,
  discarding the live fetch's labels on a label-only change — exactly what
  Pass 1 of flag-then-close produces — so Pass 2 could misread the stale
  stored label set and re-run Pass 1 (duplicate warning, delayed
  enforcement). The resync now also fires on a sorted-set label mismatch.
@superagent-security

Copy link
Copy Markdown
Contributor

Superagent didn't find any vulnerabilities or security issues in this PR.

@JSONbored JSONbored self-assigned this Jul 26, 2026
@JSONbored
JSONbored merged commit 8d8716f into main Jul 26, 2026
4 checks passed
@JSONbored
JSONbored deleted the fix/labels-reorder-guard branch July 26, 2026 07:25
@codecov

codecov Bot commented Jul 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.81%. Comparing base (11fb8ee) to head (bfa3323).
⚠️ Report is 5 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #8812   +/-   ##
=======================================
  Coverage   93.81%   93.81%           
=======================================
  Files         797      797           
  Lines       79608    79619   +11     
  Branches    24127    24134    +7     
=======================================
+ Hits        74686    74697   +11     
  Misses       3556     3556           
  Partials     1366     1366           
Flag Coverage Δ
backend 95.10% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/db/repositories.ts 96.74% <100.00%> (+0.01%) ⬆️
src/db/schema.ts 73.11% <ø> (ø)
src/queue/processors.ts 95.69% <100.00%> (+<0.01%) ⬆️

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.

fix(db): labelsJson bypasses the out-of-order webhook guard, breaking the pending-closure two-pass machine under reordering

1 participant