Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python #24

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "chen"
ThisBuild / organization := "io.appthreat"
ThisBuild / version := "0.0.21"
ThisBuild / version := "0.5.0"
ThisBuild / scalaVersion := "3.3.1"

val cpgVersion = "1.4.22"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import io.appthreat.x2cpg.X2Cpg.withNewEmptyCpg
import io.appthreat.x2cpg.X2CpgFrontend
import io.appthreat.x2cpg.datastructures.Global
import io.appthreat.x2cpg.passes.frontend.{MetaDataPass, TypeNodePass}
import io.appthreat.x2cpg.passes.taggers.CdxPass
import io.shiftleft.codepropertygraph.Cpg
import org.slf4j.LoggerFactory
import soot.options.Options
Expand Down Expand Up @@ -120,7 +119,6 @@ class Jimple2Cpg extends X2CpgFrontend[Config] {
.createAndApply()
DeclarationRefPass(cpg).createAndApply()
new ConfigFileCreationPass(cpg).createAndApply()
new CdxPass(cpg).createAndApply()
}

override def createCpg(config: Config): Try[Cpg] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2019,7 +2019,9 @@ object PythonAstVisitor {
// There is a corresponding list in policies which needs to be updated if this one is updated and vice versa.
val builtinFunctionsV3: Iterable[String] = Iterable(
"abs",
"aiter",
"all",
"anext",
"any",
"ascii",
"bin",
Expand Down Expand Up @@ -2051,6 +2053,7 @@ object PythonAstVisitor {
"locals",
"map",
"max",
"memoryview",
"min",
"next",
"oct",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.shiftleft.codepropertygraph.generated.Languages
import io.shiftleft.passes.CpgPass
import io.shiftleft.semanticcpg.language.*

import java.io.File
import java.util.regex.Pattern
import scala.collection.mutable
import scala.io.Source
Expand Down Expand Up @@ -45,6 +46,16 @@ class CdxPass(atom: Cpg) extends CpgPass(atom) {

private val BOM_JSON_FILE = ".*(bom|cdx).json"

private def toPyModuleForm(str: String) = {
if (str.nonEmpty && str.count(_ == '.') > 0) {
s".*${str.split("\\.").take(2).mkString(Pattern.quote(File.separator))}.*"
} else if (str.nonEmpty) {
s"$str${Pattern.quote(File.separator)}.*"
} else {
str
}
}

override def run(dstGraph: DiffGraphBuilder): Unit = {
atom.configFile.name(BOM_JSON_FILE).content.foreach { cdxData =>
val cdxJson = parse(cdxData).getOrElse(Json.Null)
Expand Down Expand Up @@ -72,12 +83,19 @@ class CdxPass(atom: Cpg) extends CpgPass(atom) {
.filterNot(_.contains("mock"))
.filterNot(_.endsWith(".lock"))
.filterNot(_.endsWith(".json"))
.filterNot(_.endsWith(".txt"))
.foreach { (pkg: String) =>
var bpkg = pkg.takeWhile(_ != '$')
if (language == Languages.JAVA || language == Languages.JAVASRC)
if (language == Languages.JAVA || language == Languages.JAVASRC) {
bpkg = bpkg.split("\\.").take(PKG_NS_SIZE).mkString(".").concat(".*")
if (language == Languages.JSSRC || language == Languages.JAVASCRIPT) bpkg = s".*${bpkg}.*"
if (!donePkgs.contains(bpkg)) {
bpkg = bpkg.replace(File.separator, Pattern.quote(File.separator))
}
if (language == Languages.JSSRC || language == Languages.JAVASCRIPT) {
bpkg = s".*${bpkg}.*"
bpkg = bpkg.replace(File.separator, Pattern.quote(File.separator))
}
if (language == Languages.PYTHON || language == Languages.PYTHONSRC) bpkg = toPyModuleForm(bpkg)
if (bpkg.nonEmpty && !donePkgs.contains(bpkg)) {
donePkgs.put(bpkg, true)
if (!containsRegex(bpkg)) {
atom.call.typeFullNameExact(bpkg).newTagNode(compPurl).store()(dstGraph)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "appthreat-chen"
version = "0.0.21"
version = "0.5.0"
description = "Code Hierarchy Exploration Net (chen)"
authors = ["Team AppThreat <cloud@appthreat.com>"]
license = "Apache-2.0"
Expand Down