From 30712bbbabb40dace46e7d8cf7e0860e69ed134f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20Leuth=C3=A4user?= <1417198+max-leuthaeuser@users.noreply.github.com> Date: Tue, 7 Nov 2023 14:45:59 +0100 Subject: [PATCH] Fixed handling of empty source map entries (#316) Source maps may contain empty map entries / empty file paths. Fixes: failing gazelle tests (e.g., see and test `npm pack morphi`) --- project/plugins.sbt | 1 - .../scala/io/shiftleft/js2cpg/parser/JsSource.scala | 11 +++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/project/plugins.sbt b/project/plugins.sbt index 0dbf0806..fa69a01f 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -2,5 +2,4 @@ addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.9.16") addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.0") addSbtPlugin("io.shiftleft" % "sbt-ci-release-early" % "2.0.27") addSbtPlugin("com.dwijnand" % "sbt-dynver" % "4.1.1") -addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.1.0") addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.10.0") diff --git a/src/main/scala/io/shiftleft/js2cpg/parser/JsSource.scala b/src/main/scala/io/shiftleft/js2cpg/parser/JsSource.scala index cab030f2..b28e9d29 100644 --- a/src/main/scala/io/shiftleft/js2cpg/parser/JsSource.scala +++ b/src/main/scala/io/shiftleft/js2cpg/parser/JsSource.scala @@ -82,8 +82,10 @@ class JsSource(val srcDir: File, val projectDir: Path, val source: Source) { } } - private def constructSourceFilePath(sourceFileName: String): File = sourceFileName match { - case _ if absoluteFilePath.contains(NuxtTranspiler.NUXT_FOLDER) && srcDir.path.compareTo(projectDir) == 0 => + private def constructSourceFilePath(sourceFileName: String): File = { + if (sourceFileName.isEmpty) { + srcDir / source.getName + } else if (absoluteFilePath.contains(NuxtTranspiler.NUXT_FOLDER) && srcDir.path.compareTo(projectDir) == 0) { // For nuxt-js transpilation we have the same src and project dir and we need some special handling here if (sourceFileName.startsWith(WEBPACK_PREFIX)) { val replacedName = FileUtils.cleanPath(sourceFileName.replace(WEBPACK_PREFIX, "")) @@ -91,11 +93,11 @@ class JsSource(val srcDir: File, val projectDir: Path, val source: Source) { } else { File(absoluteFilePath).parent / sourceFileName } - case _ if sourceFileName.startsWith(WEBPACK_PREFIX) => + } else if (sourceFileName.startsWith(WEBPACK_PREFIX)) { // Additionally, source map files coming from webpack (e.g., from Vue transpilation) are somewhat hidden val replacedName = sourceFileName.replace(WEBPACK_PREFIX, "") srcDir / replacedName.substring(replacedName.indexOf("/") + 1) - case _ => + } else { val cleanedPath = FileUtils.cleanPath(sourceFileName) // having "/" here is fine as JS source maps always have platform independent path separators val lookupPath = if (cleanedPath.contains("/" + srcDir.name + "/")) { @@ -115,6 +117,7 @@ class JsSource(val srcDir: File, val projectDir: Path, val source: Source) { } } srcFilePath + } } private def sourceMapOrigin(): Option[SourceMapOrigin] = {