Skip to content

Commit

Permalink
Extract interface EtsApplicationGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
Lipen committed Jul 26, 2024
1 parent 4b15fc6 commit 0109f77
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@
package org.jacodb.ets.graph

import org.jacodb.api.common.analysis.ApplicationGraph
import org.jacodb.ets.base.EtsCallStmt
import org.jacodb.ets.base.EtsStmt
import org.jacodb.ets.model.EtsFile
import org.jacodb.ets.model.EtsMethod
import org.jacodb.ets.utils.callExpr

class EtsApplicationGraph(
val cp: EtsFile,
) : ApplicationGraph<EtsMethod, EtsStmt> {
interface EtsApplicationGraph : ApplicationGraph<EtsMethod, EtsStmt> {
val cp: EtsFile
}

class EtsApplicationGraphImpl(
override val cp: EtsFile,
) : EtsApplicationGraph {

override fun predecessors(node: EtsStmt): Sequence<EtsStmt> {
val graph = node.method.flowGraph()
Expand All @@ -48,12 +51,14 @@ class EtsApplicationGraph(
}

override fun callers(method: EtsMethod): Sequence<EtsStmt> {
return cp.classes.asSequence()
.flatMap { it.methods }
.flatMap { it.cfg.instructions }
.filterIsInstance<EtsCallStmt>()
// TODO: consider comparing only by name
.filter { it.expr.method == method.signature }
// Note: currently, nobody uses `callers`, so if is safe to disable it for now.
// Note: comparing methods by signature may be incorrect, and comparing only by name fails for constructors.
TODO("disabled for now, need re-design")

Check warning on line 56 in jacodb-ets/src/main/kotlin/org/jacodb/ets/graph/EtsApplicationGraph.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/graph/EtsApplicationGraph.kt#L56

Added line #L56 was not covered by tests
// return cp.classes.asSequence()
// .flatMap { it.methods }
// .flatMap { it.cfg.instructions }
// .filterIsInstance<EtsCallStmt>()
// .filter { it.expr.method == method.signature }
}

override fun entryPoints(method: EtsMethod): Sequence<EtsStmt> {
Expand Down
19 changes: 8 additions & 11 deletions jacodb-ets/src/test/kotlin/org/jacodb/ets/test/EtsIfds.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.jacodb.analysis.taint.TaintAnalysisOptions
import org.jacodb.analysis.taint.TaintManager
import org.jacodb.analysis.util.EtsTraits
import org.jacodb.ets.base.EtsStmt
import org.jacodb.ets.graph.EtsApplicationGraph
import org.jacodb.ets.graph.EtsApplicationGraphImpl
import org.jacodb.ets.model.EtsFile
import org.jacodb.ets.model.EtsMethod
import org.jacodb.taint.configuration.Argument
Expand All @@ -43,9 +43,6 @@ import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.EnabledIf
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.io.path.exists
import kotlin.io.path.toPath
import kotlin.time.Duration.Companion.seconds
Expand All @@ -71,7 +68,7 @@ class EtsIfds {
@Test
fun `test taint analysis on MethodCollision`() {
val project = loadSample("MethodCollision")
val graph = EtsApplicationGraph(project)
val graph = EtsApplicationGraphImpl(project)
val unitResolver = UnitResolver<EtsMethod> { SingletonUnit }
val getConfigForMethod: ForwardTaintFlowFunctions<EtsMethod, EtsStmt>.(EtsMethod) -> List<TaintConfigurationItem>? =
{ method ->
Expand Down Expand Up @@ -115,7 +112,7 @@ class EtsIfds {
@Test
fun `test taint analysis on TypeMismatch`() {
val project = loadSample("TypeMismatch")
val graph = EtsApplicationGraph(project)
val graph = EtsApplicationGraphImpl(project)
val unitResolver = UnitResolver<EtsMethod> { SingletonUnit }
val getConfigForMethod: ForwardTaintFlowFunctions<EtsMethod, EtsStmt>.(EtsMethod) -> List<TaintConfigurationItem>? =
{ method ->
Expand Down Expand Up @@ -160,7 +157,7 @@ class EtsIfds {
@Test
fun `test taint analysis on DataFlowSecurity`() {
val project = loadSample("DataFlowSecurity")
val graph = EtsApplicationGraph(project)
val graph = EtsApplicationGraphImpl(project)
val unitResolver = UnitResolver<EtsMethod> { SingletonUnit }
val getConfigForMethod: ForwardTaintFlowFunctions<EtsMethod, EtsStmt>.(EtsMethod) -> List<TaintConfigurationItem>? =
{ method ->
Expand Down Expand Up @@ -225,7 +222,7 @@ class EtsIfds {
@Test
fun `test taint analysis on case1 - untrusted loop bound scenario`() {
val project = loadSample("cases/case1")
val graph = EtsApplicationGraph(project)
val graph = EtsApplicationGraphImpl(project)
val unitResolver = UnitResolver<EtsMethod> { SingletonUnit }
val getConfigForMethod: ForwardTaintFlowFunctions<EtsMethod, EtsStmt>.(EtsMethod) -> List<TaintConfigurationItem>? =
{ method ->
Expand Down Expand Up @@ -262,7 +259,7 @@ class EtsIfds {
@Test
fun `test taint analysis on case2 - untrusted array buffer size scenario`() {
val project = loadSample("cases/case2")
val graph = EtsApplicationGraph(project)
val graph = EtsApplicationGraphImpl(project)
val unitResolver = UnitResolver<EtsMethod> { SingletonUnit }
val getConfigForMethod: ForwardTaintFlowFunctions<EtsMethod, EtsStmt>.(EtsMethod) -> List<TaintConfigurationItem>? =
{ method ->
Expand Down Expand Up @@ -300,7 +297,7 @@ class EtsIfds {
@Test
fun `test taint analysis on case3 - send plain information with sensitive data`() {
val project = loadSample("cases/case3")
val graph = EtsApplicationGraph(project)
val graph = EtsApplicationGraphImpl(project)
val unitResolver = UnitResolver<EtsMethod> { SingletonUnit }
val getConfigForMethod: ForwardTaintFlowFunctions<EtsMethod, EtsStmt>.(EtsMethod) -> List<TaintConfigurationItem>? =
{ method ->
Expand Down Expand Up @@ -344,7 +341,7 @@ class EtsIfds {
@Test
fun `test taint analysis on AccountManager`() {
val project = loadEtsFileFromResource("/etsir/project1/entry/src/main/ets/base/account/AccountManager.ts.json")
val graph = EtsApplicationGraph(project)
val graph = EtsApplicationGraphImpl(project)
val unitResolver = UnitResolver<EtsMethod> { SingletonUnit }
val getConfigForMethod: ForwardTaintFlowFunctions<EtsMethod, EtsStmt>.(EtsMethod) -> List<TaintConfigurationItem>? =
{ method ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import org.jacodb.analysis.taint.TaintVulnerability
import org.jacodb.analysis.util.EtsTraits
import org.jacodb.analysis.util.getPathEdges
import org.jacodb.ets.base.EtsStmt
import org.jacodb.ets.graph.EtsApplicationGraph
import org.jacodb.ets.graph.EtsApplicationGraphImpl
import org.jacodb.ets.model.EtsFile
import org.jacodb.ets.model.EtsMethod
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -136,7 +136,7 @@ class EtsProjectAnalysis {
}

private fun runAnalysis(project: EtsFile) {
val graph = EtsApplicationGraph(project)
val graph = EtsApplicationGraphImpl(project)
val unitResolver = UnitResolver<EtsMethod> { SingletonUnit }
val manager = TaintManager(
graph = graph,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.jacodb.analysis.taint.ForwardTaintFlowFunctions
import org.jacodb.analysis.taint.TaintManager
import org.jacodb.analysis.util.EtsTraits
import org.jacodb.ets.base.EtsStmt
import org.jacodb.ets.graph.EtsApplicationGraph
import org.jacodb.ets.graph.EtsApplicationGraphImpl
import org.jacodb.ets.model.EtsFile
import org.jacodb.ets.model.EtsMethod
import org.jacodb.taint.configuration.Argument
Expand Down Expand Up @@ -103,7 +103,7 @@ class EtsTaintAnalysisTest {
}

fun runTaintAnalysis(etsFile: EtsFile) {
val graph = EtsApplicationGraph(etsFile)
val graph = EtsApplicationGraphImpl(etsFile)
val unitResolver = UnitResolver<EtsMethod> { SingletonUnit }

val manager = TaintManager(
Expand Down

0 comments on commit 0109f77

Please sign in to comment.