-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dataflowengineoss] Turn
Semantics
into a node-directed trait (#4920)
* Refactor `Semantics` -> `FullNameSemantics` * Refactor `FlowSemantic`/`FlowNode`/`FlowPath` out of the FullNameSemantics parser * Rename `Parser` -> `FullNameSemanticsParser` * Introduce `Semantics` trait with `initialize` and `forMethod`
- Loading branch information
1 parent
19011fb
commit 3a530c0
Showing
32 changed files
with
301 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...gineoss/src/main/scala/io/joern/dataflowengineoss/semanticsloader/FullNameSemantics.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.joern.dataflowengineoss.semanticsloader | ||
|
||
import io.shiftleft.codepropertygraph.generated.Cpg | ||
import io.shiftleft.codepropertygraph.generated.nodes.Method | ||
import io.shiftleft.semanticcpg.language.* | ||
|
||
import scala.collection.mutable | ||
|
||
object FullNameSemantics { | ||
|
||
def fromList(elements: List[FlowSemantic]): FullNameSemantics = { | ||
new FullNameSemantics( | ||
mutable.Map.newBuilder | ||
.addAll(elements.map { e => | ||
e.methodFullName -> e | ||
}) | ||
.result() | ||
) | ||
} | ||
|
||
def empty: FullNameSemantics = fromList(List()) | ||
|
||
} | ||
|
||
class FullNameSemantics private (methodToSemantic: mutable.Map[String, FlowSemantic]) extends Semantics { | ||
|
||
/** The map below keeps a mapping between results of a regex and the regex string it matches. e.g. | ||
* | ||
* `path/to/file.py:<module>.Foo.sink` -> `^path.*Foo\\.sink$` | ||
*/ | ||
private val regexMatchedFullNames = mutable.HashMap.empty[String, String] | ||
|
||
/** Initialize all the method semantics that use regex with all their regex results before query time. | ||
*/ | ||
override def initialize(cpg: Cpg): Unit = { | ||
import io.shiftleft.semanticcpg.language._ | ||
|
||
methodToSemantic.filter(_._2.regex).foreach { case (regexString, _) => | ||
cpg.method.fullName(regexString).fullName.foreach { methodMatch => | ||
regexMatchedFullNames.put(methodMatch, regexString) | ||
} | ||
} | ||
} | ||
|
||
def elements: List[FlowSemantic] = methodToSemantic.values.toList | ||
|
||
private def forMethod(fullName: String): Option[FlowSemantic] = regexMatchedFullNames.get(fullName) match { | ||
case Some(matchedFullName) => methodToSemantic.get(matchedFullName) | ||
case None => methodToSemantic.get(fullName) | ||
} | ||
|
||
override def forMethod(method: Method): Option[FlowSemantic] = forMethod(method.fullName) | ||
|
||
def serialize: String = { | ||
elements | ||
.sortBy(_.methodFullName) | ||
.map { elem => | ||
s"\"${elem.methodFullName}\" " + elem.mappings | ||
.collect { case FlowMapping(x, y) => s"$x -> $y" } | ||
.mkString(" ") | ||
} | ||
.mkString("\n") | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
...s/src/main/scala/io/joern/dataflowengineoss/semanticsloader/FullNameSemanticsParser.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.joern.dataflowengineoss.semanticsloader | ||
|
||
import io.joern.dataflowengineoss.SemanticsParser.MappingContext | ||
import io.joern.dataflowengineoss.{SemanticsBaseListener, SemanticsLexer, SemanticsParser} | ||
import org.antlr.v4.runtime.tree.ParseTreeWalker | ||
import org.antlr.v4.runtime.{CharStream, CharStreams, CommonTokenStream} | ||
|
||
import scala.collection.mutable | ||
import scala.jdk.CollectionConverters.* | ||
|
||
class FullNameSemanticsParser { | ||
|
||
def parse(input: String): List[FlowSemantic] = { | ||
val charStream = CharStreams.fromString(input) | ||
parseCharStream(charStream) | ||
} | ||
|
||
def parseFile(fileName: String): List[FlowSemantic] = { | ||
val charStream = CharStreams.fromFileName(fileName) | ||
parseCharStream(charStream) | ||
} | ||
|
||
private def parseCharStream(charStream: CharStream): List[FlowSemantic] = { | ||
val lexer = new SemanticsLexer(charStream) | ||
val tokenStream = new CommonTokenStream(lexer) | ||
val parser = new SemanticsParser(tokenStream) | ||
val treeWalker = new ParseTreeWalker() | ||
|
||
val tree = parser.taintSemantics() | ||
val listener = new Listener() | ||
treeWalker.walk(listener, tree) | ||
listener.result.toList | ||
} | ||
|
||
implicit class AntlrFlowExtensions(val ctx: MappingContext) { | ||
|
||
def isPassThrough: Boolean = Option(ctx.PASSTHROUGH()).isDefined | ||
|
||
def srcIdx: Int = ctx.src().argIdx().NUMBER().getText.toInt | ||
|
||
def srcArgName: Option[String] = Option(ctx.src().argName()).map(_.name().getText) | ||
|
||
def dstIdx: Int = ctx.dst().argIdx().NUMBER().getText.toInt | ||
|
||
def dstArgName: Option[String] = Option(ctx.dst().argName()).map(_.name().getText) | ||
|
||
} | ||
|
||
private class Listener extends SemanticsBaseListener { | ||
|
||
val result: mutable.ListBuffer[FlowSemantic] = mutable.ListBuffer[FlowSemantic]() | ||
|
||
override def enterTaintSemantics(ctx: SemanticsParser.TaintSemanticsContext): Unit = { | ||
ctx.singleSemantic().asScala.foreach { semantic => | ||
val methodName = semantic.methodName().name().getText | ||
val mappings = semantic.mapping().asScala.toList.map(ctxToParamMapping) | ||
result.addOne(FlowSemantic(methodName, mappings)) | ||
} | ||
} | ||
|
||
private def ctxToParamMapping(ctx: MappingContext): FlowPath = | ||
if (ctx.isPassThrough) { | ||
PassThroughMapping | ||
} else { | ||
val src = ParameterNode(ctx.srcIdx, ctx.srcArgName) | ||
val dst = ParameterNode(ctx.dstIdx, ctx.dstArgName) | ||
|
||
FlowMapping(src, dst) | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.