-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sarif] Sarif Conversion from Finding Node (#5256)
- Loading branch information
1 parent
5ef163c
commit e2d410d
Showing
13 changed files
with
1,028 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
semanticcpg/src/main/scala/io/shiftleft/semanticcpg/language/SarifExtension.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.shiftleft.semanticcpg.language | ||
|
||
import io.shiftleft.codepropertygraph.generated.Cpg | ||
import io.shiftleft.codepropertygraph.generated.help.Doc | ||
import io.shiftleft.codepropertygraph.generated.nodes.Finding | ||
import io.shiftleft.semanticcpg.sarif.SarifConfig.SarifVersion | ||
import io.shiftleft.semanticcpg.sarif.SarifSchema.{Sarif, Sarif2_1_0} | ||
import io.shiftleft.semanticcpg.sarif.{SarifConfig, SarifSchema, v2_1_0} | ||
import org.json4s.Formats | ||
import org.json4s.native.Serialization.{write, writePretty} | ||
|
||
import java.net.URI | ||
|
||
/** Converts findings written to the CPG to the SARIF format. | ||
* | ||
* @param traversal | ||
* the findings | ||
* @see | ||
* https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html | ||
*/ | ||
class SarifExtension(val traversal: Iterator[Finding]) extends AnyVal { | ||
|
||
@Doc(info = "execute this traversal and convert findings to SARIF format") | ||
def toSarif(implicit config: SarifConfig = SarifConfig()): Sarif = { | ||
|
||
def generateSarif(results: List[SarifSchema.Result], baseUri: Option[URI]): Sarif = { | ||
config.sarifVersion match { | ||
case SarifVersion.V2_1_0 => | ||
val tool = v2_1_0.Schema.ToolComponent( | ||
name = config.toolName, | ||
fullName = config.toolFullName, | ||
organization = config.organization, | ||
semanticVersion = config.semanticVersion, | ||
informationUri = config.toolInformationUri | ||
) | ||
val projectBaseUri = Map( | ||
"PROJECT_ROOT" -> v2_1_0.Schema | ||
.ArtifactLocation(uriBaseId = baseUri.map(_.toString).orElse(Option("<empty>"))) | ||
) | ||
val runs = v2_1_0.Schema.Run( | ||
tool = v2_1_0.Schema.Tool(driver = tool), | ||
originalUriBaseIds = projectBaseUri, | ||
results = results | ||
) :: Nil | ||
Sarif2_1_0(runs = runs) | ||
} | ||
} | ||
|
||
traversal.l match { | ||
case Nil => generateSarif(results = Nil, baseUri = None) | ||
case findings @ head :: _ => | ||
val baseUri = Cpg(head.graph).metaData.root.headOption.map(java.io.File(_).toURI) | ||
val results = findings.map(config.resultConverter.convertFindingToResult) | ||
generateSarif(results, baseUri) | ||
} | ||
|
||
} | ||
|
||
@Doc(info = "execute this traversal and convert findings to SARIF format as JSON") | ||
def toSarifJson(pretty: Boolean = false)(implicit config: SarifConfig = SarifConfig()): String = { | ||
implicit val formats: Formats = org.json4s.DefaultFormats ++ config.customSerializers | ||
|
||
val results = toSarif | ||
if (pretty) writePretty(results) | ||
else write(results) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
semanticcpg/src/main/scala/io/shiftleft/semanticcpg/sarif/SarifConfig.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.shiftleft.semanticcpg.sarif | ||
|
||
import io.shiftleft.semanticcpg.sarif.SarifConfig.SarifVersion | ||
import io.shiftleft.semanticcpg.sarif.v2_1_0.JoernScanResultToSarifConverter | ||
import org.json4s.Serializer | ||
|
||
import java.net.URI | ||
|
||
/** A configuration for tool-specific information and arguments on transforming how findings are to be converted to | ||
* SARIF. | ||
* | ||
* @param toolName | ||
* The name of the tool component. | ||
* @param toolFullName | ||
* The name of the tool component along with its version and any other useful identifying information, such as its | ||
* locale. | ||
* @param toolInformationUri | ||
* The absolute URI at which information about this version of the tool component can be found. | ||
* @param organization | ||
* The organization or company that produced the tool component. | ||
* @param semanticVersion | ||
* The tool component version in the format specified by Semantic Versioning 2.0. | ||
* @param sarifVersion | ||
* The SARIF format version of the resulting log file. | ||
* @param resultConverter | ||
* A transformer class to map from Finding nodes to a SARIF `Result`. | ||
* @param customSerializers | ||
* Additional JSON serializers for any additional properties for [[io.shiftleft.semanticcpg.sarif.Sarif]] derived | ||
* classes. | ||
*/ | ||
case class SarifConfig( | ||
toolName: String = "Joern", | ||
toolFullName: String = "Joern - The Bug Hunter's Workbench", | ||
toolInformationUri: URI = URI("https://joern.io"), | ||
organization: String = "Joern.io", | ||
semanticVersion: String = "0.0.1", | ||
sarifVersion: SarifVersion = SarifVersion.V2_1_0, | ||
resultConverter: ScanResultToSarifConverter = JoernScanResultToSarifConverter(), | ||
customSerializers: List[Serializer[?]] = SarifSchema.serializers | ||
) | ||
|
||
object SarifConfig { | ||
|
||
enum SarifVersion { | ||
case V2_1_0 | ||
} | ||
|
||
} |
Oops, something went wrong.