Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 39 additions & 19 deletions src/Infrastructure/Parser/YamlImageParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
66 changes: 66 additions & 0 deletions tests/Infrastructure/YamlImageParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading