Fix group transfer failing and wedging on 2FA

What does this MR do and why?

Fixes two bugs that together leave a failed group transfer unable to cancel.

  1. The 2FA check reads the wrong group. Groups::TransferService reads "Allow more restrictive 2FA enforcement for subgroups" from the immediate new parent, but the Group validation reads it from the top-level ancestor. When the new parent is a subgroup, its column has the default true, so the service keeps the group's 2FA requirement and save! fails. The service now reads the setting from the new parent's root ancestor, the same group the validation reads.

  2. cancel_transfer! runs on the failed transfer's in-memory group. When the service fails before Group.transaction commits, the transaction rolls back, but the changes stay on the in-memory group, such as the new parent_id. The worker then calls cancel_transfer! on that object, and the state machine saves the whole record:

    • If the changes are invalid, the cancel fails with StateMachines::InvalidTransition, and the job retries until it dies.
    • If they are valid, the cancel saves them, such as the new parent_id, without the rest of the transfer. The rest includes the project authorization refresh.

    The worker now reloads the group and clears its memoized root_ancestor before cancelling, in both the else and rescue paths. The memo matters too: after a validation failure in save!, it still points at the new parent's root.

    This MR doesn't change failures after Group.transaction commits. By then the new parent_id is saved, so the reload keeps it, and the cancel only resets the transfer state, as before.

There's no feature flag. These are small fixes to failure paths, and both restore the intended behavior.

Customer impact: severity1. A customer's group transfer failed this way, and their work is blocked until the group can be moved. The workaround is to transfer into the destination top-level group first, then into the subgroup. This fix stops it from happening again.

The fix was first reviewed as a security MR. AppSec triaged it as a functional bug, not a vulnerability, so it moved here.

References

Resolves https://gitlab.com/gitlab-org/gitlab/-/work_items/639600

Screenshots or screen recordings

Not applicable. There is no UI change.

How to set up and validate locally

  1. Create a top-level group r. In Settings > General > Permissions and group features, turn off Allow more restrictive 2FA enforcement for subgroups.
  2. Create a subgroup r/s.
  3. Create a top-level group g, and turn on Require all users in this group to set up 2FA.
  4. In g, go to Settings > General > Advanced > Transfer group, and transfer it to r/s.
  5. On master, the transfer fails, and log/sidekiq.log shows StateMachines::InvalidTransition from cancel_transfer. With this branch, g moves under r/s, and its 2FA requirement is turned off.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

🤖 Generated with Claude Code

Merge request reports

Loading
Loading