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:
JsonSchemaValidatormeasures the byte size of the compact JSON (Gitlab::Json.dump(spec).bytesize).BuildComponentsServiceaddsinputs_orderbefore it builds the component, soinputs_ordercounts toward the size.Gitlab::Json.dumpwrites<,>, and&as 6-byte escapes, so aspecwith many of them counts as larger than its stored size.- The validator checks the size before the schema, so an oversized
specdoes 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
specpublished on GitLab.com, with that escaping counted, so every existing component can still be published again. - The error message and the docs say
256 KiBbecause GitLab's locale uses binary unit names innumber_to_human_size.
References
- Closes https://gitlab.com/gitlab-org/gitlab/-/work_items/635174
- Limit on the number of inputs, from the same effort: !264013 (merged)
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').executeResult on GDK:
- Release
1.0.0returnsstatus: :errorwith the messageBuild component error: Spec is too large. Maximum size allowed is 256 KiB. No catalog version is created. - Release
1.0.1returnsstatus: :successand 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.