Blame SOURCES/0001-Fix-CVE-2017-18640-and-add-a-test.patch

d1445f
From 8cbe3bee31e605d535783431bfb74c3e2867ee37 Mon Sep 17 00:00:00 2001
d1445f
From: Severin Gehwolf <sgehwolf@redhat.com>
d1445f
Date: Wed, 15 Apr 2020 17:41:17 +0200
d1445f
Subject: [PATCH] Fix CVE-2017-18640 and add a test
d1445f
d1445f
---
d1445f
 collector/pom.xml                             |  2 +-
d1445f
 .../java/io/prometheus/jmx/JmxCollector.java  | 12 ++++++++-
d1445f
 .../io/prometheus/jmx/JmxCollectorTest.java   | 26 ++++++++++++++++---
d1445f
 collector/src/test/resources/testyaml.config  |  9 +++++++
d1445f
 4 files changed, 44 insertions(+), 5 deletions(-)
d1445f
 create mode 100644 collector/src/test/resources/testyaml.config
d1445f
d1445f
diff --git a/collector/pom.xml b/collector/pom.xml
d1445f
index 8e90d58..b58d94a 100644
d1445f
--- a/collector/pom.xml
d1445f
+++ b/collector/pom.xml
d1445f
@@ -29,7 +29,7 @@
d1445f
     <dependency>
d1445f
       <groupId>org.yaml</groupId>
d1445f
       <artifactId>snakeyaml</artifactId>
d1445f
-      <version>1.16</version>
d1445f
+      <version>1.26</version>
d1445f
     </dependency>
d1445f
   </dependencies>
d1445f
 
d1445f
diff --git a/collector/src/main/java/io/prometheus/jmx/JmxCollector.java b/collector/src/main/java/io/prometheus/jmx/JmxCollector.java
d1445f
index f06d3ef..a5fc96f 100644
d1445f
--- a/collector/src/main/java/io/prometheus/jmx/JmxCollector.java
d1445f
+++ b/collector/src/main/java/io/prometheus/jmx/JmxCollector.java
d1445f
@@ -3,6 +3,7 @@ package io.prometheus.jmx;
d1445f
 import io.prometheus.client.Collector;
d1445f
 import io.prometheus.client.Counter;
d1445f
 import org.yaml.snakeyaml.Yaml;
d1445f
+import org.yaml.snakeyaml.error.YAMLException;
d1445f
 
d1445f
 import javax.management.MalformedObjectNameException;
d1445f
 import javax.management.ObjectName;
d1445f
@@ -71,7 +72,16 @@ public class JmxCollector extends Collector implements Collector.Describable {
d1445f
 
d1445f
     public JmxCollector(File in) throws IOException, MalformedObjectNameException {
d1445f
         configFile = in;
d1445f
-        config = loadConfig((Map<String, Object>)new Yaml().load(new FileReader(in)));
d1445f
+        // Be defensive about loading yaml from a user. In some cases YAMLException
d1445f
+        // will be thrown for bad configs
d1445f
+        Map<String, Object> yamlConfig = null;
d1445f
+        try {
d1445f
+            yamlConfig = (Map<String, Object>)new Yaml().load(new FileReader(in));
d1445f
+        } catch (YAMLException e) {
d1445f
+            System.err.println("YAML configuration error: " + e.getMessage());
d1445f
+            throw new IllegalArgumentException(e);
d1445f
+        }
d1445f
+        config = loadConfig(yamlConfig);
d1445f
         config.lastUpdate = configFile.lastModified();
d1445f
     }
d1445f
 
d1445f
diff --git a/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java b/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java
d1445f
index dc35734..3d4ecf0 100644
d1445f
--- a/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java
d1445f
+++ b/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java
d1445f
@@ -3,15 +3,21 @@ package io.prometheus.jmx;
d1445f
 import static org.junit.Assert.assertEquals;
d1445f
 import static org.junit.Assert.assertNotNull;
d1445f
 import static org.junit.Assert.assertNull;
d1445f
+import static org.junit.Assert.assertTrue;
d1445f
 import static org.junit.Assert.fail;
d1445f
 
d1445f
-import io.prometheus.client.Collector;
d1445f
-import io.prometheus.client.CollectorRegistry;
d1445f
+import java.io.File;
d1445f
 import java.lang.management.ManagementFactory;
d1445f
+
d1445f
 import javax.management.MBeanServer;
d1445f
-import org.junit.Test;
d1445f
+
d1445f
 import org.junit.Before;
d1445f
 import org.junit.BeforeClass;
d1445f
+import org.junit.Test;
d1445f
+import org.yaml.snakeyaml.error.YAMLException;
d1445f
+
d1445f
+import io.prometheus.client.Collector;
d1445f
+import io.prometheus.client.CollectorRegistry;
d1445f
 
d1445f
 
d1445f
 public class JmxCollectorTest {
d1445f
@@ -252,4 +258,18 @@ public class JmxCollectorTest {
d1445f
       Thread.sleep(2000);
d1445f
       assertEquals(1.0, registry.getSampleValue("boolean_Test_True", new String[]{}, new String[]{}), .001);
d1445f
     }
d1445f
+
d1445f
+    @Test
d1445f
+    public void testBillionLaughs() throws Exception {
d1445f
+      File configFile = new File(getClass().getResource("/testyaml.config").getPath());
d1445f
+      assertTrue(configFile.exists());
d1445f
+      try {
d1445f
+        JmxCollector jc = new JmxCollector(configFile);
d1445f
+        fail("Excected yaml exception due to billion laughs");
d1445f
+      } catch (IllegalArgumentException e) {
d1445f
+        Throwable ex = e.getCause();
d1445f
+        String prefix = YAMLException.class.getName() + ": ";
d1445f
+        assertEquals(prefix + "Number of aliases for non-scalar nodes exceeds the specified max=50", e.getMessage());
d1445f
+      }
d1445f
+    }
d1445f
 }
d1445f
diff --git a/collector/src/test/resources/testyaml.config b/collector/src/test/resources/testyaml.config
d1445f
new file mode 100644
d1445f
index 0000000..4a3ed69
d1445f
--- /dev/null
d1445f
+++ b/collector/src/test/resources/testyaml.config
d1445f
@@ -0,0 +1,9 @@
d1445f
+a: &a ["lol","lol","lol","lol","lol","lol","lol","lol","lol"]
d1445f
+b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a]
d1445f
+c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b]
d1445f
+d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c]
d1445f
+e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d]
d1445f
+f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e]
d1445f
+g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]
d1445f
+h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g]
d1445f
+i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h]
d1445f
\ No newline at end of file
d1445f
-- 
d1445f
2.21.1
d1445f