|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Fraunhofer AISEC. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * $$$$$$\ $$$$$$$\ $$$$$$\ |
| 17 | + * $$ __$$\ $$ __$$\ $$ __$$\ |
| 18 | + * $$ / \__|$$ | $$ |$$ / \__| |
| 19 | + * $$ | $$$$$$$ |$$ |$$$$\ |
| 20 | + * $$ | $$ ____/ $$ |\_$$ | |
| 21 | + * $$ | $$\ $$ | $$ | $$ | |
| 22 | + * \$$$$$ |$$ | \$$$$$ | |
| 23 | + * \______/ \__| \______/ |
| 24 | + * |
| 25 | + */ |
| 26 | +package de.fraunhofer.aisec.codyze.compliance |
| 27 | + |
| 28 | +import de.fraunhofer.aisec.codyze.* |
| 29 | +import de.fraunhofer.aisec.cpg.TranslationResult |
| 30 | +import io.github.detekt.sarif4k.MultiformatMessageString |
| 31 | +import io.github.detekt.sarif4k.ReportingDescriptor |
| 32 | +import io.github.detekt.sarif4k.Result |
| 33 | + |
| 34 | +/** Loads the security goals from the project directory. */ |
| 35 | +fun AnalysisProject.loadSecurityGoals(): List<SecurityGoal> { |
| 36 | + return securityGoalsFolder?.let { loadSecurityGoals(it) } ?: listOf() |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Extends the regular [AnalysisProject.analyze] method with the ability to load security goals and |
| 41 | + * execute queries based on them. |
| 42 | + */ |
| 43 | +fun AnalysisProject.analyzeWithGoals(): AnalysisResult { |
| 44 | + return this.analyze(postProcess = ::executeSecurityGoalsQueries) |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Executes the security goals queries and returns the security goals as SARIF rules and the query |
| 49 | + * results as SARIF results. |
| 50 | + */ |
| 51 | +fun AnalysisProject.executeSecurityGoalsQueries( |
| 52 | + tr: TranslationResult |
| 53 | +): Pair<List<ReportingDescriptor>, List<Result>> { |
| 54 | + val rules = mutableListOf<ReportingDescriptor>() |
| 55 | + val results = mutableListOf<Result>() |
| 56 | + val goals = loadSecurityGoals() |
| 57 | + |
| 58 | + // Connect the security goals to the translation result for now. Later we will add them |
| 59 | + // to individual concepts |
| 60 | + for (goal in goals) { |
| 61 | + goal.underlyingNode = tr |
| 62 | + |
| 63 | + // Load and execute queries associated to the goals |
| 64 | + for (objective in goal.objectives) { |
| 65 | + val objectiveID = objective.name.localName.lowercase().replace(" ", "-") |
| 66 | + objective.underlyingNode = tr |
| 67 | + |
| 68 | + projectDir?.let { |
| 69 | + val scriptFile = it.resolve("queries").resolve("${objectiveID}.query.kts") |
| 70 | + for (stmt in objective.statements.withIndex()) { |
| 71 | + val idx1 = stmt.index + 1 |
| 72 | + val statementID = "statement${idx1}" |
| 73 | + val rule = |
| 74 | + ReportingDescriptor( |
| 75 | + id = "${objectiveID}-${statementID}", |
| 76 | + name = "${objective.name.localName}: Statement $idx1", |
| 77 | + shortDescription = MultiformatMessageString(text = stmt.value), |
| 78 | + ) |
| 79 | + val queryResult = tr.evalQuery(scriptFile.toFile(), statementID, rule.id) |
| 80 | + results += queryResult.sarif |
| 81 | + |
| 82 | + rules += rule |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return Pair(rules, results) |
| 89 | +} |
0 commit comments