Skip to content

Commit a654a98

Browse files
committed
fix: filter possibly sensitive use properties
1 parent 9b008bc commit a654a98

2 files changed

Lines changed: 60 additions & 7 deletions

File tree

src/main/java/org/apache/commons/release/plugin/internal/BuildDefinitions.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import java.lang.management.ManagementFactory;
2222
import java.nio.file.Path;
2323
import java.util.ArrayList;
24+
import java.util.Arrays;
2425
import java.util.Collections;
2526
import java.util.HashMap;
2627
import java.util.List;
28+
import java.util.Locale;
2729
import java.util.Map;
2830
import java.util.Properties;
2931
import java.util.TreeMap;
@@ -37,8 +39,18 @@
3739
*/
3840
public final class BuildDefinitions {
3941

42+
/**
43+
* User-property names containing any of these substrings (case-insensitive) are omitted from attestations.
44+
*
45+
* <p>The Maven GPG plugin discourages passing credentials on the command line, but a stray {@code -Dgpg.passphrase=...} must not be captured in the
46+
* attestation if someone does it anyway.</p>
47+
*/
48+
private static final List<String> SENSITIVE_KEYWORDS =
49+
Arrays.asList("secret", "password", "passphrase", "token", "credential");
50+
4051
/**
4152
* Reconstructs the Maven command line string from the given execution request.
53+
* User properties whose name matches {@link #SENSITIVE_KEYWORDS} are omitted.
4254
*
4355
* @param request the Maven execution request
4456
* @return a string representation of the Maven command line
@@ -49,10 +61,26 @@ static String commandLine(final MavenExecutionRequest request) {
4961
if (!profiles.isEmpty()) {
5062
args.add("-P" + profiles);
5163
}
52-
request.getUserProperties().forEach((key, value) -> args.add("-D" + key + "=" + value));
64+
request.getUserProperties().forEach((key, value) -> {
65+
final String k = key.toString();
66+
if (isNotSensitive(k)) {
67+
args.add("-D" + k + "=" + value);
68+
}
69+
});
5370
return String.join(" ", args);
5471
}
5572

73+
/**
74+
* Checks if a property key is not sensitive.
75+
*
76+
* @param property A property key
77+
* @return {@code true} if the property is not considered sensitive
78+
*/
79+
private static boolean isNotSensitive(final String property) {
80+
final String lower = property.toLowerCase(Locale.ROOT);
81+
return SENSITIVE_KEYWORDS.stream().noneMatch(lower::contains);
82+
}
83+
5684
/**
5785
* Returns a map of external build parameters captured from the current JVM and Maven session.
5886
*
@@ -65,7 +93,7 @@ public static Map<String, Object> externalParameters(final MavenSession session)
6593
final MavenExecutionRequest request = session.getRequest();
6694
params.put("maven.goals", request.getGoals());
6795
params.put("maven.profiles", request.getActiveProfiles());
68-
params.put("maven.user.properties", request.getUserProperties());
96+
params.put("maven.user.properties", getUserProperties(request));
6997
params.put("maven.cmdline", commandLine(request));
7098
final Map<String, Object> env = new HashMap<>();
7199
params.put("env", env);
@@ -78,6 +106,23 @@ public static Map<String, Object> externalParameters(final MavenSession session)
78106
return params;
79107
}
80108

109+
/**
110+
* Returns a filtered map of user properties.
111+
*
112+
* @param request A Maven request
113+
* @return A map of user properties.
114+
*/
115+
private static TreeMap<String, String> getUserProperties(final MavenExecutionRequest request) {
116+
final TreeMap<String, String> properties = new TreeMap<>();
117+
request.getUserProperties().forEach((k, value) -> {
118+
final String key = k.toString();
119+
if (isNotSensitive(key)) {
120+
properties.put(key, value.toString());
121+
}
122+
});
123+
return properties;
124+
}
125+
81126
/**
82127
* Creates a {@link ResourceDescriptor} for the JDK used during the build.
83128
*

src/test/java/org/apache/commons/release/plugin/internal/BuildDefinitionsTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,23 @@ static Stream<Arguments> commandLineArguments() {
4040
Arguments.of("multiple goals", asList("clean", "verify"), emptyList(), new Properties(), "clean verify"),
4141
Arguments.of("single profile", singletonList("verify"), singletonList("release"), new Properties(), "verify -Prelease"),
4242
Arguments.of("multiple profiles", singletonList("verify"), asList("release", "sign"), new Properties(), "verify -Prelease,sign"),
43-
Arguments.of("user property", singletonList("verify"), emptyList(), singletonProperties("foo", "bar"), "verify -Dfoo=bar"),
44-
Arguments.of("goals, profile and property", singletonList("verify"), singletonList("release"), singletonProperties("foo", "bar"),
45-
"verify -Prelease -Dfoo=bar")
43+
Arguments.of("user property", singletonList("verify"), emptyList(), toProperties("foo", "bar"), "verify -Dfoo=bar"),
44+
Arguments.of("goals, profile and property", singletonList("verify"), singletonList("release"), toProperties("foo", "bar"),
45+
"verify -Prelease -Dfoo=bar"),
46+
Arguments.of("redacts gpg.passphrase", singletonList("verify"), emptyList(), toProperties("gpg.passphrase", "s3cr3t"), "verify"),
47+
Arguments.of("redacts passphrase case-insensitively", singletonList("verify"), emptyList(), toProperties("GPG_PASSPHRASE", "s3cr3t"), "verify"),
48+
Arguments.of("redacts any *password*", singletonList("verify"), emptyList(), toProperties("my.db.password", "hunter2"), "verify"),
49+
Arguments.of("redacts *token*", singletonList("verify"), emptyList(), toProperties("github.token", "ghp_xxx"), "verify"),
50+
Arguments.of("keeps safe property, drops sensitive one", singletonList("verify"), emptyList(),
51+
toProperties("foo", "bar", "gpg.passphrase", "s3cr3t"), "verify -Dfoo=bar")
4652
);
4753
}
4854

49-
private static Properties singletonProperties(final String key, final String value) {
55+
private static Properties toProperties(final String... keysAndValues) {
5056
final Properties p = new Properties();
51-
p.setProperty(key, value);
57+
for (int i = 0; i < keysAndValues.length; i += 2) {
58+
p.setProperty(keysAndValues[i], keysAndValues[i + 1]);
59+
}
5260
return p;
5361
}
5462

0 commit comments

Comments
 (0)