Limit the size of Duo context exclusion rules

What does this MR do and why?

A project's Duo context exclusion rules have no size limit. This MR caps them at 64 KiB, the same limit the other JSON settings on Ai::NamespaceSetting and ApplicationSetting use, behind the duo_context_exclusion_settings_size_limit flag. The rules are only validated when they change, so a project that already stores more can still save its other settings. The flag is removed in !265923 (merged), without a rollout.

Why these numbers

Production today (Database Lab, project_settings on main, 2026-10-28):

Projects with at least one rule 290
Largest stored rules 21 KiB
Most rules in one project 863 (p99: 95)
Longest single rule 252 characters
Projects over 64 KiB 0

So 64 KiB is three times the largest real value, and enabling the flag affects no project on GitLab.com.

  • The limit is the JSON-serialised size of the whole setting, so it bounds both the number of rules and their length. I left maxItems and maxLength out of the JSON schema: the schema can't be switched with a flag, and the byte limit already covers both.
  • if: :will_save_change_to_duo_context_exclusion_settings? also stops the schema from being re-validated on unrelated saves. The stored value was validated when it was written. The spec "does not block saving other settings when the stored rules are already over the limit" fails without it.
  • The flag uses the :instance actor because JsonSchemaValidator calls a size_limit lambda without the record.
  • Both write paths, the projectSettingsUpdate mutation and the project settings form, save through the same model validation.

References

Closes https://gitlab.com/gitlab-org/gitlab/-/work_items/636070

Screenshots or screen recordings

No UI change. Saving rules over 64 KiB while the flag is on returns this error:

Before After
Saved Duo context exclusion settings is too large. Maximum size allowed is 64 KiB

How to set up and validate locally

Steps and what I got

Run against GDK with Duo enabled, as root (a maintainer of flightjs/Flight).

  1. Create a personal access token with the api scope and set up these helpers:

    export GDK=http://gdk.test:3000 TOKEN=<token>
    
    gql() {
      ruby -rjson -e 'puts({ query: "mutation($i: ProjectSettingsUpdateInput!) { projectSettingsUpdate(input: $i) { errors } }", variables: { i: JSON.parse(ARGV[0]) } }.to_json)' "$1" |
        curl -s "$GDK/api/graphql" -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' --data-binary @-
      echo
    }
    rule() { ruby -rjson -e 'puts({ fullPath: "flightjs/Flight", duoContextExclusionSettings: { exclusionRules: ["a" * ARGV[0].to_i] } }.to_json)' "$1"; }
    stored() {
      ruby -rjson -e 'puts({ query: "{ project(fullPath: \"flightjs/Flight\") { duoContextExclusionSettings { exclusionRules } } }" }.to_json)' |
        curl -s "$GDK/api/graphql" -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' --data-binary @- |
        ruby -rjson -e 'r = JSON.parse($stdin.read).dig("data", "project", "duoContextExclusionSettings", "exclusionRules") || []; puts "stored: #{r.size} rule(s), #{r.sum(&:size)} characters"'
    }
  2. With the flag off, save one 70,000-character rule. It is accepted, as before:

    gql "$(rule 70000)"
    stored

    What I got:

    {"data":{"projectSettingsUpdate":{"errors":[]}}}
    stored: 1 rule(s), 70000 characters
  3. Enable the flag in a Rails console, then wait a minute. The web process caches flags, so a request straight after enabling still sees it off.

    Feature.enable(:duo_context_exclusion_settings_size_limit)
  4. Save an 80,000-character rule. It is rejected, and the stored rules are unchanged:

    gql "$(rule 80000)"
    stored

    What I got:

    {"data":{"projectSettingsUpdate":{"errors":["Duo context exclusion settings is too large. Maximum size allowed is 64 KiB"]}}}
    stored: 1 rule(s), 70000 characters
  5. With the 70,000-character rule still stored, change a different setting. It saves:

    gql '{"fullPath": "flightjs/Flight", "duoFeaturesEnabled": false}'

    What I got:

    {"data":{"projectSettingsUpdate":{"errors":[]}}}
  6. Shrink the rules. That saves too:

    gql "$(rule 100)"
    stored

    What I got:

    {"data":{"projectSettingsUpdate":{"errors":[]}}}
    stored: 1 rule(s), 100 characters
  7. Clean up:

    project = Project.find_by_full_path('flightjs/Flight')
    project.project_setting.update!(duo_context_exclusion_settings: {}, duo_features_enabled: true)
    Feature.remove(:duo_context_exclusion_settings_size_limit)

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.

Edited by Fred de Gier

Merge request reports

Loading
Loading