-
-
Notifications
You must be signed in to change notification settings - Fork 76
Fix SlaveCommandStatistics memory leak with some commands #943
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 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0eeb67a
Fix SlaveCommandStatistics memory leak with some commands
apuig d2a288a
use jenkins.util.SystemProperties
apuig 33c1599
remove
apuig 58a4bc9
fix spotless
apuig 54c371b
Merge branch 'master' into limit-command-statistics
jglick 8524440
Merge branch 'master' into limit-command-statistics
Dohbedoh 0eacea5
Merge branch 'master' into limit-command-statistics
apuig 411c2a0
build ci
apuig 9a031e6
build ci
apuig 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
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
121 changes: 121 additions & 0 deletions
121
src/test/java/com/cloudbees/jenkins/support/impl/SlaveCommandStatisticsClassifyTest.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,121 @@ | ||
| /* | ||
| * The MIT License | ||
| * | ||
| * Copyright 2025 CloudBees, Inc. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package com.cloudbees.jenkins.support.impl; | ||
|
|
||
| import static com.cloudbees.jenkins.support.impl.SlaveCommandStatistics.Statistics.classify; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.*; | ||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| public class SlaveCommandStatisticsClassifyTest { | ||
|
|
||
| @Test | ||
| public void patternStripsEndBracketsHashAndParentheses() { | ||
| assertEquals( | ||
| "UserRequest:RemoteLaunchCallable", | ||
| classify("UserRequest:RemoteLaunchCallable[cmd=[docker, exec, --env, FOO=bar]]@2b194a2b")); | ||
|
|
||
| assertEquals("UserRequest:Callable", classify("UserRequest:Callable@1a2b3c4d[params]@5e6f7890(Context)")); | ||
|
|
||
| assertEquals( | ||
| "UserRequest:RemoteLaunchCallable", | ||
| classify( | ||
| "UserRequest:RemoteLaunchCallable[cmd=[nohup, sh, -c, (cp script.sh script.sh.copy; sh -xe script.sh.copy)], env=[Ljava.lang.String;@284e13da]@abc123")); | ||
|
|
||
| assertEquals( | ||
| "UserRequest:UserRPCRequest:org.jenkinsci.plugins.gitclient.GitClient.addCredentials", | ||
| classify( | ||
| "UserRequest:UserRPCRequest:org.jenkinsci.plugins.gitclient.GitClient.addCredentials[java.lang.String,com.cloudbees.plugins.credentials.common.StandardCredentials]@abc")); | ||
|
|
||
| assertEquals( | ||
| "UserRequest:hudson.FilePath$CallableWith", | ||
| classify("UserRequest:hudson.FilePath$CallableWith[workspace=/home/jenkins/workspace]@7f31245a")); | ||
|
|
||
| assertEquals( | ||
| "UserRequest:com.cloudbees.jenkins.support.impl.ThreadDumps$GetThreadDump", | ||
| classify("UserRequest:com.cloudbees.jenkins.support.impl.ThreadDumps$GetThreadDump@abc123")); | ||
|
|
||
| assertEquals("UserRequest:hudson.EnvVars$GetEnvVars", classify("UserRequest:hudson.EnvVars$GetEnvVars@def456")); | ||
|
|
||
| assertEquals( | ||
| "RPCRequest:hudson.FilePath.act", | ||
| classify( | ||
| "RPCRequest:hudson.FilePath.act[hudson.FilePath$FileCallable,hudson.remoting.VirtualChannel$ACL](123)")); | ||
|
|
||
| assertEquals("RPCRequest:hudson.FilePath.exists", classify("RPCRequest:hudson.FilePath.exists[](456)")); | ||
|
|
||
| // Legacy format (pre-jenkinsci/jenkins#9921) | ||
| assertEquals("Response", classify("Response@456abc(hudson.remoting.Channel)")); | ||
| assertEquals("UserRequest:RemoteLaunchCallable", classify("UserRequest:RemoteLaunchCallable@67c6eade8")); | ||
|
|
||
| // Invalid hex not matched | ||
| assertEquals("Cmd@zebra", classify("Cmd@zebra")); | ||
|
|
||
| // Brackets in middle preserved ($ anchor) | ||
| assertEquals("Prefix[arg]:Suffix", classify("Prefix[arg]:Suffix@abc")); | ||
| } | ||
|
|
||
| @Test | ||
| public void hugeEnvVarsDeduplicateToSameKey() { | ||
| String hugeEnv = "group1,group2,group3,%s".formatted("x".repeat(12000)); | ||
| String withHugeEnv = | ||
| "UserRequest:RemoteLaunchCallable[cmd=[docker, exec, --env, BUILD_USER_GROUPS=%s]]@67c6eade8" | ||
| .formatted(hugeEnv); | ||
|
|
||
| String expected = "UserRequest:RemoteLaunchCallable"; | ||
| assertEquals(expected, classify(withHugeEnv)); | ||
| assertEquals( | ||
| expected, classify("UserRequest:RemoteLaunchCallable[cmd=[docker, --env, BUILD_NUMBER=1]]@1a2b3c4d")); | ||
| assertEquals( | ||
| expected, classify("UserRequest:RemoteLaunchCallable[cmd=[docker, --env, BUILD_NUMBER=2]]@5e6f7890")); | ||
| assertEquals( | ||
| expected, | ||
| classify("UserRequest:RemoteLaunchCallable[cmd=[docker, --env, TIMESTAMP=%d]]@deadbeef" | ||
| .formatted(System.currentTimeMillis()))); | ||
|
|
||
| assertThat(classify(withHugeEnv).length(), lessThan(100)); | ||
| assertThat(classify(withHugeEnv), not(containsString("BUILD_USER_GROUPS"))); | ||
apuig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Test | ||
| public void defenseInDepthTruncationAndBounds() { | ||
| // Truncation: MAX_COMMAND_LENGTH = 256 | ||
| String veryLongCommand = "UserRequest:%s@1a2b3c4d".formatted("VeryLongClassName".repeat(20)); | ||
| String truncated = classify(veryLongCommand); | ||
| assertThat(truncated.length(), lessThanOrEqualTo(259)); | ||
| assertThat(truncated, endsWith("...")); | ||
|
|
||
| // Pattern bound: over 1MB gracefully degrades | ||
| String overLimit = "Cmd[%s]@abc".formatted("x".repeat(1048577)); | ||
| assertThat(classify(overLimit).length(), lessThanOrEqualTo(259)); | ||
| assertThat(classify(overLimit), startsWith("Cmd[")); | ||
|
|
||
| // ReDoS safety | ||
| assertEquals("Test", classify("Test%s]@abc".formatted("[".repeat(1000)))); | ||
| assertEquals("Test", classify("Test%s@abc".formatted("[]".repeat(500)))); | ||
| assertThat(classify("Test%s@abc".formatted("[".repeat(1000))).length(), lessThanOrEqualTo(259)); | ||
| } | ||
| } | ||
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.