From bc9eefe4645a2b5f65e0c9d3351dcd805bee5b65 Mon Sep 17 00:00:00 2001 From: VIDEORIP APP Date: Sat, 24 Oct 2026 20:48:43 +0300 Subject: [PATCH 1/2] Parse multi-document YAML manifests Co-authored-by: ruslanty --- src/Infrastructure/Parser/YamlImageParser.php | 58 +++++++++++++------ 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/src/Infrastructure/Parser/YamlImageParser.php b/src/Infrastructure/Parser/YamlImageParser.php index 84575b3..1e12179 100644 --- a/src/Infrastructure/Parser/YamlImageParser.php +++ b/src/Infrastructure/Parser/YamlImageParser.php @@ -7,6 +7,7 @@ use PhpRuntimeAudit\Application\ManifestFile; use PhpRuntimeAudit\Application\ManifestParser; use PhpRuntimeAudit\Domain\ImageReference; +use RuntimeException; use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Yaml; @@ -24,31 +25,50 @@ public function supports(string $path): bool public function parse(ManifestFile $file): array { - try { - $document = Yaml::parse($file->contents); - } catch (ParseException $parseException) { - throw new ParseException( - sprintf('Invalid YAML in %s: %s', $file->path, $parseException->getMessage()), - $parseException->getParsedLine(), - $parseException->getSnippet(), - $file->path, - $parseException, - ); - } - $references = []; $usedPositions = []; - foreach ($this->imageValues($document) as $image) { - if (! $this->matcher->matches($image) - && preg_match('~\$(?:[A-Za-z_{])~', $image) !== 1 - ) { - continue; + $documents = preg_split( + '~(?m)^---(?:[ \t]+(?=[^# \t\r\n])|[ \t]+(?:\#[^\r\n]*)?(?:\R|\z)|(?:\R|\z))~', + $file->contents, + -1, + PREG_SPLIT_OFFSET_CAPTURE, + ); + + if ($documents === false) { + throw new RuntimeException(sprintf('Could not split YAML documents in %s.', $file->path)); + } + + foreach ($documents as [$contents, $offset]) { + $lineOffset = substr_count($file->contents, chr(10), 0, $offset); + + try { + $document = Yaml::parse($contents); + } catch (ParseException $parseException) { + $parsedLine = $parseException->getParsedLine(); + + throw new ParseException( + sprintf('Invalid YAML in %s: %s', $file->path, $parseException->getMessage()), + $parsedLine > 0 + ? $parsedLine + $lineOffset + : $parsedLine, + $parseException->getSnippet(), + $file->path, + $parseException, + ); } - $line = $this->sourceLineLocator->locate($file->contents, $image, $usedPositions); + foreach ($this->imageValues($document) as $image) { + if (! $this->matcher->matches($image) + && preg_match('~\$(?:[A-Za-z_{])~', $image) !== 1 + ) { + continue; + } - $references[] = new ImageReference($file->path, $line, $image); + $line = $this->sourceLineLocator->locate($file->contents, $image, $usedPositions); + + $references[] = new ImageReference($file->path, $line, $image); + } } return $references; From 585a0801cf35859b8a2c18a20e8ee12fbb1827f6 Mon Sep 17 00:00:00 2001 From: Ruslan <56517603+ruslanty@users.noreply.github.com> Date: Sat, 24 Oct 2026 20:52:57 +0300 Subject: [PATCH 2/2] Parse multi-document YAML manifests --- README.md | 4 +- tests/Infrastructure/YamlImageParserTest.php | 66 ++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b8c03bf..cd20105 100644 --- a/README.md +++ b/README.md @@ -58,8 +58,8 @@ potentially vulnerable result. ## Scope and limits The catalog uses the [PHP security advisory](https://www.php.net/archive/2026.php#2026-10-21-5). -The scanner checks Dockerfile `FROM` entries and `image` fields in -single-document YAML files. A `FROM` entry that uses a build argument is +The scanner checks Dockerfile `FROM` entries and `image` fields across +multi-document YAML files. A `FROM` entry that uses a build argument is reported as `unknown`, even when the argument has a default: `docker build` can override that default. Explicit version tags are assessed for `php` and `docker.io/library/php`; digest-pinned references are `unknown` because the diff --git a/tests/Infrastructure/YamlImageParserTest.php b/tests/Infrastructure/YamlImageParserTest.php index 1ad18a6..a75b245 100644 --- a/tests/Infrastructure/YamlImageParserTest.php +++ b/tests/Infrastructure/YamlImageParserTest.php @@ -28,6 +28,72 @@ public function testItFindsImagesInYamlManifestsWithSourceLines(): void self::assertSame(3, $references[0]->line); } + public function testItFindsImagesAcrossMultipleYamlDocumentsWithSourceLines(): void + { + $parser = new YamlImageParser(); + $file = new ManifestFile('application.yaml', implode(PHP_EOL, [ + '---', + 'services:', + ' first:', + ' image: php:8.4.26-fpm', + '---', + 'services:', + ' second:', + ' image: php:8.5.11-fpm', + ])); + + $references = $parser->parse($file); + + self::assertCount(2, $references); + self::assertSame( + [ + 'php:8.4.26-fpm', + 'php:8.5.11-fpm', + ], + array_map( + static fn(ImageReference $reference): string => $reference->image, + $references, + ) + ); + self::assertSame( + [4, 8], + array_map( + static fn(ImageReference $reference): ?int => $reference->line, + $references, + ) + ); + } + + public function testItFindsImagesAcrossDocumentsWithInlineStartMarkers(): void + { + $parser = new YamlImageParser(); + $file = new ManifestFile('application.yaml', implode(PHP_EOL, [ + '--- {services: {first: {image: php:8.4.26-fpm}}}', + '--- {services: {second: {image: php:8.5.11-fpm}}}', + ])); + + $references = $parser->parse($file); + + self::assertCount(2, $references); + self::assertSame( + [ + 'php:8.4.26-fpm', + 'php:8.5.11-fpm', + ], + array_map( + static fn(ImageReference $reference): string => $reference->image, + $references, + ) + ); + self::assertSame( + [1, 2], + array_map( + static fn(ImageReference $reference): ?int => $reference->line, + $references, + ) + ); + } + public function testItFindsImagesInYamlFlowMappings(): void { $parser = new YamlImageParser();