Stale security policy links block protected branches after transfer

Summary

A project that inherits security policies from a group keeps a Security::PolicyProjectLink row per linked policy configuration. Before GitLab 18.1 (project transfers) and 18.3 (group transfers), transferring a project or group did not remove these links. Projects transferred before those versions shipped are still carrying links to their old group's policy configuration.

The practical effect: a transferred project keeps enforcing merge request approval policies from a group it no longer belongs to. If one of those stale policies sets block_branch_modification: true, owners can no longer update or delete protected branches on the project, and there is no supported way to lift it — editing, disabling, or deleting policies in the project's current policy project does nothing, because the policy actually blocking them lives in the old group's configuration, which is no longer visible from the project.

On GitLab.com, the affected set is on the order of a thousand projects.

Steps to reproduce

  1. Create group A with a security policy project containing a merge request approval policy that has approval_settings.block_branch_modification: true.
  2. Create a project inside group A. It inherits the policy and a Security::PolicyProjectLink row is created.
  3. Transfer the project to group B, on a GitLab version before 18.1 (or simulate this on a current version by leaving the old Security::PolicyProjectLink row in place after a manual transfer).
  4. As an owner of the project (now in group B), try to update or delete one of its protected branches, via the API or the UI.

What is the current bug behavior?

  • API: PATCH/DELETE on the protected branch returns 403.
  • UI: "Unprotect" is disabled, with the message "branch protection is enforced by one or more security policies."
  • Creating a new protected branch still works — only modification and deletion are blocked.
  • The block persists regardless of what's configured in the project's current policy project, because the enforcing policy belongs to group A's configuration, not group B's.
  • Calling the resyncSecurityPolicies GraphQL mutation with relationship: INHERITED does not fix it: it only re-syncs configurations that currently apply to the project, so it never removes a link to a configuration that no longer applies.
  • Disabling the stale policy also does not lift the block, because the check that decides whether to block a protected branch update/delete does not filter out disabled policies (unlike the equivalent checks for pushes and for group-level protected branches, which both do).

What is the expected correct behavior?

  • A project's protected branch enforcement should only reflect policies from security policy configurations that currently apply to it.
  • Stale links left behind by transfers that predate the transfer cleanup workers should be reconcilable without manual database intervention.
  • A disabled policy should never block a protected branch change, consistently across all protected branch enforcement paths.

Possible fixes

  • Extend the existing resync path so that relationship: INHERITED also reconciles group-transfer links (link configurations that now apply, unlink configurations that no longer apply), not just re-sync currently-applicable ones.
  • Harden protected branch enforcement (both the push check and the update/delete check) to only consider policies from configurations that currently apply to the project, and to ignore disabled policies, so stale or disabled policies can't block changes even before cleanup runs.
Edited by Alan (Maciej) Paruszewski