Skip to content

Commit b722692

Browse files
timjayaroslavafenkin
authored andcommitted
[SECURITY-3075]
1 parent 31209c0 commit b722692

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/main/java/org/jenkinsci/plugins/credentialsbinding/masking/SecretPatterns.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,25 @@
2727
import edu.umd.cs.findbugs.annotations.CheckForNull;
2828
import edu.umd.cs.findbugs.annotations.NonNull;
2929
import hudson.console.LineTransformationOutputStream;
30+
import java.util.Arrays;
31+
import jenkins.util.JenkinsJVM;
32+
3033
import java.io.IOException;
3134
import java.io.OutputStream;
3235
import java.util.Collection;
3336
import java.util.Comparator;
37+
import java.util.List;
3438
import java.util.function.Supplier;
39+
import java.util.logging.Level;
40+
import java.util.logging.Logger;
3541
import java.util.regex.Matcher;
3642
import java.util.regex.Pattern;
3743
import java.util.stream.Collectors;
3844

3945
public class SecretPatterns {
4046

47+
private static final Logger LOGGER = Logger.getLogger(SecretPatterns.class.getName());
48+
4149
private static final Comparator<String> BY_LENGTH_DESCENDING =
4250
Comparator.comparingInt(String::length).reversed().thenComparing(String::compareTo);
4351

@@ -51,10 +59,11 @@ public class SecretPatterns {
5159
* absence of quoting, the longer form is masked.
5260
*/
5361
public static @NonNull Pattern getAggregateSecretPattern(@NonNull Collection<String> inputs) {
62+
List<SecretPatternFactory> secretPatternFactories = getSecretPatternFactories();
5463
String pattern = inputs.stream()
5564
.filter(input -> !input.isEmpty())
5665
.flatMap(input ->
57-
SecretPatternFactory.all().stream().flatMap(factory ->
66+
secretPatternFactories.stream().flatMap(factory ->
5867
factory.getEncodedForms(input).stream()))
5968
.sorted(BY_LENGTH_DESCENDING)
6069
.distinct()
@@ -63,6 +72,31 @@ public class SecretPatterns {
6372
return Pattern.compile(pattern);
6473
}
6574

75+
private static List<SecretPatternFactory> getSecretPatternFactories() {
76+
if (JenkinsJVM.isJenkinsJVM()) {
77+
return SecretPatternFactory.all();
78+
} else {
79+
// TODO Change this to a hard fail in future, e.g. JenkinsJVM.checkJenkinsJVM();
80+
LOGGER.log(
81+
Level.WARNING,
82+
"An agent attempted to look up secret patterns from the controller, which is unsupported. " +
83+
"Falling back to basic implementation that may not mask common transformations of the secret. " +
84+
"This workaround will be removed in a future release. " +
85+
"This is a bug in the plugin calling SecretPatterns#getAggregateSecretPattern(String) " +
86+
"and should be reported to its maintainers. " +
87+
"The plugin can be identified through the stacktrace below.",
88+
new RuntimeException()
89+
);
90+
return Arrays.asList(
91+
new AlmquistShellSecretPatternFactory(),
92+
new BashSecretPatternFactory(),
93+
new BatchSecretPatternFactory(),
94+
new DollarSecretPatternFactory(),
95+
new LiteralSecretPatternFactory()
96+
);
97+
}
98+
}
99+
66100
/**
67101
* Delegating output stream that masks occurrences of a set of secrets.
68102
*/

0 commit comments

Comments
 (0)