-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: correct reliability of Central etc (JCS cache) analyzers on Java 25/Docker by making CLI classpath deterministic #8117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9eca5b4
fix: make JCS logging adapter configuration self-contained with DataC…
chadlwilson ba9d8ba
fix: Launch CLI from executable jar so we can ensure deterministic cl…
chadlwilson 09b8b4d
Merge branch 'main' into jcs-logging
chadlwilson 763ee10
Merge branch 'main' into jcs-logging
jeremylong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| #LICENSE_HEADER# | ||
| @echo off | ||
|
|
||
| set ERROR_CODE=0 | ||
|
|
||
| :init | ||
| @REM Decide how to startup depending on the version of windows | ||
|
|
||
| @REM -- Win98ME | ||
| if NOT "%OS%"=="Windows_NT" goto Win9xArg | ||
|
|
||
| @REM set local scope for the variables with windows NT shell | ||
| if "%OS%"=="Windows_NT" @setlocal | ||
|
|
||
| @REM -- 4NT shell | ||
| if "%eval[2+2]" == "4" goto 4NTArgs | ||
|
|
||
| @REM -- Regular WinNT shell | ||
| set CMD_LINE_ARGS=%* | ||
| goto WinNTGetScriptDir | ||
|
|
||
| @REM The 4NT Shell from jp software | ||
| :4NTArgs | ||
| set CMD_LINE_ARGS=%$ | ||
| goto WinNTGetScriptDir | ||
|
|
||
| :Win9xArg | ||
| @REM Slurp the command line arguments. This loop allows for an unlimited number | ||
| @REM of arguments (up to the command line limit, anyway). | ||
| set CMD_LINE_ARGS= | ||
| :Win9xApp | ||
| if %1a==a goto Win9xGetScriptDir | ||
| set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1 | ||
| shift | ||
| goto Win9xApp | ||
|
|
||
| :Win9xGetScriptDir | ||
| set SAVEDIR=%CD% | ||
| %0\ | ||
| cd %0\..\.. | ||
| set BASEDIR=%CD% | ||
| cd %SAVEDIR% | ||
| set SAVE_DIR= | ||
| goto repoSetup | ||
|
|
||
| :WinNTGetScriptDir | ||
| for %%i in ("%~dp0..") do set "BASEDIR=%%~fi" | ||
|
|
||
| :repoSetup | ||
| set REPO= | ||
| #ENV_SETUP# | ||
|
|
||
| if "%JAVACMD%"=="" set JAVACMD=#JAVA_BINARY# | ||
|
|
||
| if "%REPO%"=="" set REPO=%BASEDIR%\#REPO# | ||
|
|
||
| @REM Reaching here means variables are defined and arguments have been captured | ||
| :endInit | ||
|
|
||
| "%JAVACMD%" %JAVA_OPTS% #EXTRA_JVM_ARGUMENTS# -Dapp.name="#APP_NAME#" -Dapp.repo="%REPO%" -Dapp.home="%BASEDIR%" -Dbasedir="%BASEDIR%" #APP_ARGUMENTS#%CMD_LINE_ARGS% | ||
| if %ERRORLEVEL% NEQ 0 goto error | ||
| goto end | ||
|
|
||
| :error | ||
| if "%OS%"=="Windows_NT" @endlocal | ||
| set ERROR_CODE=%ERRORLEVEL% | ||
|
|
||
| :end | ||
| @REM set local scope for the variables with windows NT shell | ||
| if "%OS%"=="Windows_NT" goto endNT | ||
|
|
||
| @REM For old DOS remove the set variables from ENV - we assume they were not set | ||
| @REM before we started - at least we don't leave any baggage around | ||
| set CMD_LINE_ARGS= | ||
| goto postExec | ||
|
|
||
| :endNT | ||
| @REM If error code is set to 1 then the endlocal was done already in :error. | ||
| if %ERROR_CODE% EQU 0 @endlocal | ||
|
|
||
|
|
||
| :postExec | ||
|
|
||
| if "%FORCE_EXIT_ON_ERROR%" == "on" ( | ||
| if %ERROR_CODE% NEQ 0 exit %ERROR_CODE% | ||
| ) | ||
|
|
||
| exit /B %ERROR_CODE% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
cli/src/main/java/org/owasp/dependencycheck/PluginLoader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package org.owasp.dependencycheck; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.lang.instrument.Instrumentation; | ||
| import java.util.jar.JarFile; | ||
|
|
||
| /** | ||
| * Java agent for loading plugin JARs from a specified directory into the system classpath | ||
| * before the main application starts. This allows additional plugins to be available at runtime | ||
| * by appending their JAR files to the system class loader search path; while allowing use of an | ||
| * executable jar with deterministic classpath ordering. | ||
| * <p/> | ||
| * To use, specify this class as a Java agent and provide the plugins directory as the -javaagent argument | ||
| */ | ||
| public class PluginLoader { | ||
| /** | ||
| * Java agent entry point. Loads all JAR files from the specified plugins directory | ||
| * and appends them to the system class loader search path. | ||
| * | ||
| * @param agentArg the path to the plugins directory containing JAR files to load, e.g `-javaagent:cli.jar=/usr/share/dependency-check/plugins` | ||
| * @param inst the instrumentation instance provided by the JVM | ||
| */ | ||
| public static void premain(String agentArg, Instrumentation inst) { | ||
| File pluginsDir = new File(agentArg); | ||
| if (pluginsDir.isDirectory()) { | ||
| File[] files = pluginsDir.listFiles((dir, name) -> name.endsWith(".jar")); | ||
| for (File file : files == null ? new File[0] : files) { | ||
| try (JarFile jar = new JarFile(file)) { | ||
chadlwilson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| inst.appendToSystemClassLoaderSearch(jar); | ||
| } catch (IOException e) { | ||
| System.err.printf("[WARN] Failed to read plugin jar file at %s. Jar will not be available on classpath: %s%n", file, e); | ||
| e.printStackTrace(System.err); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
cli/src/test/java/org/owasp/dependencycheck/PluginLoaderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package org.owasp.dependencycheck; | ||
|
|
||
| import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.PrintStream; | ||
| import java.lang.instrument.Instrumentation; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.matchesPattern; | ||
| import static org.mockito.ArgumentMatchers.assertArg; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.verifyNoInteractions; | ||
|
|
||
| class PluginLoaderTest { | ||
|
|
||
| private final Instrumentation instrumentation = Mockito.mock(Instrumentation.class); | ||
|
|
||
| @TempDir | ||
| Path tempDir; | ||
|
|
||
| @Test | ||
| void shouldDoNothingIfDirectoryDoesntExist() { | ||
| PluginLoader.premain("blah", instrumentation); | ||
| verifyNoInteractions(instrumentation); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldDoNothingIfDirectoryIsEmpty() { | ||
| PluginLoader.premain(tempDir.toString(), instrumentation); | ||
| verifyNoInteractions(instrumentation); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldAddJarToClassPath() throws Exception { | ||
| createEmptyValidJar(); | ||
| createEmptyValidJar(); | ||
| PluginLoader.premain(tempDir.toString(), instrumentation); | ||
| verify(instrumentation, times(2)) | ||
| .appendToSystemClassLoaderSearch(assertArg(jar -> assertThat(jar.getName(), matchesPattern(".*/dummy.*\\.jar")))); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldStopLoadingPluginsOnBadJarButSucceed() throws Exception { | ||
| PrintStream originalErr = System.err; | ||
| ByteArrayOutputStream errContent = new ByteArrayOutputStream(); | ||
| System.setErr(new PrintStream(errContent)); | ||
| try { | ||
| createEmptyBadJar(); | ||
| PluginLoader.premain(tempDir.toString(), instrumentation); | ||
| assertThat(errContent.toString(), matchesPattern(Pattern.compile("\\[WARN\\] Failed to read plugin jar file at .*/dummy.*\\.jar\\. Jar will not be available on classpath.*zip file is empty.*", Pattern.DOTALL))); | ||
| } finally { | ||
| System.setErr(originalErr); | ||
| } | ||
| } | ||
|
|
||
| private Path createEmptyBadJar() throws IOException { | ||
| return Files.createTempFile(tempDir, "dummy", ".jar"); | ||
| } | ||
|
|
||
| private void createEmptyValidJar() throws IOException { | ||
| new ZipArchiveOutputStream(createEmptyBadJar().toFile()).close(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.