Limit the size of project and group variable values

What does this MR do and why?

Adds a value size limit to project variables (Ci::Variable) and group variables (Ci::GroupVariable). An oversized env_var value cannot reach the job at all, and today nothing stops one being saved.

Both models reuse the shared ci_variable_value_size and ci_variable_file_value_size plan limit columns from !253433 (merged), so there is no migration, no new admin setting and no API change. A limit of 0 is not enforced.

The check lives in a new Ci::SizeLimitedVariable concern that both models include.

Pipeline and job variables read the same columns, so each type here has its own beta flag: ci_project_variable_limits and ci_group_variable_limits. Both are enabled by default, with the root namespace as the actor. GitLab.com can roll each type out and back without touching the columns, and GitLab Self-Managed and GitLab Dedicated enforce as soon as an administrator sets the limits.

For a root namespace with ci_pipeline_variable_limits_override enabled, a file type value gets the 10 MiB limit, the same as pipeline variables. The flags and the override are read only for a value over the plan limit.

Each value over the plan limit is logged once per record to application_json.log, with over_limit, rejected and override fields, so the rollout can be watched while a flag is off. The key and the value are not logged.

Existing data keeps working. The check runs only when the value or the variable type changes, so a stored oversized value stays usable until someone edits it.

Work items: https://gitlab.com/gitlab-org/gitlab/-/work_items/613420 and https://gitlab.com/gitlab-org/gitlab/-/work_items/613419. The two models are near copies, so they are delivered together.

Count limits are out of scope. project_ci_variables and group_ci_variables already exist and are unchanged here. !270929 (merged) puts the count check for each type behind the same flag, so the flag names do not say "size".

Why an oversized value breaks the job

Project and group variables reach the runner through the same path as pipeline variables. Gitlab::Ci::Variables::Builder#user_defined_variables collects instance, group, project, pipeline, schedule and job variables into one Collection, and Ci::BuildRunnerPresenter#runner_variables serializes them identically.

Linux caps a single environment variable at MAX_ARG_STRLEN, which is 32 * PAGE_SIZE, so 131,072 bytes on a 4 KiB page kernel. Above that the job cannot start. With the Docker executor the container never comes up and the whole job log is exec /usr/bin/bash: argument list too long. Neither executor names the variable responsible.

The page size belongs to the runner host, so the application cannot detect the right value. That is why this is a configurable plan limit rather than a constant. The full reasoning and verification are on https://gitlab.com/gitlab-org/gitlab/-/work_items/613416, which owns the column.

A group variable is inherited by every project under the group, so one oversized value there can break jobs across many projects.

File type values are written to a file by the runner and never enter the environment, so they get the separate, larger limit.

Why a model validation is safe here

Every write path for these two models saves through ActiveRecord with validations on:

  • the project and group CI/CD settings forms, through Ci::ChangeVariablesService
  • POST and PUT on /projects/:id/variables and /groups/:id/variables, through Ci::ChangeVariableService

The settings forms are Vue apps whose Apollo mutations are local resolvers calling those controllers over REST, so there is no server side GraphQL mutation to cover. Nothing bulk inserts these rows, and project import and export does not carry ci_variables.

Job variables could not use a model validation (https://gitlab.com/gitlab-org/gitlab/-/work_items/613417), because the retry path bulk inserts with validations off and a validation error on the play path becomes a silent retry that drops the variables. Neither applies here.

attr_encrypted defines value_changed? on the decrypted value, so a settings form resubmitting every variable unchanged does not trip the check.

Rollout

  • GitLab Self-Managed and GitLab Dedicated: nothing is enforced on upgrade, because both columns default to 0 and there is no seed migration.
  • GitLab.com: the columns get values there for pipeline variables. Both flags must be false on staging and production before this MR merges, or the check is enforced as soon as both the code and the columns are in place.
  • Each type then rolls out with chatops from its rollout issue: #639852 for project variables and #639853 for group variables.

Screenshots or screen recordings

No UI change. The error surfaces through the existing validation error path on the settings form and the REST API.

How to set up and validate locally

  1. Set a small limit:

    Plan.default.actual_limits.update!(ci_variable_value_size: 10)
  2. In a project, go to Settings > CI/CD > Variables and add a variable with a value longer than 10 bytes. The save fails with an error naming the variable and the limit.

  3. Add a variable of 10 bytes or fewer. It saves.

  4. Disable the flag with Feature.disable(:ci_project_variable_limits) and confirm the long value saves. log/application_json.log shows the value with "rejected":false. Enable it again with Feature.enable(:ci_project_variable_limits).

  5. Set the limit back to 0 and confirm the long value saves.

  6. Repeat on a group at Settings > CI/CD > Variables, using ci_group_variable_limits for the flag step.

  7. For grandfathering, create a variable with a long value while the limit is 0, set the limit to 10, then change the variable description. The save succeeds. Changing the value fails.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Oleg Yakovenko

Merge request reports

Loading
Loading