Skip to content

Commit

Permalink
SONARJAVA-3452 Catch LinkageErrors when transpiling JSP files
Browse files Browse the repository at this point in the history
  • Loading branch information
saberduck authored and Wohops committed Jul 1, 2020
1 parent e9b96be commit 83734b1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<e:forHtml value="${param.name}" /> <!-- Compliant: Sanitized with e:forHtml where "e" stands for OWASP Encoder -->
<h1>${e:forHtml(param.name)}</h1> <!-- Compliant: Sanitized with e:forHtml where "e" stands for OWASP Encoder -->

<c:if test="true">
<b>true</b>
</c:if>
</body>

</html>
10 changes: 10 additions & 0 deletions java-jsp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@
<artifactId>spring-webmvc</artifactId>
<version>5.2.3.RELEASE</version>
</artifactItem>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
</artifactItem>
<artifactItem>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/test-jars</outputDirectory>
</configuration>
Expand Down
8 changes: 6 additions & 2 deletions java-jsp/src/main/java/org/sonar/java/jsp/Jasper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
Expand Down Expand Up @@ -87,9 +89,11 @@ public Collection<GeneratedFile> generateFiles(SensorContext sensorContext, List
try {
Path generatedFile = transpileJsp(jsp.path(), uriRoot, classLoader, servletContext, options, runtimeContext);
generatedJavaFiles.put(generatedFile, new GeneratedFile(generatedFile));
} catch (Exception e) {
} catch (Exception | LinkageError e) {
errorTranspiling = true;
LOG.debug("Error transpiling " + jsp, e);
StringWriter w = new StringWriter();
e.printStackTrace(new PrintWriter(w));
LOG.debug("Error transpiling {}. Error:\n{}", jsp, w.toString());
}
}
if (errorTranspiling) {
Expand Down
30 changes: 28 additions & 2 deletions java-jsp/src/test/java/org/sonar/java/jsp/JasperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.java.model.GeneratedFile;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -76,6 +77,8 @@ class JasperTest {
public LogTester logTester = new LogTester();
private Path jspFile;
private final File springJar = Paths.get("target/test-jars/spring-webmvc-5.2.3.RELEASE.jar").toFile();
private final File jstlJar = Paths.get("target/test-jars/jstl-1.2.jar").toFile();
private final File jee6Jar = Paths.get("target/test-jars/javaee-web-api-6.0.jar").toFile();

@BeforeEach
void setUp() throws Exception {
Expand Down Expand Up @@ -127,6 +130,24 @@ void test_with_classpath() throws Exception {
assertThat(generatedCode).contains(" org.springframework.web.servlet.tags.UrlTag _jspx_th_spring_005furl_005f0 = new org.springframework.web.servlet.tags.UrlTag();");
}


@Test
void test_with_classpath_jee6_jstl() throws Exception {
SensorContextTester ctx = jspContext(
"<%@ taglib uri = \"http://java.sun.com/jsp/jstl/core\" prefix = \"c\" %>\n" +
"<html>\n" +
"<body>\n" +
"<h2>Hello World!</h2>\n" +
"<c:if test=\"true\">what-if</c:if>\n" +
"</body>\n" +
"</html>");
Collection<GeneratedFile> generatedFiles = new Jasper().generateFiles(ctx, asList(jee6Jar, jstlJar));

assertThat(generatedFiles).isEmpty();
assertThat(logTester.logs(LoggerLevel.DEBUG)).matches(logs -> logs.stream().anyMatch(line ->
line.startsWith("Error transpiling src/main/webapp/WEB-INF/jsp/test.jsp. Error:\njava.lang.ClassFormatError")));
}

@Test
void test_compilation_without_webinf() throws Exception {
SensorContext ctx = jspContext(JSP_SOURCE, tempFolder.resolve("test.jsp"));
Expand All @@ -144,7 +165,9 @@ void test_exception_handling() throws Exception {
SensorContextTester ctx = jspContext("<%=");
Collection<GeneratedFile> inputFiles = new Jasper().generateFiles(ctx, emptyList());
assertThat(inputFiles).isEmpty();
assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Error transpiling src/main/webapp/WEB-INF/jsp/test.jsp");
assertThat(logTester.logs(LoggerLevel.DEBUG))
.matches(logs -> logs.stream().anyMatch(line ->
line.startsWith("Error transpiling src/main/webapp/WEB-INF/jsp/test.jsp.")));
assertThat(logTester.logs(LoggerLevel.WARN)).contains("Some JSP pages failed to transpile. Enable debug log for details.");
}

Expand Down Expand Up @@ -235,7 +258,10 @@ void test_failing_tag_compilation() throws Exception {
stream().collect(Collectors.toMap(GeneratedFile::filename, f -> f));

assertThat(generatedFiles).isEmpty();
assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Error transpiling src/main/webapp/WEB-INF/jsp/test.jsp");
assertThat(logTester.logs(LoggerLevel.DEBUG))
.matches(logs -> logs.stream().anyMatch(line ->
line.startsWith("Error transpiling src/main/webapp/WEB-INF/jsp/test.jsp. Error:\norg.apache.jasper.JasperException:")));

}

private SensorContextTester jspContext(String jspSource) throws IOException {
Expand Down

0 comments on commit 83734b1

Please sign in to comment.