Add string sanitization and validation to config file lib names/versions

What does this MR do and why?

Background

The Repository X-Ray is a feature that scans a repository for dependency manager configuration files and parses a list of library names/versions from those files. Its core functionality involves the ConfigFiles::Base class, which contains the logic to parse and extract libraries from the file content.

This MR

As part of https://gitlab.com/gitlab-org/gitlab/-/issues/509031, we introduce additional sanitization and validation logic to the parsed library name and version strings, as proposed in https://gitlab.com/gitlab-org/gitlab/-/issues/505590#note_2251707440. This is to mitigate potential malicious strings.

Specifically, in this MR, we:

  • Remove extraneous version qualifiers and the prefixed v from the lib.version value.
  • Apply validation to lib.name with the following rules:
  1. Only contain these characters (no spaces): A-Z, a-z, 0-9, /, -, _, .
  2. Only start/end with A-Z, a-z, or 0-9.
  3. Not exceed 50 characters.
  • Apply validation to lib.version with the following rules:
  1. Only contain: 0-9, , ., <, >, =, +, ^, *, !, |, ,.
  2. Not exceed 20 characters.

References

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

How to set up and validate locally

We can validate that the x-ray parsing service still works as expected and also sanitizes the string values as necessary.

  1. Ensure you have GitLab Duo Code Suggestions enabled on your gdk.
  2. Create a new project. In the project's root, add the following file.

requirements.txt

  lib_with_leading_and_trailing_spaces    !=v0.3.0-prerelease
otherLib == 3.2.1+build-metadata
  1. In the rails console, observe that the expected X-ray report has been saved with the library strings sanitized.
project = Project.last # This should be the new project you created
project.xray_reports

Screenshot_2025-01-02_at_1.24.21_PM

Edited by Leaminn Ma

Merge request reports

Loading