-
Notifications
You must be signed in to change notification settings - Fork 12
/
DataClasses.kt
189 lines (160 loc) · 5.73 KB
/
DataClasses.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright 2022 UnitTestBot contributors (utbot.org)
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jacodb.analysis.sarif
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToStream
import org.jacodb.analysis.engine.IfdsVertex
import org.jacodb.analysis.engine.VulnerabilityInstance
import org.jacodb.api.JcMethod
import org.jacodb.api.cfg.JcInst
import java.io.OutputStream
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.io.path.Path
@Serializable
data class SarifMessage(val text: String)
@Serializable
data class SarifArtifactLocation(val uri: String)
@Serializable
data class SarifRegion(val startLine: Int)
@Serializable
data class SarifPhysicalLocation(val artifactLocation: SarifArtifactLocation, val region: SarifRegion)
@Serializable
data class SarifLogicalLocation(val fullyQualifiedName: String)
@Serializable
data class SarifLocation(val physicalLocation: SarifPhysicalLocation, val logicalLocations: List<SarifLogicalLocation>) {
companion object {
private val JcMethod.fullyQualifiedName: String
get() = "${enclosingClass.name}#${name}"
private val currentPath: Path = Paths.get("").toAbsolutePath()
fun fromInst(inst: JcInst): SarifLocation = SarifLocation(
physicalLocation = SarifPhysicalLocation(
SarifArtifactLocation("${currentPath.relativize(Path(inst.location.method.declaration.location.path))}"),
SarifRegion(inst.location.lineNumber)
),
logicalLocations = listOf(
SarifLogicalLocation(inst.location.method.fullyQualifiedName)
)
)
}
}
@Serializable
data class SarifDomainFact(val text: String)
@Serializable
data class SarifState(val domainFact: SarifDomainFact)
@Serializable
data class SarifThreadFlowLocation(val location: SarifLocation, val state: SarifState)
@Serializable
data class SarifThreadFlow(val locations: List<SarifThreadFlowLocation>)
@Serializable
data class SarifCodeFlow(val threadFlows: List<SarifThreadFlow>) {
companion object {
fun fromTrace(trace: List<IfdsVertex>): SarifCodeFlow {
val threadFlow = trace.map {
SarifThreadFlowLocation(
SarifLocation.fromInst(it.statement),
SarifState(SarifDomainFact(it.domainFact.toString()))
)
}
return SarifCodeFlow(listOf(SarifThreadFlow(threadFlow)))
}
}
}
@Serializable
data class SarifResult(
val ruleId: String,
val message: SarifMessage,
val level: SarifSeverityLevel,
val locations: List<SarifLocation>,
val codeFlows: List<SarifCodeFlow>
) {
companion object {
fun fromVulnerabilityInstance(instance: VulnerabilityInstance, maxPathsCount: Int): SarifResult = SarifResult(
instance.vulnerabilityDescription.ruleId,
instance.vulnerabilityDescription.message,
instance.vulnerabilityDescription.level,
listOf(SarifLocation.fromInst(instance.traceGraph.sink.statement)),
instance.traceGraph.getAllTraces().take(maxPathsCount).map { SarifCodeFlow.fromTrace(it) }.toList()
)
}
}
@Serializable
data class SarifDriver(
val name: String,
val version: String,
val informationUri: String,
)
@Serializable
data class SarifTool(val driver: SarifDriver)
val IfdsTool = SarifTool(
SarifDriver(
name = "JaCo-IFDS",
version = "1.2.0",
informationUri = "https://github.com/UnitTestBot/jacodb/blob/develop/jacodb-analysis/README.md"
)
)
@Serializable
data class SarifRun(val tool: SarifTool, val results: List<SarifResult>)
@OptIn(ExperimentalSerializationApi::class)
@Serializable
data class SarifReport(
val version: String,
@SerialName("\$schema")
val schema: String,
val runs: List<SarifRun>
) {
fun encodeToStream(stream: OutputStream) {
json.encodeToStream(this, stream)
}
companion object {
private const val defaultVersion =
"2.1.0"
private const val defaultSchema =
"https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json"
private const val defaultPathsCount: Int = 3
private val json = Json {
prettyPrint = true
encodeDefaults = false
}
fun fromVulnerabilities(
vulnerabilities: List<VulnerabilityInstance>,
pathsCount: Int = defaultPathsCount
): SarifReport = SarifReport(
version = defaultVersion,
schema = defaultSchema,
runs = listOf(
SarifRun(
IfdsTool,
vulnerabilities.map { SarifResult.fromVulnerabilityInstance(it, pathsCount) }
)
)
)
}
}
@Serializable
enum class SarifSeverityLevel {
@SerialName("error") ERROR,
@SerialName("warning") WARNING,
@SerialName("note") NOTE
}
data class VulnerabilityDescription(
val message: SarifMessage,
val ruleId: String,
val level: SarifSeverityLevel = SarifSeverityLevel.WARNING
)