From 067031956fec75c5f80b52e8295b1424c2d811d3 Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Tue, 24 Jan 2023 23:02:37 +0000 Subject: [PATCH 1/2] Add a rule to block empty YAML constructors. --- pom.xml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pom.xml b/pom.xml index e335ad4bc0..a8225e5e10 100644 --- a/pom.xml +++ b/pom.xml @@ -468,6 +468,17 @@ INVALID IMPORTS (GUAVA) + + + + src/**/*.java + + + Forbids new Yaml() + ^.*new Yaml\(\).*$ + INVALID CONSTRUCTOR (SNAKEYAML) + + From d4fe343d4cd1a655c9102859358c820d9949c3e5 Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Tue, 24 Jan 2023 22:54:00 +0000 Subject: [PATCH 2/2] Switch to SafeConstructor for YAML everywhere. --- .../src/main/java/io/kubernetes/client/util/FilePersister.java | 3 ++- .../io/kubernetes/client/util/generic/dynamic/Dynamics.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/util/src/main/java/io/kubernetes/client/util/FilePersister.java b/util/src/main/java/io/kubernetes/client/util/FilePersister.java index 16163927b5..f4c800268d 100644 --- a/util/src/main/java/io/kubernetes/client/util/FilePersister.java +++ b/util/src/main/java/io/kubernetes/client/util/FilePersister.java @@ -18,6 +18,7 @@ import java.util.ArrayList; import java.util.HashMap; import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.SafeConstructor; public class FilePersister implements ConfigPersister { File configFile; @@ -50,7 +51,7 @@ public void save( // Note this is imperfect, should protect against other processes writing this file too... synchronized (configFile) { try (FileWriter fw = new FileWriter(configFile)) { - Yaml yaml = new Yaml(); + Yaml yaml = new Yaml(new SafeConstructor()); yaml.dump(config, fw); fw.flush(); } diff --git a/util/src/main/java/io/kubernetes/client/util/generic/dynamic/Dynamics.java b/util/src/main/java/io/kubernetes/client/util/generic/dynamic/Dynamics.java index 47e263beae..f93e05b34f 100644 --- a/util/src/main/java/io/kubernetes/client/util/generic/dynamic/Dynamics.java +++ b/util/src/main/java/io/kubernetes/client/util/generic/dynamic/Dynamics.java @@ -17,11 +17,12 @@ import io.kubernetes.client.openapi.JSON; import java.util.Map; import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.SafeConstructor; public class Dynamics { static final JSON internalJSONCodec = new JSON(); - static final Yaml internalYamlCodec = new Yaml(); + static final Yaml internalYamlCodec = new Yaml(new SafeConstructor()); public static DynamicKubernetesObject newFromJson(String jsonContent) { return newFromJson(internalJSONCodec.getGson(), jsonContent);