From 0db78f9c3521e43df841af393078189e05d6d009 Mon Sep 17 00:00:00 2001 From: Pandurang Patil Date: Thu, 7 Dec 2023 17:07:34 +0530 Subject: [PATCH] [jssrc2cpg] - Graceful handling of error situation for jssrc2cpg (#3911) While processing `.ejs` files, if the astgen throws any error while parsing the file. We encoutner this error in Finish method where we read skiped file contents for the report. In case of `.ejs` files as we preprocess and convert that into `.js` file. And for some reason this `.js` file is not present at the expected place, we get file not found exception. I have handled the situation gracefully in order to avoid breaking the entire process of generating the CPG. --- .../io/joern/jssrc2cpg/passes/AstCreationPass.scala | 7 ++++++- .../joern/jssrc2cpg/preprocessing/EjsPassTest.scala | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/passes/AstCreationPass.scala b/joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/passes/AstCreationPass.scala index fb6f6ea449ce..5d2c42c3c182 100644 --- a/joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/passes/AstCreationPass.scala +++ b/joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/passes/AstCreationPass.scala @@ -26,7 +26,12 @@ class AstCreationPass(cpg: Cpg, astGenRunnerResult: AstGenRunnerResult, config: astGenRunnerResult.skippedFiles.foreach { skippedFile => val (rootPath, fileName) = skippedFile val filePath = Paths.get(rootPath, fileName) - val fileLOC = IOUtils.readLinesInFile(filePath).size + val fileLOC = Try(IOUtils.readLinesInFile(filePath)) match { + case Success(filecontent) => filecontent.size + case Failure(exception) => + logger.warn(s"Failed to read file: '${filePath}'", exception) + 0 + } report.addReportInfo(fileName, fileLOC) } } diff --git a/joern-cli/frontends/jssrc2cpg/src/test/scala/io/joern/jssrc2cpg/preprocessing/EjsPassTest.scala b/joern-cli/frontends/jssrc2cpg/src/test/scala/io/joern/jssrc2cpg/preprocessing/EjsPassTest.scala index 1e55706db69e..f60bdf28001e 100644 --- a/joern-cli/frontends/jssrc2cpg/src/test/scala/io/joern/jssrc2cpg/preprocessing/EjsPassTest.scala +++ b/joern-cli/frontends/jssrc2cpg/src/test/scala/io/joern/jssrc2cpg/preprocessing/EjsPassTest.scala @@ -70,6 +70,17 @@ class EjsPassTest extends AbstractPassTest { "user.name" ) } + + "invalid EJS file test" in AstFixture( + """ + | + |

Welcome <%@#$= user.name %>

+ | + |""".stripMargin, + "index.js.ejs" + ) { cpg => + cpg.file.l.size shouldBe 0 + } } }