Skip to content

Commit 485eb00

Browse files
authored
runtime attach: prevent NPE with null value (#3712)
* runtime attach: prevent NPE with null value
1 parent ffdd4c9 commit 485eb00

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Use subheadings with the "=====" level for adding notes for unreleased changes:
3535
===== Bug fixes
3636
* Restore compatibility with Java 7 - {pull}3657[#3657]
3737
* Avoid `ClassCastException` and issue warning when trying to use otel span links - {pull}3672[#3672]
38+
* Avoid `NullPointerException` with runtime attach API and invalid map entries - {pull}3712[#3712]
3839
3940
[float]
4041
===== Features

apm-agent-attach/src/main/java/co/elastic/apm/attach/ElasticApmAttacher.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,14 @@ public static void attach(String pid, Map<String, String> configuration) {
150150
*/
151151
public static void attach(String pid, Map<String, String> configuration, File agentJarFile) {
152152
// making a copy of provided configuration as user might have used an immutable map impl.
153-
Map<String, String> config = new HashMap<>(configuration);
153+
// and guard against user-provided null keys and values
154+
Map<String, String> config = new HashMap<>();
155+
for (Map.Entry<String, String> entry : configuration.entrySet()) {
156+
if (entry.getKey() != null && entry.getValue() != null) {
157+
config.put(entry.getKey(), entry.getValue());
158+
}
159+
}
160+
154161
if (!config.containsKey("activation_method")) {
155162
config.put("activation_method", "PROGRAMMATIC_SELF_ATTACH");
156163
}

integration-tests/runtime-attach/runtime-attach-app/src/main/java/co/elastic/apm/testapp/AppMain.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public static void main(String[] args) {
7979
Map<String, String> config = new HashMap<>();
8080
config.put("service_name", "self-attach-external-config");
8181
config.put("config_file", agentConfig.getAbsolutePath());
82+
83+
// user-provided map might have null values and be un-modifiable
84+
// thus we must test it to ensure it's working as expected
85+
config.put("null_config_entry", null);
8286
ElasticApmAttacher.attach(Collections.unmodifiableMap(config));
8387
}
8488
}

0 commit comments

Comments
 (0)