Blame SOURCES/0001-Fix-CVE-2022-1471-and-add-a-test.patch

57a889
From fa99814967c44e654aae64802947209e2817359f Mon Sep 17 00:00:00 2001
57a889
From: Severin Gehwolf <sgehwolf@redhat.com>
57a889
Date: Tue, 6 Dec 2022 17:32:30 +0100
57a889
Subject: [PATCH] Fix CVE-2022-1471 and add a test
57a889
57a889
---
57a889
 .../java/io/prometheus/jmx/JmxCollector.java  | 34 +++++++++----
57a889
 .../io/prometheus/jmx/JmxCollectorTest.java   | 51 +++++++++++++++++++
57a889
 .../test/java/io/prometheus/jmx/Person.java   | 38 ++++++++++++++
57a889
 .../test/resources/testyaml_javabean.config   |  6 +++
57a889
 4 files changed, 119 insertions(+), 10 deletions(-)
57a889
 create mode 100644 collector/src/test/java/io/prometheus/jmx/Person.java
57a889
 create mode 100644 collector/src/test/resources/testyaml_javabean.config
57a889
57a889
diff --git a/collector/src/main/java/io/prometheus/jmx/JmxCollector.java b/collector/src/main/java/io/prometheus/jmx/JmxCollector.java
57a889
index a5fc96f..38f13ed 100644
57a889
--- a/collector/src/main/java/io/prometheus/jmx/JmxCollector.java
57a889
+++ b/collector/src/main/java/io/prometheus/jmx/JmxCollector.java
57a889
@@ -1,12 +1,7 @@
57a889
 package io.prometheus.jmx;
57a889
 
57a889
-import io.prometheus.client.Collector;
57a889
-import io.prometheus.client.Counter;
57a889
-import org.yaml.snakeyaml.Yaml;
57a889
-import org.yaml.snakeyaml.error.YAMLException;
57a889
+import static java.lang.String.format;
57a889
 
57a889
-import javax.management.MalformedObjectNameException;
57a889
-import javax.management.ObjectName;
57a889
 import java.io.File;
57a889
 import java.io.FileReader;
57a889
 import java.io.IOException;
57a889
@@ -25,7 +20,19 @@ import java.util.logging.Logger;
57a889
 import java.util.regex.Matcher;
57a889
 import java.util.regex.Pattern;
57a889
 
57a889
-import static java.lang.String.format;
57a889
+import javax.management.MalformedObjectNameException;
57a889
+import javax.management.ObjectName;
57a889
+
57a889
+import org.yaml.snakeyaml.DumperOptions;
57a889
+import org.yaml.snakeyaml.LoaderOptions;
57a889
+import org.yaml.snakeyaml.Yaml;
57a889
+import org.yaml.snakeyaml.constructor.SafeConstructor;
57a889
+import org.yaml.snakeyaml.error.YAMLException;
57a889
+import org.yaml.snakeyaml.representer.Representer;
57a889
+import org.yaml.snakeyaml.resolver.Resolver;
57a889
+
57a889
+import io.prometheus.client.Collector;
57a889
+import io.prometheus.client.Counter;
57a889
 
57a889
 public class JmxCollector extends Collector implements Collector.Describable {
57a889
     static final Counter configReloadSuccess = Counter.build()
57a889
@@ -76,7 +83,7 @@ public class JmxCollector extends Collector implements Collector.Describable {
57a889
         // will be thrown for bad configs
57a889
         Map<String, Object> yamlConfig = null;
57a889
         try {
57a889
-            yamlConfig = (Map<String, Object>)new Yaml().load(new FileReader(in));
57a889
+            yamlConfig = (Map<String, Object>)createYamlInstance().load(new FileReader(in));
57a889
         } catch (YAMLException e) {
57a889
             System.err.println("YAML configuration error: " + e.getMessage());
57a889
             throw new IllegalArgumentException(e);
57a889
@@ -86,7 +93,14 @@ public class JmxCollector extends Collector implements Collector.Describable {
57a889
     }
57a889
 
57a889
     public JmxCollector(String yamlConfig) throws MalformedObjectNameException {
57a889
-        config = loadConfig((Map<String, Object>)new Yaml().load(yamlConfig));
57a889
+        config = loadConfig((Map<String, Object>)createYamlInstance().load(yamlConfig));
57a889
+    }
57a889
+
57a889
+    private static Yaml createYamlInstance() {
57a889
+        // Equivalent to default Yaml() constructor but using SafeConstructor()
57a889
+        // over Constructor();
57a889
+        return new Yaml(new SafeConstructor(), new Representer(), new DumperOptions(), new LoaderOptions(),
57a889
+                new Resolver());
57a889
     }
57a889
 
57a889
     private void reloadConfig() {
57a889
@@ -94,7 +108,7 @@ public class JmxCollector extends Collector implements Collector.Describable {
57a889
         FileReader fr = new FileReader(configFile);
57a889
 
57a889
         try {
57a889
-          Map<String, Object> newYamlConfig = (Map<String, Object>)new Yaml().load(fr);
57a889
+          Map<String, Object> newYamlConfig = (Map<String, Object>)createYamlInstance().load(fr);
57a889
           config = loadConfig(newYamlConfig);
57a889
           config.lastUpdate = configFile.lastModified();
57a889
           configReloadSuccess.inc();
57a889
diff --git a/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java b/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java
57a889
index 3d4ecf0..f499fb3 100644
57a889
--- a/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java
57a889
+++ b/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java
57a889
@@ -8,12 +8,15 @@ import static org.junit.Assert.fail;
57a889
 
57a889
 import java.io.File;
57a889
 import java.lang.management.ManagementFactory;
57a889
+import java.nio.file.Files;
57a889
+import java.util.stream.Collectors;
57a889
 
57a889
 import javax.management.MBeanServer;
57a889
 
57a889
 import org.junit.Before;
57a889
 import org.junit.BeforeClass;
57a889
 import org.junit.Test;
57a889
+import org.yaml.snakeyaml.constructor.ConstructorException;
57a889
 import org.yaml.snakeyaml.error.YAMLException;
57a889
 
57a889
 import io.prometheus.client.Collector;
57a889
@@ -272,4 +275,52 @@ public class JmxCollectorTest {
57a889
         assertEquals(prefix + "Number of aliases for non-scalar nodes exceeds the specified max=50", e.getMessage());
57a889
       }
57a889
     }
57a889
+
57a889
+	@Test
57a889
+	public void javaBeanLoadTestFile() throws Exception {
57a889
+	    try {
57a889
+	        File configFile = new File(getClass().getResource("/testyaml_javabean.config").getPath());
57a889
+	        doJavaBeanLoadTest(configFile, false);
57a889
+	    } finally {
57a889
+	        Person.fixtureReset();
57a889
+	    }
57a889
+	}
57a889
+
57a889
+	@Test
57a889
+    public void javaBeanLoadTestString() throws Exception {
57a889
+	    try {
57a889
+	        File configFile = new File(getClass().getResource("/testyaml_javabean.config").getPath());
57a889
+	        doJavaBeanLoadTest(configFile, true);
57a889
+	    } finally {
57a889
+	        Person.fixtureReset();
57a889
+	    }
57a889
+    }
57a889
+
57a889
+    private void doJavaBeanLoadTest(File configFile, boolean passAsString) throws Exception {
57a889
+        assertTrue(configFile.exists());
57a889
+        assertEquals(0, Person.loadCount.get());
57a889
+        try {
57a889
+            if (passAsString) {
57a889
+                String config = Files.readAllLines(configFile.toPath()).stream().collect(Collectors.joining("\n"));
57a889
+                new JmxCollector(config);
57a889
+            } else {
57a889
+                new JmxCollector(configFile);
57a889
+            }
57a889
+            assertEquals("Expected Person class to not be instantiated", 0, Person.loadCount.get());
57a889
+        } catch (ConstructorException e) {
57a889
+            String problem = e.getProblem();
57a889
+            assertTrue(problem.contains(Person.class.getName()));
57a889
+            assertTrue(problem.contains("could not determine a constructor"));
57a889
+        } catch (IllegalArgumentException e) {
57a889
+            Throwable cause = e.getCause();
57a889
+            if (cause instanceof ConstructorException) {
57a889
+                ConstructorException ce = (ConstructorException) cause;
57a889
+                String problem = ce.getProblem();
57a889
+                assertTrue(problem.contains(Person.class.getName()));
57a889
+                assertTrue(problem.contains("could not determine a constructor"));
57a889
+            } else {
57a889
+                fail("Expected ConstructorException as cause!");
57a889
+            }
57a889
+        }
57a889
+    }
57a889
 }
57a889
diff --git a/collector/src/test/java/io/prometheus/jmx/Person.java b/collector/src/test/java/io/prometheus/jmx/Person.java
57a889
new file mode 100644
57a889
index 0000000..e3b339a
57a889
--- /dev/null
57a889
+++ b/collector/src/test/java/io/prometheus/jmx/Person.java
57a889
@@ -0,0 +1,38 @@
57a889
+package io.prometheus.jmx;
57a889
+
57a889
+import java.util.concurrent.atomic.AtomicInteger;
57a889
+
57a889
+/**
57a889
+ * Simple java bean (used by yaml load test)
57a889
+ *
57a889
+ */
57a889
+public class Person {
57a889
+
57a889
+    public static final AtomicInteger loadCount = new AtomicInteger(0);
57a889
+
57a889
+    private String firstName;
57a889
+    private String lastName;
57a889
+
57a889
+    public String getFirstName() {
57a889
+        return firstName;
57a889
+    }
57a889
+
57a889
+    public void setFirstName(String firstName) {
57a889
+        loadCount.incrementAndGet();
57a889
+        this.firstName = firstName;
57a889
+    }
57a889
+
57a889
+    public String getLastName() {
57a889
+        return lastName;
57a889
+    }
57a889
+
57a889
+    public void setLastName(String lastName) {
57a889
+        loadCount.incrementAndGet();
57a889
+        this.lastName = lastName;
57a889
+    }
57a889
+
57a889
+    public static void fixtureReset() {
57a889
+        loadCount.set(0);
57a889
+    }
57a889
+
57a889
+}
57a889
diff --git a/collector/src/test/resources/testyaml_javabean.config b/collector/src/test/resources/testyaml_javabean.config
57a889
new file mode 100644
57a889
index 0000000..6552d83
57a889
--- /dev/null
57a889
+++ b/collector/src/test/resources/testyaml_javabean.config
57a889
@@ -0,0 +1,6 @@
57a889
+lowercaseOutputName: true
57a889
+lowercaseOutputLabelNames: true
57a889
+blacklistObjectNames:
57a889
+# handled by agent's default exporter
57a889
+- "java.lang:*"
57a889
+other: !!io.prometheus.jmx.Person { firstName: Andrey, lastName: Tool }
57a889
-- 
57a889
2.38.1
57a889