From 843a3ed0aa3c505b2b74002a4fd555a693f392e7 Mon Sep 17 00:00:00 2001 From: hengyunabc Date: Tue, 27 Apr 2021 10:26:10 +0800 Subject: [PATCH] fix commons-io security problem. #1782 --- memorycompiler/pom.xml | 7 ---- .../arthas/compiler/DynamicCompilerTest.java | 40 +++++++++++++++++-- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/memorycompiler/pom.xml b/memorycompiler/pom.xml index f13ccd522..b239fde91 100644 --- a/memorycompiler/pom.xml +++ b/memorycompiler/pom.xml @@ -11,13 +11,6 @@ arthas-memorycompiler - - commons-io - commons-io - 2.6 - test - - org.slf4j slf4j-api diff --git a/memorycompiler/src/test/java/com/taobao/arthas/compiler/DynamicCompilerTest.java b/memorycompiler/src/test/java/com/taobao/arthas/compiler/DynamicCompilerTest.java index 46348feaa..fe6d064fc 100644 --- a/memorycompiler/src/test/java/com/taobao/arthas/compiler/DynamicCompilerTest.java +++ b/memorycompiler/src/test/java/com/taobao/arthas/compiler/DynamicCompilerTest.java @@ -1,14 +1,14 @@ package com.taobao.arthas.compiler; +import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.net.URL; import java.net.URLClassLoader; -import java.nio.charset.Charset; import java.util.Map; -import org.apache.commons.io.IOUtils; import org.junit.Assert; import org.junit.Test; import org.slf4j.LoggerFactory; @@ -33,8 +33,8 @@ public class DynamicCompilerTest { InputStream logger1Stream = DynamicCompilerTest.class.getClassLoader().getResourceAsStream("TestLogger1.java"); InputStream logger2Stream = DynamicCompilerTest.class.getClassLoader().getResourceAsStream("TestLogger2.java"); - dynamicCompiler.addSource("TestLogger2", IOUtils.toString(logger2Stream, Charset.defaultCharset())); - dynamicCompiler.addSource("TestLogger1", IOUtils.toString(logger1Stream, Charset.defaultCharset())); + dynamicCompiler.addSource("TestLogger2", toString(logger2Stream)); + dynamicCompiler.addSource("TestLogger1", toString(logger1Stream)); Map byteCodes = dynamicCompiler.buildByteCodes(); @@ -42,4 +42,36 @@ public class DynamicCompilerTest { Assert.assertTrue("TestLogger2", byteCodes.containsKey("com.hello.TestLogger2")); } + /** + * Get the contents of an InputStream as a String + * using the default character encoding of the platform. + *

+ * This method buffers the input internally, so there is no need to use a + * BufferedInputStream. + * + * @param input the InputStream to read from + * @return the requested String + * @throws NullPointerException if the input is null + * @throws IOException if an I/O error occurs + */ + public static String toString(InputStream input) throws IOException { + BufferedReader br = null; + try { + StringBuilder sb = new StringBuilder(); + br = new BufferedReader(new InputStreamReader(input)); + String line; + while ((line = br.readLine()) != null) { + sb.append(line).append("\n"); + } + return sb.toString(); + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException e) { + // ignore + } + } + } + } }