From d524bd5fc39cc5a583b20ec656cd01a9742fce7a Mon Sep 17 00:00:00 2001 From: witchcraze <67056980+witchcraze@users.noreply.github.com> Date: Thu, 12 Jan 2023 03:46:20 +0900 Subject: [PATCH] feat: Add php binary catalogers (#1444) * add php classifier Signed-off-by: witchcraze * make lint-fix Signed-off-by: witchcraze --- syft/pkg/cataloger/binary/cataloger_test.go | 42 +++++++++++++++++++ .../cataloger/binary/default_classifiers.go | 28 +++++++++++++ .../classifiers/positive/.gitignore | 3 +- .../positive/php-apache-8.2.1/libphp.so | 5 +++ .../classifiers/positive/php-cli-8.2.1/php | 5 +++ .../positive/php-fpm-8.2.1/php-fpm | 5 +++ 6 files changed, 87 insertions(+), 1 deletion(-) create mode 100755 syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/php-apache-8.2.1/libphp.so create mode 100755 syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/php-cli-8.2.1/php create mode 100755 syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/php-fpm-8.2.1/php-fpm diff --git a/syft/pkg/cataloger/binary/cataloger_test.go b/syft/pkg/cataloger/binary/cataloger_test.go index 7a5209074..58df64219 100644 --- a/syft/pkg/cataloger/binary/cataloger_test.go +++ b/syft/pkg/cataloger/binary/cataloger_test.go @@ -17,6 +17,48 @@ func TestClassifierCataloger_DefaultClassifiers_PositiveCases(t *testing.T) { fixtureDir string expected pkg.Package }{ + { + name: "positive-php-cli-8.2.1", + fixtureDir: "test-fixtures/classifiers/positive/php-cli-8.2.1", + expected: pkg.Package{ + Name: "php-cli", + Version: "8.2.1", + Type: "binary", + PURL: "pkg:generic/php-cli@8.2.1", + Locations: singleLocation("php"), + Metadata: pkg.BinaryMetadata{ + Classifier: "php-cli-binary", + }, + }, + }, + { + name: "positive-php-fpm-8.2.1", + fixtureDir: "test-fixtures/classifiers/positive/php-fpm-8.2.1", + expected: pkg.Package{ + Name: "php-fpm", + Version: "8.2.1", + Type: "binary", + PURL: "pkg:generic/php-fpm@8.2.1", + Locations: singleLocation("php-fpm"), + Metadata: pkg.BinaryMetadata{ + Classifier: "php-fpm-binary", + }, + }, + }, + { + name: "positive-php-apache-8.2.1", + fixtureDir: "test-fixtures/classifiers/positive/php-apache-8.2.1", + expected: pkg.Package{ + Name: "libphp", + Version: "8.2.1", + Type: "binary", + PURL: "pkg:generic/php@8.2.1", + Locations: singleLocation("libphp.so"), + Metadata: pkg.BinaryMetadata{ + Classifier: "php-apache-binary", + }, + }, + }, { name: "positive-redis-2.8.23", fixtureDir: "test-fixtures/classifiers/positive/redis-server-2.8.23", diff --git a/syft/pkg/cataloger/binary/default_classifiers.go b/syft/pkg/cataloger/binary/default_classifiers.go index 6a3b9b95c..986faebaf 100644 --- a/syft/pkg/cataloger/binary/default_classifiers.go +++ b/syft/pkg/cataloger/binary/default_classifiers.go @@ -104,4 +104,32 @@ var defaultClassifiers = []classifier{ `(?m)BusyBox\s+v(?P[0-9]+\.[0-9]+\.[0-9]+)`), Package: "busybox", }, + { + Class: "php-cli-binary", + FileGlob: "**/php*", + EvidenceMatcher: fileNameTemplateVersionMatcher( + `(.*/|^)php[0-9]*$`, + `(?m)X-Powered-By: PHP\/(?P[0-9]+\.[0-9]+\.[0-9]+(beta[0-9]+|alpha[0-9]+|RC[0-9]+)?)`), + Package: "php-cli", + PURL: mustPURL("pkg:generic/php-cli@version"), + CPEs: singleCPE("cpe:2.3:a:php:php:*:*:*:*:*:*:*:*"), + }, + { + Class: "php-fpm-binary", + FileGlob: "**/php-fpm*", + EvidenceMatcher: fileContentsVersionMatcher( + `(?m)X-Powered-By: PHP\/(?P[0-9]+\.[0-9]+\.[0-9]+(beta[0-9]+|alpha[0-9]+|RC[0-9]+)?)`), + Package: "php-fpm", + PURL: mustPURL("pkg:generic/php-fpm@version"), + CPEs: singleCPE("cpe:2.3:a:php:php:*:*:*:*:*:*:*:*"), + }, + { + Class: "php-apache-binary", + FileGlob: "**/libphp*.so", + EvidenceMatcher: fileContentsVersionMatcher( + `(?m)X-Powered-By: PHP\/(?P[0-9]+\.[0-9]+\.[0-9]+(beta[0-9]+|alpha[0-9]+|RC[0-9]+)?)`), + Package: "libphp", + PURL: mustPURL("pkg:generic/php@version"), + CPEs: singleCPE("cpe:2.3:a:php:php:*:*:*:*:*:*:*:*"), + }, } diff --git a/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/.gitignore b/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/.gitignore index 5b1ba3eb4..5b880180c 100644 --- a/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/.gitignore +++ b/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/.gitignore @@ -1 +1,2 @@ -!libpython3.7.so \ No newline at end of file +!libpython3.7.so +!libphp.so \ No newline at end of file diff --git a/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/php-apache-8.2.1/libphp.so b/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/php-apache-8.2.1/libphp.so new file mode 100755 index 000000000..8d3b0c698 --- /dev/null +++ b/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/php-apache-8.2.1/libphp.so @@ -0,0 +1,5 @@ +[null] Script: '%s' +%s,%s +X-Powered-By: PHP/8.2.1 +index pointer +PHP_VERSION diff --git a/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/php-cli-8.2.1/php b/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/php-cli-8.2.1/php new file mode 100755 index 000000000..8d3b0c698 --- /dev/null +++ b/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/php-cli-8.2.1/php @@ -0,0 +1,5 @@ +[null] Script: '%s' +%s,%s +X-Powered-By: PHP/8.2.1 +index pointer +PHP_VERSION diff --git a/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/php-fpm-8.2.1/php-fpm b/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/php-fpm-8.2.1/php-fpm new file mode 100755 index 000000000..8d3b0c698 --- /dev/null +++ b/syft/pkg/cataloger/binary/test-fixtures/classifiers/positive/php-fpm-8.2.1/php-fpm @@ -0,0 +1,5 @@ +[null] Script: '%s' +%s,%s +X-Powered-By: PHP/8.2.1 +index pointer +PHP_VERSION