Skip to content

Commit 3a85d6c

Browse files
authored
Fix vulnerabilty (#42)
don't use printStackTrace() when catching exception
1 parent 1860fd5 commit 3a85d6c

File tree

5 files changed

+50
-58
lines changed

5 files changed

+50
-58
lines changed

community-rust-plugin/src/main/java/org/elegoff/plugins/communityrust/RustTokensVisitor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
* Copyright (C) 2021 Eric Le Goff
44
* mailto:community-rust AT pm DOT me
55
* http://github.com/elegoff/sonar-rust
6-
* <p>
6+
*
77
* This program is free software; you can redistribute it and/or
88
* modify it under the terms of the GNU Lesser General Public
99
* License as published by the Free Software Foundation; either
1010
* version 3 of the License, or (at your option) any later version.
11-
* <p>
11+
*
1212
* This program is distributed in the hope that it will be useful,
1313
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1515
* Lesser General Public License for more details.
16-
* <p>
16+
*
1717
* You should have received a copy of the GNU Lesser General Public License
1818
* along with this program; if not, write to the Free Software Foundation,
1919
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

community-rust-plugin/src/main/java/org/elegoff/plugins/communityrust/xunit/StaxParser.java

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,69 +20,52 @@
2020
*/
2121
package org.elegoff.plugins.communityrust.xunit;
2222

23-
import org.apache.commons.lang.StringUtils;
2423
import org.codehaus.staxmate.SMInputFactory;
2524
import org.codehaus.staxmate.in.SMHierarchicCursor;
2625

2726
import javax.xml.stream.XMLInputFactory;
28-
import javax.xml.stream.XMLResolver;
2927
import javax.xml.stream.XMLStreamException;
3028
import java.io.File;
3129
import java.io.FileInputStream;
3230
import java.io.IOException;
3331
import java.io.InputStream;
34-
public class StaxParser {
3532

36-
private SMInputFactory inf;
33+
public class StaxParser {
3734

38-
private XmlStreamHandler streamHandler;
35+
private SMInputFactory inf;
3936

40-
public StaxParser(XmlStreamHandler streamHandler) {
41-
this.streamHandler = streamHandler;
42-
XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
37+
private XmlStreamHandler streamHandler;
4338

44-
inf = new SMInputFactory(xmlFactory);
45-
}
39+
public StaxParser(XmlStreamHandler streamHandler) {
40+
this.streamHandler = streamHandler;
41+
XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
4642

47-
public void parse(File xmlFile) throws XMLStreamException {
48-
try (FileInputStream input = new FileInputStream(xmlFile)) {
49-
parse(input);
50-
} catch (IOException e) {
51-
throw new XMLStreamException(e);
43+
inf = new SMInputFactory(xmlFactory);
5244
}
53-
}
5445

55-
public void parse(InputStream xmlInput) throws XMLStreamException {
56-
SMHierarchicCursor rootCursor = inf.rootElementCursor(xmlInput);
57-
try {
58-
streamHandler.stream(rootCursor);
59-
} finally {
60-
rootCursor.getStreamReader().closeCompletely();
46+
public void parse(File xmlFile) throws XMLStreamException {
47+
try (FileInputStream input = new FileInputStream(xmlFile)) {
48+
parse(input);
49+
} catch (IOException e) {
50+
throw new XMLStreamException(e);
51+
}
6152
}
62-
}
6353

64-
private static class UndeclaredEntitiesXMLResolver implements XMLResolver {
65-
66-
@Override
67-
public Object resolveEntity(String arg0, String arg1, String fileName, String undeclaredEntity) throws XMLStreamException {
68-
String undeclared = undeclaredEntity;
69-
// avoid problems with XML docs containing undeclared entities.. return the entity under its raw form if not a Unicode expression
70-
if (StringUtils.startsWithIgnoreCase(undeclaredEntity, "u") && undeclaredEntity.length() == 5) {
71-
int unicodeCharHexValue = Integer.parseInt(undeclaredEntity.substring(1), 16);
72-
if (Character.isDefined(unicodeCharHexValue)) {
73-
undeclared = new String(new char[] {(char) unicodeCharHexValue});
54+
public void parse(InputStream xmlInput) throws XMLStreamException {
55+
SMHierarchicCursor rootCursor = inf.rootElementCursor(xmlInput);
56+
try {
57+
streamHandler.stream(rootCursor);
58+
} finally {
59+
rootCursor.getStreamReader().closeCompletely();
7460
}
75-
}
76-
return undeclared;
7761
}
78-
}
7962

80-
/**
81-
* Simple interface for handling XML stream to parse.
82-
*/
83-
@FunctionalInterface
84-
public interface XmlStreamHandler {
85-
void stream(SMHierarchicCursor rootCursor) throws XMLStreamException;
86-
}
63+
/**
64+
* Simple interface for handling XML stream to parse.
65+
*/
66+
@FunctionalInterface
67+
public interface XmlStreamHandler {
68+
void stream(SMHierarchicCursor rootCursor) throws XMLStreamException;
69+
}
8770

8871
}

community-rust-plugin/src/main/java/org/elegoff/plugins/communityrust/xunit/TestCase.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,11 @@ public class TestCase {
3737

3838
/**
3939
* Constructs a testcase instance out of following parameters
40-
* @param name The name of this testcase
4140
* @param status The execution status of the testcase
42-
* @param stackTrace The stack trace occurred while executing of this testcase; pass "" if the testcase passed/skipped.
43-
* @param errorMessage The error message associated with this testcase of the execution was erroneous; pass "" if not.
4441
* @param time The execution time in milliseconds
4542
* @param file The optional file to which this test case applies.
46-
* @param testClassname The classname of the test.
4743
*/
48-
public TestCase(String name, TestCaseStatus status, String stackTrace, String errorMessage, int time, @Nullable String file, @Nullable String testClassname) {
44+
public TestCase(String name, TestCaseStatus status, String stackTrace, String errorMessage, int time, @Nullable String file, @Nullable String testClassname) {
4945
this.name = name;
5046
this.status = status;
5147
this.stackTrace = stackTrace;
@@ -54,16 +50,18 @@ public TestCase(String name, TestCaseStatus status, String stackTrace, String er
5450
this.file = file;
5551
this.testClassname = testClassname;
5652
}
53+
5754
/**
5855
* Returns true if this testcase is an error, false otherwise
5956
*/
60-
public boolean isError(){
57+
public boolean isError() {
6158
return TestCaseStatus.ERROR.equals(status);
6259
}
60+
6361
/**
6462
* Returns true if this testcase is a failure, false otherwise
6563
*/
66-
public boolean isFailure(){
64+
public boolean isFailure() {
6765
return TestCaseStatus.FAILURE.equals(status);
6866
}
6967

@@ -82,6 +80,22 @@ public String getFile() {
8280
return file;
8381
}
8482

83+
public String getName() {
84+
return name;
85+
}
86+
87+
public TestCaseStatus getStatus() {
88+
return status;
89+
}
90+
91+
public String getStackTrace() {
92+
return stackTrace;
93+
}
94+
95+
public String getErrorMessage() {
96+
return errorMessage;
97+
}
98+
8599
public String getTestClassname() {
86100
return testClassname;
87101
}

community-rust-plugin/src/main/java/org/elegoff/plugins/communityrust/xunit/XUnitSensor.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public void describe(SensorDescriptor descriptor) {
5050
.name("XUnit Sensor for Rust")
5151
.onlyOnLanguage(RustLanguage.KEY)
5252
.onlyOnFileType(InputFile.Type.MAIN);
53-
;
5453
}
5554

5655
@Override
@@ -64,7 +63,6 @@ public void execute(SensorContext context) {
6463
processReports(context, reports);
6564
} catch (Exception e) {
6665
LOG.warn("Cannot read report '{}', the following exception occurred: {}", reportPath, e.getMessage());
67-
e.printStackTrace();
6866
}
6967
}
7068

community-rust-plugin/src/test/java/org/elegoff/plugins/communityrust/xunit/XUnitSensorTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ public class XUnitSensorTest {
4646
@Rule
4747
public LogTester logTester = new LogTester();
4848

49-
@Rule
50-
public TemporaryFolder tmpDir = new TemporaryFolder();
51-
5249
@Before
5350
public void init() {
5451

0 commit comments

Comments
 (0)