-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the `Security.Advisories.Convert.OSV` module, which defines the conversion from our `Advisory` data type to the OSV `Model`. Currently, no database-specific or ecosystem-specific fields are set. Whether or how to use those fields is a matter for future discussion. Add the `osv` subcommand to `hsec-tools`. It works in the same way as `check`, but emits the encoded OSV JSON data. Later commits will add the CI workflows to generate and publish the OSV data. Fixes: #3
- Loading branch information
1 parent
1c0fd46
commit 8ee2421
Showing
3 changed files
with
78 additions
and
0 deletions.
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
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,60 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Security.Advisories.Convert.OSV | ||
( convert | ||
) | ||
where | ||
|
||
import qualified Data.Text as T | ||
import Data.Time (zonedTimeToUTC) | ||
import Data.Void | ||
|
||
import Security.Advisories | ||
import qualified Security.OSV as OSV | ||
|
||
convert :: Advisory -> OSV.Model Void Void Void Void | ||
convert adv = | ||
( OSV.newModel' | ||
(advisoryId adv) | ||
(zonedTimeToUTC $ advisoryModified adv) | ||
) | ||
{ OSV.modelPublished = Just $ zonedTimeToUTC (advisoryPublished adv) | ||
, OSV.modelAliases = advisoryAliases adv | ||
, OSV.modelSummary = Just $ advisorySummary adv | ||
, OSV.modelDetails = Just $ advisoryDetails adv | ||
, OSV.modelReferences = advisoryReferences adv | ||
, OSV.modelAffected = fmap mkAffected (advisoryAffected adv) | ||
} | ||
|
||
mkAffected :: Affected -> OSV.Affected Void Void Void | ||
mkAffected aff = | ||
OSV.Affected | ||
{ OSV.affectedPackage = mkPackage (affectedPackage aff) | ||
, OSV.affectedRanges = pure $ mkRange (affectedVersions aff) | ||
, OSV.affectedSeverity = mkSeverity (affectedCVSS aff) | ||
, OSV.affectedEcosystemSpecific = Nothing | ||
, OSV.affectedDatabaseSpecific = Nothing | ||
} | ||
|
||
mkPackage :: T.Text -> OSV.Package | ||
mkPackage name = OSV.Package | ||
{ OSV.packageName = name | ||
, OSV.packageEcosystem = "Hackage" | ||
, OSV.packagePurl = Nothing | ||
} | ||
|
||
-- NOTE: This is unpleasant. But we will eventually switch to a | ||
-- proper CVSS type and the unpleasantness will go away. | ||
-- | ||
mkSeverity :: T.Text -> [OSV.Severity] | ||
mkSeverity s = case T.take 6 s of | ||
"CVSS:2" -> [OSV.SeverityCvss2 s] | ||
"CVSS:3" -> [OSV.SeverityCvss3 s] | ||
_ -> [] -- unexpected; don't include severity | ||
|
||
mkRange :: [AffectedVersionRange] -> OSV.Range Void | ||
mkRange ranges = OSV.RangeEcosystem (foldMap mkEvs ranges) Nothing | ||
where | ||
mkEvs range = | ||
OSV.EventIntroduced (affectedVersionRangeIntroduced range) | ||
: maybe [] (pure . OSV.EventFixed) (affectedVersionRangeFixed range) |