Add setting to reject a git push that updates too many refs

What does this MR do and why?

Add a setting for a maximum number of refs that can be in one push. The push is rejected if the maximum is exceeded.

References

related to: https://gitlab.com/gitlab-org/gitlab/-/work_items/636142

How to set up and validate locally

1. Migration up and down

bundle exec rails db:migrate
bundle exec rails db:rollback STEP=1
bundle exec rails db:migrate
  • Up adds application_settings.git_push_settings as jsonb, NOT NULL, default {}, with the check_application_settings_git_push_settings_is_hash constraint.
  • Down removes both cleanly.

2. Default is unlimited

curl -s --header "PRIVATE-TOKEN: $TOKEN" "$URL/api/v4/application/settings" \
  | jq .max_refs_per_push
  • Returns 0.
  • Pushing several branches succeeds, with no limit message and no log line. Nothing changes until an administrator sets a value.

3. Setting round-trip

curl -s --request PUT --header "PRIVATE-TOKEN: $TOKEN" \
  "$URL/api/v4/application/settings?max_refs_per_push=10" | jq .max_refs_per_push
  • Returns 10, and a follow-up GET still returns 10.
  • Admin → Settings → Network → Performance optimization shows Maximum number of changes (branches or tags) in a single push above which the push is rejected with the value 10. Changing it there and saving persists, and the new value is visible over the API.
  • The field rejects a negative number (min: 0).

4. Over the limit, enforcement disabled: logged only

git_push_ref_limit_enforce is default-off, so start here.

for i in $(seq 1 12); do git branch ref-limit-test-$i HEAD; done
git push origin 'refs/heads/ref-limit-test-*:refs/heads/ref-limit-test-*'
  • The push succeeds — shadow mode does not reject.
  • log/application_json.log has one entry with "message": "Push exceeds the ref limit but was not rejected because enforcement is disabled", "ref_limit": 10 and "total_ref_count": 12.
tail -n 200 log/application_json.log | grep -F 'Push exceeds the ref limit' | jq .

5. Over the limit, enforcement enabled: rejected

bundle exec rails runner 'Feature.enable(:git_push_ref_limit_enforce)'

Delete the test branches on the remote first (see cleanup), then repeat the push from step 4.

  • The push is rejected with remote: GitLab: This push updates 12 refs, more than the limit of 10. Push the refs in smaller batches.
  • No branches were created — the push is refused before any per-ref work.
  • The rejection is immediate, not after the 50 second pre-receive timeout.
  • Same result over both SSH and HTTP.

6. At the limit still succeeds

With the limit still 10 and the flag still enabled, push exactly 10 refs.

  • Succeeds. The check is changes.length <= limit, so 10 is allowed and 11 is not.

7. Deletes count toward the limit

git push origin --delete ref-limit-test-1 ... ref-limit-test-12
  • A single delete push of more than the limit is rejected the same way. Deletes are ref updates, so they are counted. Delete in batches at or under the limit.

8. Back to unlimited

curl -s --request PUT --header "PRIVATE-TOKEN: $TOKEN" \
  "$URL/api/v4/application/settings?max_refs_per_push=0" | jq .max_refs_per_push
  • Returns 0, and an oversized push succeeds again with the flag still enabled. 0 means unlimited regardless of the flag.

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.

Related to #636142

Merge request reports

Loading
Loading