Skip to content

Commit bcf6cf6

Browse files
authored
Merge pull request #394 from dwnusbaum/JENKINS-68070
[JENKINS-68070] Adapt to addition of CharSequence.isEmpty in Java 15
2 parents f967fbe + f42b0b3 commit bcf6cf6

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

src/main/java/org/jenkinsci/plugins/scriptsecurity/sandbox/whitelists/EnumeratingWhitelist.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ static String canonicalStaticFieldSig(@NonNull Field field) {
314314
public static class MethodSignature extends Signature {
315315
final String receiverType, method;
316316
final String[] argumentTypes;
317-
public MethodSignature(String receiverType, String method, String[] argumentTypes) {
317+
public MethodSignature(String receiverType, String method, String... argumentTypes) {
318318
this.receiverType = receiverType;
319319
this.method = method;
320320
this.argumentTypes = argumentTypes.clone();
@@ -360,7 +360,7 @@ public boolean isWildcard() {
360360
}
361361

362362
static class StaticMethodSignature extends MethodSignature {
363-
StaticMethodSignature(String receiverType, String method, String[] argumentTypes) {
363+
StaticMethodSignature(String receiverType, String method, String... argumentTypes) {
364364
super(receiverType, method, argumentTypes);
365365
}
366366
@Override public String toString() {

src/main/resources/org/jenkinsci/plugins/scriptsecurity/sandbox/whitelists/generic-whitelist

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ staticMethod java.lang.Boolean parseBoolean java.lang.String
115115
staticMethod java.lang.Boolean valueOf boolean
116116
staticMethod java.lang.Boolean valueOf java.lang.String
117117
method java.lang.CharSequence charAt int
118+
method java.lang.CharSequence isEmpty
118119
method java.lang.CharSequence length
119120
method java.lang.CharSequence subSequence int int
120121
method java.lang.Class getName
@@ -773,6 +774,14 @@ staticField java.util.concurrent.TimeUnit HOURS
773774
staticField java.util.concurrent.TimeUnit MILLISECONDS
774775
staticField java.util.concurrent.TimeUnit MINUTES
775776
staticField java.util.concurrent.TimeUnit SECONDS
777+
method java.util.random.RandomGenerator nextBoolean
778+
method java.util.random.RandomGenerator nextBytes byte[]
779+
method java.util.random.RandomGenerator nextDouble
780+
method java.util.random.RandomGenerator nextFloat
781+
method java.util.random.RandomGenerator nextGaussian
782+
method java.util.random.RandomGenerator nextInt
783+
method java.util.random.RandomGenerator nextInt int
784+
method java.util.random.RandomGenerator nextLong
776785
method java.util.regex.MatchResult end
777786
method java.util.regex.MatchResult end int
778787
method java.util.regex.MatchResult group

src/test/java/org/jenkinsci/plugins/scriptsecurity/sandbox/whitelists/EnumeratingWhitelistTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public void m(Object[] args) {}
7878
assertFalse(new EnumeratingWhitelist.MethodSignature(HashMap.class, "size").exists());
7979
assertTrue(new EnumeratingWhitelist.MethodSignature(Map.class, "size").exists());
8080
assertTrue(new EnumeratingWhitelist.MethodSignature(Map.Entry.class, "getKey").exists());
81-
assertTrue(new EnumeratingWhitelist.MethodSignature("java.util.Map$Entry", "getKey", new String[0]).exists());
82-
assertThrows(ClassNotFoundException.class, new EnumeratingWhitelist.MethodSignature("java.util.Map.Entry", "getKey", new String[0])::exists);
81+
assertTrue(new EnumeratingWhitelist.MethodSignature("java.util.Map$Entry", "getKey").exists());
82+
assertThrows(ClassNotFoundException.class, new EnumeratingWhitelist.MethodSignature("java.util.Map.Entry", "getKey")::exists);
8383
}
8484

8585
@Test

src/test/java/org/jenkinsci/plugins/scriptsecurity/sandbox/whitelists/StaticWhitelistTest.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.Collections;
3838
import java.util.HashSet;
3939
import java.util.List;
40+
import java.util.Random;
4041
import java.util.Set;
4142

4243
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.EnumeratingWhitelist.MethodSignature;
@@ -98,26 +99,50 @@ static void sanity(URL definition) throws Exception {
9899

99100

100101
for (EnumeratingWhitelist.Signature sig : sigs) {
102+
if (KNOWN_GOOD_SIGNATURES.contains(sig)) {
103+
continue;
104+
}
101105
try {
102106
assertTrue(sig + " does not exist (or is an override)", sig.exists());
103107
} catch (ClassNotFoundException x) {
104-
if (!KNOWN_GOOD_SIGNATURES.contains(sig)) {
105-
throw new Exception("Unable to verify existence of " + sig, x);
106-
}
108+
// Wrapping exception to include the full signature in the error message.
109+
throw new Exception("Unable to verify existence of " + sig, x);
107110
}
108111
}
109112
}
110113

111114
/**
112-
* A set of signatures that are well-formed, but for which {@link Signature#exists} throws an exception because
113-
* they involve types that are not available on the classpath when these tests run.
115+
* A set of signatures that are well-formed, but for which {@link Signature#exists} may throw an exception depending
116+
* on the test environment.
114117
*/
115118
private static final Set<Signature> KNOWN_GOOD_SIGNATURES = new HashSet<>(Arrays.asList(
116-
// From blacklist
117-
new MethodSignature("org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper", "getRawBuild", new String[0]),
118-
// From generic-whitelist
119+
// From workflow-support, which is not a dependency of this plugin.
120+
new MethodSignature("org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper", "getRawBuild"),
121+
// From groovy-cps, which is not a dependency of this plugin.
119122
new StaticMethodSignature("com.cloudbees.groovy.cps.CpsDefaultGroovyMethods", "each",
120-
new String[] { "java.util.Iterator", "groovy.lang.Closure" })
123+
"java.util.Iterator", "groovy.lang.Closure"),
124+
// Overrides CharSequence.isEmpty in Java 15+.
125+
new MethodSignature(String.class, "isEmpty"),
126+
// Does not exist until Java 15.
127+
new MethodSignature(CharSequence.class, "isEmpty"),
128+
// Override the corresponding RandomGenerator methods in Java 17+.
129+
new MethodSignature(Random.class, "nextBoolean"),
130+
new MethodSignature(Random.class, "nextBytes", byte[].class),
131+
new MethodSignature(Random.class, "nextDouble"),
132+
new MethodSignature(Random.class, "nextFloat"),
133+
new MethodSignature(Random.class, "nextGaussian"),
134+
new MethodSignature(Random.class, "nextInt"),
135+
new MethodSignature(Random.class, "nextInt", int.class),
136+
new MethodSignature(Random.class, "nextLong"),
137+
// Do not exist until Java 17.
138+
new MethodSignature("java.util.random.RandomGenerator", "nextBoolean"),
139+
new MethodSignature("java.util.random.RandomGenerator", "nextBytes", "byte[]"),
140+
new MethodSignature("java.util.random.RandomGenerator", "nextDouble"),
141+
new MethodSignature("java.util.random.RandomGenerator", "nextFloat"),
142+
new MethodSignature("java.util.random.RandomGenerator", "nextGaussian"),
143+
new MethodSignature("java.util.random.RandomGenerator", "nextInt"),
144+
new MethodSignature("java.util.random.RandomGenerator", "nextInt", "int"),
145+
new MethodSignature("java.util.random.RandomGenerator", "nextLong")
121146
));
122147

123148
@Test public void sanity() throws Exception {

0 commit comments

Comments
 (0)