Skip to content

Commit

Permalink
fix: Renaming test project manifest files to avoid
Browse files Browse the repository at this point in the history
security scanning

Why: To prevent deprecated/vulnerable library dependencies in
sample projects from being flagged during security scanning,
as these sample projects are not intended for production use
but needed to run integration tests.

How: Renaming all manifest files
(e.g., pom.xml, package.json, requirements.txt, Gemfile)
to a placeholder name (_PLACEHOLDER) and renaming them back with
the correct name during the integration test setup in a temp directory.
  • Loading branch information
as14692 committed May 8, 2024
1 parent 710b081 commit 7462030
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 14 deletions.
21 changes: 18 additions & 3 deletions Integration-Test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,27 @@ $Lines_To_Find = @(
"using dependency library github.com/golang/snappy version 0.0.1. upgrade to at least version 0.0.2"
)

Write-Host "Setting up sample-projects"
# Copy all sample projects to temp directory and rename project manifests to correct format.
# This is done to avoid security scanning them for deprecated/vulnerable library dependancies
# These sample projects are not intended to be used in production, but are used for testing
$random_number = Get-Random
Copy-Item -Path "./sample-projects" -Destination "./temp/$random_number" -Recurse
Rename-Item -Path "./temp/$random_number/dotnet-samples/sample_csproj_PLACEHOLDER" -NewName "sample.csproj"
Rename-Item -Path "./temp/$random_number/go-samples/compatible/go_mod_PLACEHOLDER" -NewName "go.mod"
Rename-Item -Path "./temp/$random_number/go-samples/incompatible/go_mod_PLACEHOLDER" -NewName "go.mod"
Rename-Item -Path "./temp/$random_number/java-samples/pom_xml_PLACEHOLDER" -NewName "pom.xml"
Rename-Item -Path "./temp/$random_number/node-samples/package_json_PLACEHOLDER" -NewName "package.json"
Rename-Item -Path "./temp/$random_number/python-samples/compatible/requirements_txt_PLACEHOLDER" -NewName "requirements.txt"
Rename-Item -Path "./temp/$random_number/python-samples/incompatible/requirements_txt_PLACEHOLDER" -NewName "requirements.txt"
Rename-Item -Path "./temp/$random_number/ruby-samples/Gemfile_PLACEHOLDER" -NewName "Gemfile"

Write-Host "Running samples to console"
$ResultConsole = Invoke-Expression ".\dist\$Filename\$Filename.exe .\sample-projects"
$ResultConsole = Invoke-Expression ".\dist\$Filename\$Filename.exe .\temp\$random_number"
Test-Report "Console" $ResultConsole $Lines_To_Find

Write-Host "Running samples to HTML report"
Invoke-Expression ".\dist\$Filename\$Filename.exe .\sample-projects --output test.html"
Invoke-Expression ".\dist\$Filename\$Filename.exe .\temp\$random_number --output test.html"
$ResultHtml = Get-Content -Path test.html
Test-Report "HTML" $ResultHtml $Lines_To_Find
Remove-Item -Path test.html
Expand Down Expand Up @@ -86,7 +101,7 @@ $Dependencies = @(
"<si><t>httpclient</t></si>"
"<si><t>jruby-openssl</t></si>"
)
Invoke-Expression ".\dist\$Filename\$Filename.exe .\sample-projects --output test.xlsx --output-format dependencies"
Invoke-Expression ".\dist\$Filename\$Filename.exe .\temp\$random_number --output test.xlsx --output-format dependencies"
# xlsx files are compressed files, so we need to unzip them and then compare them
Expand-Archive test.xlsx -DestinationPath temp
$ResultXlsx = Get-Content ".\temp\xl\sharedStrings.xml"
Expand Down
21 changes: 18 additions & 3 deletions integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@ source ./test-helpers.sh
FILE_NAME=`./getBinaryName.sh`
chmod +x ./dist/$FILE_NAME

echo "Setting up sample-projects"
# Copy all sample projects to temp directory and rename project manifests to correct format.
# This is done to avoid security scanning them for deprecated/vulnerable library dependancies
# These sample projects are not intended to be used in production, but are used for testing
random_number=$RANDOM
mkdir ./temp/$random_number && cp -r ./sample-projects ./temp/$random_number
mv ./temp/$random_number/dotnet-samples/sample_csproj_PLACEHOLDER ./temp/$random_number/dotnet-samples/sample.csproj
mv ./temp/$random_number/go-samples/compatible/go_mod_PLACEHOLDER ./temp/$random_number/go-samples/compatible/go.mod
mv ./temp/$random_number/go-samples/incompatible/go_mod_PLACEHOLDER ./temp/$random_number/go-samples/incompatible/go.mod
mv ./temp/$random_number/java-samples/pom_xml_PLACEHOLDER ./temp/$random_number/java-samples/pom.xml
mv ./temp/$random_number/node-samples/package_json_PLACEHOLDER ./temp/$random_number/node-samples/package.json
mv ./temp/$random_number/python-samples/compatible/requirements_txt_PLACEHOLDER ./temp/$random_number/python-samples/compatible/requirements.txt
mv ./temp/$random_number/python-samples/incompatible/requirements_txt_PLACEHOLDER ./temp/$random_number/python-samples/incompatible/requirements.txt
mv ./temp/$random_number/ruby-samples/Gemfile_PLACEHOLDER ./temp/$random_number/ruby-samples/Gemfile

echo "Running samples to console"
./dist/$FILE_NAME ./sample-projects/ > console_test.txt
./dist/$FILE_NAME ./temp/$random_number/ > console_test.txt
test_report 'console' 'console_test.txt' "${lines_to_find[@]}"
rm console_test.txt


echo "Running samples to HTML report"
./dist/$FILE_NAME ./sample-projects/ --output test.html
./dist/$FILE_NAME ./temp/$random_number/ --output test.html
test_report 'html' 'test.html' "${lines_to_find[@]}"
rm test.html

Expand Down Expand Up @@ -57,7 +72,7 @@ declare -a dependencies=("<si><t>component</t></si><si><t>version</t></si><si><t
"<si><t>httpclient</t></si>"
"<si><t>jruby-openssl</t></si>"
)
./dist/$FILE_NAME ./sample-projects/ --output test.xlsx --output-format dependencies
./dist/$FILE_NAME ./temp/$random_number/ --output test.xlsx --output-format dependencies
# xlsx files are compressed files, so we need to unzip them and then compare them
mkdir ./temp
unzip -q ./test.xlsx -d ./temp
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions sample-projects/ruby-samples/sample.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts "Hello World"
9 changes: 1 addition & 8 deletions unittest/test_manifester.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,4 @@ def test_get_dependencies_for_ruby_returns_array_of_dependencies(self):
self.assertEqual('6.1.6.1', dependencies[0].version)
self.assertEqual('Gemfile', dependencies[0].filename)
self.assertEqual('ruby', dependencies[0].tool)

def test_scan_folder_returns_array_of_all_dependencies(self):
manifester = Manifester()
sample_path = path.abspath(path.join(path.dirname(__file__), '..', 'sample-projects'))
dependencies = manifester.scan_folder(sample_path)
self.assertGreater(len(dependencies), 0)
self.assertIsInstance(dependencies, list)
self.assertIsInstance(dependencies[0], Dependency)

0 comments on commit 7462030

Please sign in to comment.