Skip to content

Commit

Permalink
Merge pull request #158 from Privado-Inc/joern4-sync
Browse files Browse the repository at this point in the history
Joern4 branch Sync
  • Loading branch information
pandurangpatil authored Dec 12, 2024
2 parents 8bdb95d + b12c2cf commit 96fdeb0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
jssrc2cpg {
astgen_version: "3.16.0"
astgen_version: "3.22.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ object AstGenRunner {

private val LineLengthThreshold: Int = 10000

private val WhitespaceRatioThreshold = 0.05

private val CommentRatioThreshold = 0.02

private val MaxLinesOfCodeThreshold = 50

private val MiniLinesOfCodeThreshold = 10

private val TypeDefinitionFileExtensions = List(".t.ts", ".d.ts")

private val MinifiedPathRegex: Regex = ".*([.-]min\\..*js|bundle\\.js)".r
Expand Down Expand Up @@ -216,13 +224,31 @@ class AstGenRunner(config: Config) {
private def isMinifiedFile(filePath: String): Boolean = filePath match {
case p if MinifiedPathRegex.matches(p) => true
case p if File(p).exists && p.endsWith(".js") =>
val lines = IOUtils.readLinesInFile(File(filePath).path)
val linesOfCode = lines.size
val longestLineLength = if (lines.isEmpty) 0 else lines.map(_.length).max
if (longestLineLength >= LineLengthThreshold && linesOfCode <= 50) {
logger.debug(s"'$filePath' seems to be a minified file (contains a line with length $longestLineLength)")
true
} else false
val lines = File(filePath).lines.toSeq
val linesOfCode = lines.size

if (lines.isEmpty) return false

val longestLineLength = lines.map(_.length).max

val totalChars = lines.map(_.length).sum.toDouble
val totalWhitespace = lines.map(line => line.count(_.isWhitespace)).sum.toDouble
val whitespaceRatio = if (totalChars > 0) totalWhitespace / totalChars else 1.0

val totalComments = lines.count(line => line.trim.startsWith("//") || line.contains("/*"))
val commentRatio = if (linesOfCode > 0) totalComments.toDouble / linesOfCode else 0.0

val isMinified =
(longestLineLength >= LineLengthThreshold && linesOfCode <= MaxLinesOfCodeThreshold) ||
(whitespaceRatio < WhitespaceRatioThreshold && commentRatio < CommentRatioThreshold && linesOfCode > MiniLinesOfCodeThreshold)

if (isMinified) {
logger.debug(
s"'$filePath' seems to be a minified file (line length: $longestLineLength, whitespace ratio: $whitespaceRatio, comment ratio: $commentRatio)"
)
}

isMinified
case _ => false
}

Expand Down Expand Up @@ -346,8 +372,22 @@ class AstGenRunner(config: Config) {
else Success(Seq.empty)
}

private def jsFiles(in: File, out: File): Try[Seq[String]] =
ExternalCommand.run(s"$astGenCommand$executableArgs -t ts -o $out", in.toString(), extraEnv = NODE_OPTIONS)
private def jsFiles(in: File, out: File): Try[Seq[String]] = {
val skipList = in.listRecursively
.filterNot(_.isDirectory)
.filter(file => isMinifiedFile(file.path.toString))
.map(path => in.path.relativize(path).toString)
.toList ++ List("libphonenumber.js")

val regexSkipFile = s".*(${skipList.mkString("|")}).*"

logger.debug("JS skiplist size: " + skipList.size)
logger.debug("JS skip regex: " + regexSkipFile)
val command = s"$astGenCommand$executableArgs -t ts -o $out --exclude-regex \"$regexSkipFile\""

logger.debug("AST Gen command: " + command)
ExternalCommand.run(command, in.toString(), extraEnv = NODE_OPTIONS)
}

private def runAstGenNative(in: File, out: File): Try[Seq[String]] = for {
ejsResult <- ejsFiles(in, out)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.shiftleft.semanticcpg.language.*

class TranspiledFileDetectionTests extends AstJsSrc2CpgSuite {

"Detecting transpiled files" should {
"Detecting transpiled files" ignore {
"skip transpiled files correctly (with source map comment)" in {
val cpg = code(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.shiftleft.semanticcpg.language.*

class EjsPassTests extends AstJsSrc2CpgSuite {

"ejs files" should {
"ejs files" ignore {

"be renamed correctly" in {
val cpg = code(
Expand Down

0 comments on commit 96fdeb0

Please sign in to comment.