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

add runtimeSDK #64

Merged
merged 3 commits into from
Dec 14, 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import kotlin.io.path.pathString
import kotlin.streams.toList

class AnalyzeStepByStep {
suspend fun loadRules(ruleList: String, targetSdk: Int): Rules {
suspend fun loadRules(ruleList: String, targetSdk: Int, minSdk: Int): Rules {
val rulePathList = if (ruleList.isNotEmpty())
ruleList.split(",").map { "${getConfig().rulePath}/${it.trim()}" }.toList()
else
Expand All @@ -45,7 +45,7 @@ class AnalyzeStepByStep {
}.filter { it.pathString.endsWith(".json") }.map { it.pathString }
.toList()
val rules = Rules(rulePathList, RuleFactory())
rules.loadRules(targetSdk)
rules.loadRules(targetSdk, minSdk)
return rules
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/net/bytedance/security/app/RuleData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ data class RuleData(
val ConstNumberMode: Boolean? = null,
val targetNumberArr: List<Int>? = null,

val targetSdk: String = "",
val targetSdk: String = "", // 规则适用的targetSdk版本
val runtimeSdk: String = "", // 规则适用的运行时系统版本
)

val defaultSourceReturn = SourceReturn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ object StaticAnalyzeMain {
profiler.parseApk.end()

profiler.preProcessor.start()
val rules = v3.loadRules(argumentConfig.rules, AndroidUtils.TargetSdk)
val rules = v3.loadRules(argumentConfig.rules, AndroidUtils.TargetSdk, AndroidUtils.MinSdk)
logInfo("rules loaded")
val ctx = v3.createContext(rules)
profiler.preProcessor.end()


if (getConfig().doWholeProcessMode) {
PLUtils.createWholeProgramAnalyze(ctx)
}
Expand Down
59 changes: 30 additions & 29 deletions src/main/kotlin/net/bytedance/security/app/rules/Rules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import java.nio.file.Paths

class Rules(val rulePaths: List<String>, val factory: IRuleFactory) : IRulesForContext {
val allRules: MutableList<IRule> = ArrayList()
val UNLIMITED = -1

suspend fun loadRules(targetSdk: Int? = null) {
suspend fun loadRules(targetSdk: Int = UNLIMITED, minSdk: Int = UNLIMITED) {
rulePaths.forEach {
val jsonStr = loadConfigOrQuit(it)
val rules = Json.parseToJsonElement(jsonStr)
Expand All @@ -40,7 +41,8 @@ class Rules(val rulePaths: List<String>, val factory: IRuleFactory) : IRulesForC
ruleData.sanitize = ruleData.sanitizer
ruleData.sanitizer = null
}
if (targetSdk == null || targetSdk in parseSdkVersion(ruleData.targetSdk)) {
if ((targetSdk == UNLIMITED || targetSdk in parseSdkVersion(ruleData.targetSdk)) &&
(minSdk == UNLIMITED || parseSdkVersion("$minSdk:").any { it in parseSdkVersion(ruleData.runtimeSdk) })) {
val rule = factory.create(ruleName, ruleData)
allRules.add(rule)
} else {
Expand Down Expand Up @@ -90,40 +92,39 @@ class Rules(val rulePaths: List<String>, val factory: IRuleFactory) : IRulesForC
throw Exception("read config file $path failed")
}
}

return jsonStr
}
}

fun parseSdkVersion(input: String): List<Int> {
val MIN_SDK_VERSION = 9 // Android 2.3
val MAX_SDK_VERSION = 50 // for future
fun parseSdkVersion(input: String): List<Int> {
val MIN_SDK_VERSION = 9 // Android 2.3
val MAX_SDK_VERSION = 50 // for future

if (input.isBlank() || input.trim() == ":") {
return (MIN_SDK_VERSION..MAX_SDK_VERSION).toList()
}
return input.split(Regex("[,\\s]+")).flatMap { part ->
when {
part.contains(":") -> {
val splitPart = part.split(":")
val hasStart = splitPart[0].isNotEmpty()
val hasEnd = splitPart[1].isNotEmpty()
when {
!hasStart && !hasEnd -> listOf()
!hasEnd -> {
(splitPart[0].toIntOrNull() ?: return@flatMap listOf())..MAX_SDK_VERSION
}
!hasStart -> {
(MIN_SDK_VERSION..(splitPart[1].toIntOrNull() ?: return@flatMap listOf())).toList()
}
else -> {
val start = splitPart[0].toIntOrNull() ?: return@flatMap listOf()
val end = splitPart[1].toIntOrNull() ?: return@flatMap listOf()
(start..end).toList()
if (input.isBlank() || input.trim() == ":") {
return (MIN_SDK_VERSION..MAX_SDK_VERSION).toList()
}
return input.split(Regex("[,\\s]+")).flatMap { part ->
when {
part.contains(":") -> {
val splitPart = part.split(":")
val hasStart = splitPart[0].isNotEmpty()
val hasEnd = splitPart[1].isNotEmpty()
when {
!hasStart && !hasEnd -> listOf()
!hasEnd -> {
(splitPart[0].toIntOrNull() ?: return@flatMap listOf())..MAX_SDK_VERSION
}
!hasStart -> {
(MIN_SDK_VERSION..(splitPart[1].toIntOrNull() ?: return@flatMap listOf())).toList()
}
else -> {
val start = splitPart[0].toIntOrNull() ?: return@flatMap listOf()
val end = splitPart[1].toIntOrNull() ?: return@flatMap listOf()
(start..end).toList()
}
}
}
else -> listOf(part.toIntOrNull() ?: return@flatMap listOf())
}
else -> listOf(part.toIntOrNull() ?: return@flatMap listOf())
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion src/test/kotlin/net/bytedance/security/app/rules/RulesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ package net.bytedance.security.app.rules
import kotlinx.coroutines.runBlocking
import net.bytedance.security.app.getConfig
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*
import java.io.File

internal class RulesTest {


fun createDefaultRules(): Rules {
val rules = Rules(
listOf(
Expand Down Expand Up @@ -69,7 +69,22 @@ internal class RulesTest {
println("const strings=${rules.constStringPatterns().toSortedSet().toList()}")
println("fields=${rules.fields().toSortedSet().toList()}")
println("new instances=${rules.newInstances().toSortedSet().toList()}")
}

@Test
fun testParseSdkVersion() {
assertEquals(
(9..50).toList(),
Rules.parseSdkVersion("")
)
assertEquals(
(9..50).toList(),
Rules.parseSdkVersion(":")
)
assertEquals(
(9..10).toList() + listOf(15) + (25..30).toList() + (45..50).toList(),
Rules.parseSdkVersion(":10, 15, 25:30, 45:")
)
}

companion object {
Expand Down
Loading