Kotlin: Support apply

This commit is contained in:
Tony Torralba
2023-07-10 16:14:37 +02:00
parent 3c3b53001f
commit 0f18c0227b
6 changed files with 43 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* Added support for the Kotlin method `apply`.

View File

@@ -3,5 +3,7 @@ extensions:
pack: codeql/java-all
extensible: summaryModel
data:
- ["kotlin", "StandardKt", False, "apply", "", "", "Argument[0]", "Argument[1].Parameter[0]", "value", "manual"]
- ["kotlin", "StandardKt", False, "apply", "", "", "Argument[0]", "ReturnValue", "value", "manual"]
- ["kotlin", "StandardKt", False, "with", "", "", "Argument[0]", "Argument[1].Parameter[0]", "value", "manual"]
- ["kotlin", "StandardKt", False, "with", "", "", "Argument[1].ReturnValue", "ReturnValue", "value", "manual"]

View File

@@ -0,0 +1,21 @@
/** Provides classes and predicates related to `kotlin`. */
import java
/** A call to Kotlin's `apply` method. */
class KotlinApply extends MethodAccess {
ExtensionMethod m;
KotlinApply() {
this.getMethod() = m and
m.hasQualifiedName("kotlin", "StandardKt", "apply")
}
/** Gets the function block argument of this call. */
LambdaExpr getLambdaArg() {
result = this.getArgument(m.getExtensionReceiverParameterIndex() + 1)
}
/** Gets the receiver argument of this call. */
Argument getReceiver() { result = this.getArgument(m.getExtensionReceiverParameterIndex()) }
}

View File

@@ -0,0 +1,2 @@
| apply.kt:6:9:6:41 | apply(...) |
| apply.kt:7:14:7:40 | apply(...) |

View File

@@ -0,0 +1,9 @@
class ApplyFlowTest {
fun <T> taint(t: T) = t
fun sink(s: String) { }
fun test(input: String) {
taint(input).apply { sink(this) } // $ hasValueFlow
sink(taint(input).apply { this }) // $ hasValueFlow
}
}

View File

@@ -0,0 +1,5 @@
import java
import semmle.code.java.frameworks.kotlin.Kotlin
from KotlinApply a
select a