Limit the size of a catalog component spec at publish

What does this MR do and why?

A release can hold up to 100 components. Each stored spec was bound only by the 2 MB YAML size limit, so one release could store about 200 MB in catalog_resource_components.spec.

Ci::Catalog::Resources::Component now rejects a spec larger than 256 KiB, through the size_limit option of its json_schema validation. Publishing a release with such a component fails. The worst case for one release drops to 25.6 MB.

Notes for review:

  • JsonSchemaValidator measures the byte size of the compact JSON (Gitlab::Json.dump(spec).bytesize). BuildComponentsService adds inputs_order before it builds the component, so inputs_order counts toward the size.
  • Gitlab::Json.dump writes <, >, and & as 6-byte escapes, so a spec with many of them counts as larger than its stored size.
  • The validator checks the size before the schema, so an oversized spec does not go through JSON schema validation.
  • No existing row is above the limit, so the validation does not block saves of existing components.
  • There is no feature flag. The limit is above every spec published on GitLab.com, with that escaping counted, so every existing component can still be published again.
  • The error message and the docs say 256 KiB because GitLab's locale uses binary unit names in number_to_human_size.

References

Screenshots or screen recordings

Not applicable. This MR has no UI change.

How to set up and validate locally

In the Rails console, publish a component with a spec over the limit, then publish the same component with a smaller spec:

user = User.find_by_username('root')
project = Projects::CreateService.new(
  user,
  name: 'spec-size-check',
  path: 'spec-size-check',
  namespace_id: user.namespace_id,
  description: 'Spec size check'
).execute
branch = project.default_branch_or_main
component = lambda do |size|
  "spec:\n  inputs:\n    environment:\n      description: '#{'a' * size}'\n---\njob:\n  script: echo\n"
end

project.repository.create_file(user, 'README.md', '# Spec size check', message: 'Add README', branch_name: branch)
project.repository.create_file(user, 'templates/big.yml', component.call(300.kilobytes),
  message: 'Add a component with a large spec', branch_name: branch)
Ci::Catalog::Resources::CreateService.new(project, user).execute

Releases::CreateService.new(project, user, tag: '1.0.0', ref: branch, name: '1.0.0').execute

project.repository.update_file(user, 'templates/big.yml', component.call(200.kilobytes),
  message: 'Shrink the spec', branch_name: branch)
Releases::CreateService.new(project, user, tag: '1.0.1', ref: branch, name: '1.0.1').execute

Result on GDK:

  • Release 1.0.0 returns status: :error with the message Build component error: Spec is too large. Maximum size allowed is 256 KiB. No catalog version is created.
  • Release 1.0.1 returns status: :success and creates one catalog version.

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 Oleg Yakovenko

Merge request reports

Loading
Loading