From 1a7fe2512fb283e292b30362ea40a73dd6e0228c Mon Sep 17 00:00:00 2001 From: Selina Lin Date: Fri, 8 Sep 2023 18:46:34 +0200 Subject: [PATCH 1/7] remove deprecated configuration and add its replacement --- detekt.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/detekt.yml b/detekt.yml index 35a709206..280642861 100644 --- a/detekt.yml +++ b/detekt.yml @@ -500,6 +500,8 @@ style: active: false singleLine: 'never' multiLine: 'always' + BracesOnWhenStatements: + active: false CanBeNonNullable: active: false CascadingCallWrapping: @@ -618,8 +620,6 @@ style: active: true OptionalUnit: active: false - OptionalWhenBraces: - active: false PreferToOverPairSyntax: active: false ProtectedMemberInFinalClass: From 350b2b283af236f9baaa62276d4af62773de9289 Mon Sep 17 00:00:00 2001 From: Selina Lin Date: Fri, 8 Sep 2023 18:48:07 +0200 Subject: [PATCH 2/7] rename to CokoCpgBackendCommand to prevent confusion with other CokoCpgBackend class and rename to BaseCpgBackenCommand for consistency --- .../aisec/codyze/backends/cpg/CPGOptionGroup.kt | 6 +++--- .../cli/{BaseCpgBackend.kt => BaseCpgBackendCommand.kt} | 2 +- .../cli/{CokoCpgBackend.kt => CokoCpgBackendCommand.kt} | 2 +- .../kotlin/de/fraunhofer/aisec/codyze/cli/KoinModules.kt | 8 ++++---- 4 files changed, 9 insertions(+), 9 deletions(-) rename codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/{BaseCpgBackend.kt => BaseCpgBackendCommand.kt} (97%) rename codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/{CokoCpgBackend.kt => CokoCpgBackendCommand.kt} (96%) diff --git a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGOptionGroup.kt b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGOptionGroup.kt index 3a8c9552c..4405bf564 100644 --- a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGOptionGroup.kt +++ b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGOptionGroup.kt @@ -17,8 +17,8 @@ package de.fraunhofer.aisec.codyze.backends.cpg import com.github.ajalt.clikt.parameters.options.* import com.github.ajalt.clikt.parameters.types.* -import de.fraunhofer.aisec.codyze.backends.cpg.cli.BaseCpgBackend -import de.fraunhofer.aisec.codyze.backends.cpg.cli.CokoCpgBackend +import de.fraunhofer.aisec.codyze.backends.cpg.cli.BaseCpgBackendCommand +import de.fraunhofer.aisec.codyze.backends.cpg.cli.CokoCpgBackendCommand import de.fraunhofer.aisec.codyze.core.backend.BackendOptions import de.fraunhofer.aisec.codyze.core.config.combineSources import de.fraunhofer.aisec.codyze.core.config.resolvePaths @@ -30,7 +30,7 @@ import kotlin.reflect.full.isSuperclassOf /** * Holds the common CLI options for all CPG based Codyze backends. - * Used in e.g., [BaseCpgBackend] and [CokoCpgBackend]. + * Used in e.g., [BaseCpgBackendCommand] and [CokoCpgBackendCommand]. */ @Suppress("UNUSED") class CPGOptionGroup : BackendOptions(helpName = "CPG Backend Options") { diff --git a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackend.kt b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackendCommand.kt similarity index 97% rename from codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackend.kt rename to codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackendCommand.kt index f9bfc5739..1a6a630dd 100644 --- a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackend.kt +++ b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackendCommand.kt @@ -24,7 +24,7 @@ import de.fraunhofer.aisec.codyze.core.backend.BackendCommand /** * The [CliktCommand] to add the plain cpg backend to the codyze-cli. */ -class BaseCpgBackend : BackendCommand("cpg") { +class BaseCpgBackendCommand : BackendCommand("cpg") { val backendOptions by CPGOptionGroup() override val backend = CPGBackend::class diff --git a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackend.kt b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackendCommand.kt similarity index 96% rename from codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackend.kt rename to codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackendCommand.kt index f755db75c..25fb12aa7 100644 --- a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackend.kt +++ b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackendCommand.kt @@ -25,7 +25,7 @@ import de.fraunhofer.aisec.codyze.specificationLanguages.coko.core.CokoBackend /** * The [CliktCommand] to add the cokoCpg backend to the codyze-cli. */ -class CokoCpgBackend : BackendCommand("cokoCpg") { +class CokoCpgBackendCommand : BackendCommand("cokoCpg") { val backendOptions by CPGOptionGroup() override val backend = CokoBackend::class diff --git a/codyze-cli/src/main/kotlin/de/fraunhofer/aisec/codyze/cli/KoinModules.kt b/codyze-cli/src/main/kotlin/de/fraunhofer/aisec/codyze/cli/KoinModules.kt index c455e943e..dd72e0152 100644 --- a/codyze-cli/src/main/kotlin/de/fraunhofer/aisec/codyze/cli/KoinModules.kt +++ b/codyze-cli/src/main/kotlin/de/fraunhofer/aisec/codyze/cli/KoinModules.kt @@ -15,8 +15,8 @@ */ package de.fraunhofer.aisec.codyze.cli -import de.fraunhofer.aisec.codyze.backends.cpg.cli.BaseCpgBackend -import de.fraunhofer.aisec.codyze.backends.cpg.cli.CokoCpgBackend +import de.fraunhofer.aisec.codyze.backends.cpg.cli.BaseCpgBackendCommand +import de.fraunhofer.aisec.codyze.backends.cpg.cli.CokoCpgBackendCommand import de.fraunhofer.aisec.codyze.core.backend.Backend import de.fraunhofer.aisec.codyze.core.backend.BackendCommand import de.fraunhofer.aisec.codyze.core.executor.Executor @@ -32,8 +32,8 @@ import org.koin.dsl.module * Every [Backend] must provide [BackendCommand] to be selectable in the CLI. */ val backendCommands = module { - factoryOf(::BaseCpgBackend) bind(BackendCommand::class) - factoryOf(::CokoCpgBackend) bind(BackendCommand::class) + factoryOf(::BaseCpgBackendCommand) bind(BackendCommand::class) + factoryOf(::CokoCpgBackendCommand) bind(BackendCommand::class) } /** From 776b1d5e33538500a1f1d87ea0f2bd7d7424c318 Mon Sep 17 00:00:00 2001 From: Selina Lin Date: Fri, 8 Sep 2023 19:16:50 +0200 Subject: [PATCH 3/7] change all map().flatten() to flatMap() --- .../codyze/backends/cpg/coko/dsl/ImplementationDsl.kt | 8 +++----- .../codyze/backends/cpg/coko/evaluators/OnlyEvaluator.kt | 6 ++---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/dsl/ImplementationDsl.kt b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/dsl/ImplementationDsl.kt index e67d37584..8423a9758 100644 --- a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/dsl/ImplementationDsl.kt +++ b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/dsl/ImplementationDsl.kt @@ -45,7 +45,7 @@ context(CokoBackend) fun Op.cpgGetAllNodes(): Nodes = when (this@Op) { is FunctionOp -> - this@Op.definitions.map { def -> this@CokoBackend.cpgCallFqn(def.fqn) }.flatten() + this@Op.definitions.flatMap { def -> this@CokoBackend.cpgCallFqn(def.fqn) } is ConstructorOp -> this@CokoBackend.cpgConstructor(this.classFqn) } @@ -58,7 +58,7 @@ fun Op.cpgGetNodes(): Nodes = when (this@Op) { is FunctionOp -> this@Op.definitions - .map { def -> + .flatMap { def -> this@CokoBackend.cpgCallFqn(def.fqn) { def.signatures.any { sig -> cpgSignature(*sig.parameters.toTypedArray()) && @@ -66,16 +66,14 @@ fun Op.cpgGetNodes(): Nodes = } } } - .flatten() is ConstructorOp -> this@Op.signatures - .map { sig -> + .flatMap { sig -> this@CokoBackend.cpgConstructor(this@Op.classFqn) { cpgSignature(*sig.parameters.toTypedArray()) && sig.unorderedParameters.all { it?.cpgFlowsTo(arguments) ?: false } } } - .flatten() } /** Returns a list of [ValueDeclaration]s with the matching name. */ diff --git a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/evaluators/OnlyEvaluator.kt b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/evaluators/OnlyEvaluator.kt index 6d6f33c95..bec29b8ec 100644 --- a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/evaluators/OnlyEvaluator.kt +++ b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/evaluators/OnlyEvaluator.kt @@ -40,14 +40,12 @@ class OnlyEvaluator(val ops: List) : Evaluator { override fun evaluate(context: EvaluationContext): List { val correctNodes = - with(this@CokoCpgBackend) { ops.map { it.cpgGetNodes() } } - .flatten() + with(this@CokoCpgBackend) { ops.flatMap { it.cpgGetNodes() } } .toSet() val distinctOps = ops.toSet() val allNodes = - with(this@CokoCpgBackend) { distinctOps.map { it.cpgGetAllNodes() } } - .flatten() + with(this@CokoCpgBackend) { distinctOps.flatMap { it.cpgGetAllNodes() } } .toSet() // `correctNodes` is a subset of `allNodes` From 13219d244f26e74c31d62a5297bfefb6383232de Mon Sep 17 00:00:00 2001 From: Selina Lin Date: Fri, 8 Sep 2023 19:18:12 +0200 Subject: [PATCH 4/7] remove redundant if statements --- .../specificationLanguages/coko/core/dsl/Op.kt | 8 ++------ .../coko/core/modelling/OpComponents.kt | 12 +++--------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/dsl/Op.kt b/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/dsl/Op.kt index bef09a401..8be75c30f 100644 --- a/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/dsl/Op.kt +++ b/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/dsl/Op.kt @@ -66,9 +66,7 @@ class FunctionOp internal constructor( other as FunctionOp - if (definitions != other.definitions) return false - - return true + return definitions == other.definitions } override fun hashCode(): Int { @@ -100,9 +98,7 @@ class ConstructorOp internal constructor( if (this === other) return true if (other !is ConstructorOp) return false - if (signatures != other.signatures) return false - - return true + return signatures == other.signatures } override fun hashCode(): Int { diff --git a/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/modelling/OpComponents.kt b/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/modelling/OpComponents.kt index 38a8f41b9..8e91964a7 100644 --- a/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/modelling/OpComponents.kt +++ b/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/modelling/OpComponents.kt @@ -39,9 +39,7 @@ class ParameterGroup { other as ParameterGroup - if (parameters != other.parameters) return false - - return true + return parameters == other.parameters } override fun hashCode(): Int { @@ -74,9 +72,7 @@ class Definition(val fqn: String) { other as Definition if (fqn != other.fqn) return false - if (signatures != other.signatures) return false - - return true + return signatures == other.signatures } override fun hashCode(): Int { @@ -147,9 +143,7 @@ class Signature { other as Signature if (parameters != other.parameters) return false - if (unorderedParameters != other.unorderedParameters) return false - - return true + return unorderedParameters == other.unorderedParameters } override fun hashCode(): Int { From f2ad444efc15d7ea7250bfb2fb07cddb30158a6e Mon Sep 17 00:00:00 2001 From: Selina Lin Date: Fri, 8 Sep 2023 19:20:56 +0200 Subject: [PATCH 5/7] call the primaryConstructor without arguments paramInstance is the script instance so it should probably never be an argument for the primaryConstructor. Furthermore, we check above that the primaryConstructor does not have any parameters, so it should not be necessary to use the script instance as a argument. --- .../coko/dsl/host/SpecEvaluator.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/codyze-specification-languages/coko/coko-dsl/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/host/SpecEvaluator.kt b/codyze-specification-languages/coko/coko-dsl/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/host/SpecEvaluator.kt index 48553d8b5..5b2be8523 100644 --- a/codyze-specification-languages/coko/coko-dsl/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/host/SpecEvaluator.kt +++ b/codyze-specification-languages/coko/coko-dsl/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/host/SpecEvaluator.kt @@ -73,7 +73,7 @@ class SpecEvaluator { // TODO: check for all implementations! implementationsAndInstances .filter { (it, _) -> it.createType().isSubtypeOf(param.type) } - .map { (it, paramInstance) -> + .mapNotNull { (it, _) -> val primaryConstructor = checkNotNull(it.primaryConstructor) { "Could not create an instance of ${it.qualifiedName} to pass to rule " + @@ -88,13 +88,13 @@ class SpecEvaluator { "Could not create an instance of ${it.qualifiedName} to pass to rule " + "\"${rule.name}\" because it's primary constructor expects arguments. Aborting." } - // TODO: how do we access primaryConstructor.arity ? -> then we would - // not need the try..catch try { - primaryConstructor.call(paramInstance) - } catch (e: IllegalArgumentException) { - logger.debug { "Called constructor '$primaryConstructor' without paramInstance ($e)" } primaryConstructor.call() + } catch (e: IllegalArgumentException) { + logger.debug { + "Calling constructor '$primaryConstructor' without arguments failed ($e)" + } + null } }[0] } From fd5b91a7609b1d0dbab083b5bc52ef7d03d37264 Mon Sep 17 00:00:00 2001 From: Selina Lin Date: Fri, 8 Sep 2023 19:24:42 +0200 Subject: [PATCH 6/7] remove TODO since it should be done --- .../specificationLanguages/coko/dsl/CokoSarifBuilder.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codyze-specification-languages/coko/coko-dsl/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/CokoSarifBuilder.kt b/codyze-specification-languages/coko/coko-dsl/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/CokoSarifBuilder.kt index d4f024a7a..4ae5a4805 100644 --- a/codyze-specification-languages/coko/coko-dsl/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/CokoSarifBuilder.kt +++ b/codyze-specification-languages/coko/coko-dsl/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/CokoSarifBuilder.kt @@ -40,9 +40,9 @@ private fun CokoRule.toReportingDescriptor() = ReportingDescriptor( defaultConfiguration = ReportingConfiguration(level = findAnnotation()?.severity?.toResultLevel()), help = findAnnotation()?.help?.let { desc -> MultiformatMessageString(text = desc) }, properties = PropertyBag( - tags = findAnnotation()?.tags?.toList() + tags = findAnnotation()?.tags?.toList(), + ) - // TODO: add precision, severity ) class CokoSarifBuilder(val rules: List, val backend: Backend) { From d396e790de8bbf3a024925a19a867ecea99d5ab0 Mon Sep 17 00:00:00 2001 From: Selina Lin Date: Mon, 11 Sep 2023 10:37:06 +0200 Subject: [PATCH 7/7] Revert "call the primaryConstructor without arguments" This reverts commit f2ad444efc15d7ea7250bfb2fb07cddb30158a6e. In some cases the script instance is needed as an argument for some constructors --- .../coko/dsl/host/SpecEvaluator.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/codyze-specification-languages/coko/coko-dsl/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/host/SpecEvaluator.kt b/codyze-specification-languages/coko/coko-dsl/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/host/SpecEvaluator.kt index 5b2be8523..48553d8b5 100644 --- a/codyze-specification-languages/coko/coko-dsl/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/host/SpecEvaluator.kt +++ b/codyze-specification-languages/coko/coko-dsl/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/host/SpecEvaluator.kt @@ -73,7 +73,7 @@ class SpecEvaluator { // TODO: check for all implementations! implementationsAndInstances .filter { (it, _) -> it.createType().isSubtypeOf(param.type) } - .mapNotNull { (it, _) -> + .map { (it, paramInstance) -> val primaryConstructor = checkNotNull(it.primaryConstructor) { "Could not create an instance of ${it.qualifiedName} to pass to rule " + @@ -88,13 +88,13 @@ class SpecEvaluator { "Could not create an instance of ${it.qualifiedName} to pass to rule " + "\"${rule.name}\" because it's primary constructor expects arguments. Aborting." } + // TODO: how do we access primaryConstructor.arity ? -> then we would + // not need the try..catch try { - primaryConstructor.call() + primaryConstructor.call(paramInstance) } catch (e: IllegalArgumentException) { - logger.debug { - "Calling constructor '$primaryConstructor' without arguments failed ($e)" - } - null + logger.debug { "Called constructor '$primaryConstructor' without paramInstance ($e)" } + primaryConstructor.call() } }[0] }