Skip to content

Handle malformed YAML in GitLab v2 importer (#2523) - #2525

Open
Gowri213 wants to merge 2 commits into
aboutcode-org:mainfrom
Gowri213:fix-gitlab-yaml-2523
Open

Gowri213 wants to merge 2 commits into
aboutcode-org:mainfrom
Gowri213:fix-gitlab-yaml-2523

Conversation

@Gowri213

Copy link
Copy Markdown

Fixes #2523.

  • Catch yaml.YAMLError when loading YAML files with saneyaml.load() in parse_gitlab_advisory, log the error, and return None so collect_advisories skips the malformed file instead of failing the pipeline.
  • Add a unit test (test_parse_gitlab_advisory_with_malformed_yaml) covering multi-document / malformed YAML files.

@Gowri213
Gowri213 force-pushed the fix-gitlab-yaml-2523 branch from a162daa to 26d6a3b Compare November 24, 2026 12:43
Signed-off-by: Gowri <gowri21115@gmail.com>

@sanchit955 sanchit955 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested this locally against the PR head (26d6a3b): the full test_gitlab_importer_v2.py file passes (9/9), and with the importer change reverted the new test fails with the exact yaml.composer.ComposerError: expected a single document in the stream from #2523, so the test pins the fix. I also probed saneyaml.load directly: a multi-document file raises ComposerError and a plain syntax error raises ParserError, both subclasses of yaml.YAMLError, so the except clause covers both. Returning None fits the existing contract — collect_advisories already logs and skips a falsy return, so one bad file no longer fails the pipeline. black 24.3.0 and isort 5.10.1 (the repo-pinned versions) are clean on both changed files.

Two small follow-ups, neither blocking:

  • #2523 also asks for the malformed advisory to be reported upstream — this PR skips the file (logged at ERROR), so that part still needs someone to file it with the GitLab advisories repo.
  • The test pins the multi-document case; a plain YAML syntax error goes through the same except but has no test pinning it — a second malformed-input case in the test would cover it.

@Jean-Regis-M Jean-Regis-M left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested 26d6a3b: test_gitlab_importer_v2.py passes (9), and with gitlab_importer.py from the parent commit the new test fails with the same ComposerError as #2523, so it pins the crash.

One thing about the file that triggered this, though. npm/reasonix/CVE-2026-102437.yml (still the same on advisories-community main) isn't two advisories. It's one complete advisory with a stray --- as its last line:

>>> docs = list(yaml.safe_load_all(open("CVE-2026-102437.yml")))
>>> len(docs), type(docs[0]).__name__, docs[1]
(2, 'dict', None)
>>> sorted(docs[0])[:6]
['affected_range', 'affected_versions', 'cvss_v3', 'cwe_ids', 'date', 'description']

So with this change the pipeline no longer fails, but it also drops a valid advisory (CVE-2026-102437, CVSS 7.8, CWE-78) every run until upstream fixes the file. Loading with safe_load_all and using the document when exactly one of them is non-empty would import it. Real multi-advisory files, or anything that still fails to parse, would keep the log-and-skip path you added. The test fixture (extra_doc: true as a second document) covers the second case, but not the trailing-separator case that actually happened.

Small things:

  • A skipped file is logged twice: once in parse_gitlab_advisory ("failed to parse YAML") and once in collect_advisories ("Failed to parse advisory from …").
  • Other v2 importers load YAML the same way with no guard (golang_vulndb_importer, istio_importer, oss_fuzz, project_kb_statements_importer, pypa_importer), so one bad upstream file would stop those pipelines too. Probably a separate issue, but worth knowing.

Use yaml.load_all with saneyaml.SaneLoader and ignore empty documents so advisories with a trailing '---' separator are parsed cleanly while multi-document or malformed YAML files still log an error and are skipped.

Fixes aboutcode-org#2523

Signed-off-by: Gowri <gowri21115@gmail.com>
@Gowri213

Copy link
Copy Markdown
Author

@sanchit955 Thanks for testing this locally and for the helpful suggestions!

In 396ceb8, I parameterized test_parse_gitlab_advisory_with_malformed_yaml to cover both the multi-document case and a plain YAML syntax error, alongside a new test (test_parse_gitlab_advisory_with_trailing_separator) ensuring valid advisories with a stray trailing --- are recovered cleanly.

Also, I'd be happy to open an upstream issue/MR on gitlab-org/advisories-community for npm/reasonix/CVE-2026-102437.yml if you'd like me to take care of that part of #2523 as well.

@Gowri213

Copy link
Copy Markdown
Author

@Jean-Regis-M Thanks for the thorough review and for catching that CVE-2026-102437.yml only had a trailing --- separator rather than a second document!

In 396ceb8, I updated parse_gitlab_advisory to load with yaml.load_all(..., Loader=saneyaml.SaneLoader) and ignore empty/None documents (if doc). That way, advisories with a stray trailing --- recover and import cleanly instead of being dropped, while true multi-document files and syntax errors still log an error and return None. I also added test_parse_gitlab_advisory_with_trailing_separator to pin this behavior.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gitlab_importer_v2 pipeline failing due to malformed YAML file

3 participants