module Ecluse.Core.Rules.Remediation (remediationVerdict) where
import Data.Foldable1 qualified as Foldable1
import Data.Map.Strict qualified as Map
import Data.Scientific (Scientific, fromFloatDigits)
import Ecluse.Core.Cve (AdvisoryRange (arCveId, arEpss, arSeverity), PackageAdvisories, advisoryIdsOf, affecting, fixedAt, hasFixes)
import Ecluse.Core.Rules.Types
remediationVerdict :: RemainingAdvisories -> PackageAdvisories -> RuleEvidence -> RuleVerdict
remediationVerdict :: RemainingAdvisories
-> PackageAdvisories -> RuleEvidence -> RuleVerdict
remediationVerdict RemainingAdvisories
remaining = PackageAdvisories -> RuleEvidence -> RuleVerdict
forPackage
where
onPartial :: PartialFix -> RuleVerdict
onPartial = RemainingAdvisories -> PartialFix -> RuleVerdict
partialVerdict RemainingAdvisories
remaining
forPackage :: PackageAdvisories -> RuleEvidence -> RuleVerdict
forPackage PackageAdvisories
advisories
| PackageAdvisories -> Bool
hasFixes PackageAdvisories
advisories = (PartialFix -> RuleVerdict)
-> PackageAdvisories -> RuleEvidence -> RuleVerdict
classify PartialFix -> RuleVerdict
onPartial PackageAdvisories
advisories
| Bool
otherwise = RuleVerdict -> RuleEvidence -> RuleVerdict
forall a b. a -> b -> a
const (Reason -> RuleVerdict
NoDecision Reason
FixesNoAdvisory)
data PartialFix = PartialFix
{ PartialFix -> AdvisoryIds
pfFixed :: AdvisoryIds
, PartialFix -> [AdvisoryRange]
pfFixedRows :: [AdvisoryRange]
, PartialFix -> AdvisoryIds
pfRemaining :: AdvisoryIds
, PartialFix -> Map Text [AdvisoryRange]
pfRemainingRows :: Map Text [AdvisoryRange]
}
classify :: (PartialFix -> RuleVerdict) -> PackageAdvisories -> RuleEvidence -> RuleVerdict
classify :: (PartialFix -> RuleVerdict)
-> PackageAdvisories -> RuleEvidence -> RuleVerdict
classify PartialFix -> RuleVerdict
onPartial PackageAdvisories
advisories RuleEvidence
ev = case [Text] -> Maybe AdvisoryIds
idsOf ([AdvisoryRange] -> [Text]
advisoryIdsOf [AdvisoryRange]
fixedRows) of
Maybe AdvisoryIds
Nothing -> Reason -> RuleVerdict
NoDecision Reason
FixesNoAdvisory
Just AdvisoryIds
fixed -> case [Text] -> Maybe AdvisoryIds
idsOf [Text]
openIds of
Maybe AdvisoryIds
Nothing -> Reason -> RuleVerdict
Allow (AdvisoryIds -> Reason
Remediates AdvisoryIds
fixed)
Just AdvisoryIds
remaining -> case [Text] -> Maybe AdvisoryIds
idsOf ([AdvisoryRange] -> [Text]
advisoryIdsOf [AdvisoryRange]
genuineRows) of
Maybe AdvisoryIds
Nothing -> Reason -> RuleVerdict
NoDecision (AdvisoryIds -> Reason
FixStillApplies AdvisoryIds
remaining)
Just AdvisoryIds
genuine -> PartialFix -> RuleVerdict
onPartial (AdvisoryIds
-> [AdvisoryRange]
-> AdvisoryIds
-> Map Text [AdvisoryRange]
-> PartialFix
PartialFix AdvisoryIds
genuine [AdvisoryRange]
genuineRows AdvisoryIds
remaining Map Text [AdvisoryRange]
openByAdvisory)
where
fixedRows :: [AdvisoryRange]
fixedRows = PackageAdvisories -> Version -> [AdvisoryRange]
fixedAt PackageAdvisories
advisories (RuleEvidence -> Version
evVersion RuleEvidence
ev)
openRows :: [AdvisoryRange]
openRows = PackageAdvisories -> Version -> [AdvisoryRange]
affecting PackageAdvisories
advisories (RuleEvidence -> Version
evVersion RuleEvidence
ev)
openIds :: [Text]
openIds = [AdvisoryRange] -> [Text]
advisoryIdsOf [AdvisoryRange]
openRows
openByAdvisory :: Map Text [AdvisoryRange]
openByAdvisory = ([AdvisoryRange] -> [AdvisoryRange] -> [AdvisoryRange])
-> [(Text, [AdvisoryRange])] -> Map Text [AdvisoryRange]
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith (([AdvisoryRange] -> [AdvisoryRange] -> [AdvisoryRange])
-> [AdvisoryRange] -> [AdvisoryRange] -> [AdvisoryRange]
forall a b c. (a -> b -> c) -> b -> a -> c
flip [AdvisoryRange] -> [AdvisoryRange] -> [AdvisoryRange]
forall a. Semigroup a => a -> a -> a
(<>)) [(AdvisoryRange -> Text
arCveId AdvisoryRange
row, [AdvisoryRange
row]) | AdvisoryRange
row <- [AdvisoryRange]
openRows]
genuineRows :: [AdvisoryRange]
genuineRows = (AdvisoryRange -> Bool) -> [AdvisoryRange] -> [AdvisoryRange]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Text -> Map Text [AdvisoryRange] -> Bool
forall k a. Ord k => k -> Map k a -> Bool
`Map.notMember` Map Text [AdvisoryRange]
openByAdvisory) (Text -> Bool) -> (AdvisoryRange -> Text) -> AdvisoryRange -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AdvisoryRange -> Text
arCveId) [AdvisoryRange]
fixedRows
idsOf :: [Text] -> Maybe AdvisoryIds
idsOf :: [Text] -> Maybe AdvisoryIds
idsOf = (NonEmpty Text -> AdvisoryIds)
-> Maybe (NonEmpty Text) -> Maybe AdvisoryIds
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NonEmpty Text -> AdvisoryIds
mkAdvisoryIds (Maybe (NonEmpty Text) -> Maybe AdvisoryIds)
-> ([Text] -> Maybe (NonEmpty Text)) -> [Text] -> Maybe AdvisoryIds
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Maybe (NonEmpty Text)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty
partialVerdict :: RemainingAdvisories -> PartialFix -> RuleVerdict
partialVerdict :: RemainingAdvisories -> PartialFix -> RuleVerdict
partialVerdict = \case
RemainingAdvisories
AbstainOnRemaining -> \PartialFix
partial -> Reason -> RuleVerdict
NoDecision (AdvisoryIds -> AdvisoryIds -> Reason
FixesButStillAffected (PartialFix -> AdvisoryIds
pfFixed PartialFix
partial) (PartialFix -> AdvisoryIds
pfRemaining PartialFix
partial))
Differential DifferentialLimits
limits ->
let cvss :: ScoreCheck
cvss = AdvisoryScore
-> (AdvisoryRange -> Maybe Double)
-> (Scientific -> Scientific -> Bool)
-> Double
-> ScoreGap
-> ScoreCheck
scoreCheck AdvisoryScore
Cvss AdvisoryRange -> Maybe Double
arSeverity Scientific -> Scientific -> Bool
forall a. Ord a => a -> a -> Bool
(<=) (DifferentialLimits -> Double
dlMaxRemainingCvss DifferentialLimits
limits) (DifferentialLimits -> ScoreGap
dlCvssGap DifferentialLimits
limits)
epss :: ScoreCheck
epss = AdvisoryScore
-> (AdvisoryRange -> Maybe Double)
-> (Scientific -> Scientific -> Bool)
-> Double
-> ScoreGap
-> ScoreCheck
scoreCheck AdvisoryScore
Epss AdvisoryRange -> Maybe Double
arEpss Scientific -> Scientific -> Bool
forall a. Ord a => a -> a -> Bool
(<) (DifferentialLimits -> Double
dlMaxRemainingEpss DifferentialLimits
limits) (DifferentialLimits -> ScoreGap
dlEpssGap DifferentialLimits
limits)
in DifferentialLimits
-> ScoreCheck -> ScoreCheck -> PartialFix -> RuleVerdict
differentialVerdict DifferentialLimits
limits ScoreCheck
cvss ScoreCheck
epss
data ScoreCheck = ScoreCheck
{ ScoreCheck -> AdvisoryScore
ckScore :: AdvisoryScore
, ScoreCheck -> AdvisoryRange -> Maybe Double
ckRead :: AdvisoryRange -> Maybe Double
, ScoreCheck -> Scientific -> Bool
ckWithinCeiling :: Scientific -> Bool
, ScoreCheck -> Maybe (Scientific -> Scientific -> Bool)
ckGapHolds :: Maybe (Scientific -> Scientific -> Bool)
}
scoreCheck :: AdvisoryScore -> (AdvisoryRange -> Maybe Double) -> (Scientific -> Scientific -> Bool) -> Double -> ScoreGap -> ScoreCheck
scoreCheck :: AdvisoryScore
-> (AdvisoryRange -> Maybe Double)
-> (Scientific -> Scientific -> Bool)
-> Double
-> ScoreGap
-> ScoreCheck
scoreCheck AdvisoryScore
score AdvisoryRange -> Maybe Double
readScore Scientific -> Scientific -> Bool
withinCeiling Double
limit ScoreGap
gap =
ScoreCheck
{ ckScore :: AdvisoryScore
ckScore = AdvisoryScore
score
, ckRead :: AdvisoryRange -> Maybe Double
ckRead = AdvisoryRange -> Maybe Double
readScore
, ckWithinCeiling :: Scientific -> Bool
ckWithinCeiling = let bound :: Maybe Scientific
bound = Double -> Maybe Scientific
decimal Double
limit in \Scientific
highest -> Bool -> (Scientific -> Bool) -> Maybe Scientific -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Scientific
highest Scientific -> Scientific -> Bool
`withinCeiling`) Maybe Scientific
bound
, ckGapHolds :: Maybe (Scientific -> Scientific -> Bool)
ckGapHolds = case ScoreGap
gap of
ScoreGap
NoGapRequired -> Maybe (Scientific -> Scientific -> Bool)
forall a. Maybe a
Nothing
GapOfAtLeast Double
least -> let bound :: Maybe Scientific
bound = Double -> Maybe Scientific
decimal Double
least in (Scientific -> Scientific -> Bool)
-> Maybe (Scientific -> Scientific -> Bool)
forall a. a -> Maybe a
Just (\Scientific
fixed Scientific
remaining -> Bool -> (Scientific -> Bool) -> Maybe Scientific -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Scientific
fixed Scientific -> Scientific -> Scientific
forall a. Num a => a -> a -> a
- Scientific
remaining Scientific -> Scientific -> Bool
forall a. Ord a => a -> a -> Bool
>=) Maybe Scientific
bound)
}
differentialVerdict :: DifferentialLimits -> ScoreCheck -> ScoreCheck -> PartialFix -> RuleVerdict
differentialVerdict :: DifferentialLimits
-> ScoreCheck -> ScoreCheck -> PartialFix -> RuleVerdict
differentialVerdict DifferentialLimits
limits ScoreCheck
cvss ScoreCheck
epss PartialFix
partial = case [LimitShortfall] -> Maybe (NonEmpty LimitShortfall)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty ([LimitShortfall]
cvssShortfalls [LimitShortfall] -> [LimitShortfall] -> [LimitShortfall]
forall a. Semigroup a => a -> a -> a
<> [LimitShortfall]
epssShortfalls) of
Maybe (NonEmpty LimitShortfall)
Nothing -> Reason -> RuleVerdict
Allow (ScoreComparison -> Reason
RemediatesWithinLimits ScoreComparison
comparison)
Just NonEmpty LimitShortfall
shortfalls -> Reason -> RuleVerdict
NoDecision (ScoreComparison -> LimitShortfalls -> Reason
RemediationBeyondLimits ScoreComparison
comparison (NonEmpty LimitShortfall -> LimitShortfalls
mkLimitShortfalls NonEmpty LimitShortfall
shortfalls))
where
(ScoreSides
cvssSides, [LimitShortfall]
cvssShortfalls) = ScoreCheck -> PartialFix -> (ScoreSides, [LimitShortfall])
checkScore ScoreCheck
cvss PartialFix
partial
(ScoreSides
epssSides, [LimitShortfall]
epssShortfalls) = ScoreCheck -> PartialFix -> (ScoreSides, [LimitShortfall])
checkScore ScoreCheck
epss PartialFix
partial
comparison :: ScoreComparison
comparison = AdvisoryIds
-> AdvisoryIds
-> ScoreSides
-> ScoreSides
-> DifferentialLimits
-> ScoreComparison
ScoreComparison (PartialFix -> AdvisoryIds
pfFixed PartialFix
partial) (PartialFix -> AdvisoryIds
pfRemaining PartialFix
partial) ScoreSides
cvssSides ScoreSides
epssSides DifferentialLimits
limits
checkScore :: ScoreCheck -> PartialFix -> (ScoreSides, [LimitShortfall])
checkScore :: ScoreCheck -> PartialFix -> (ScoreSides, [LimitShortfall])
checkScore ScoreCheck
check PartialFix
partial =
(ScoreReading -> ScoreReading -> ScoreSides
ScoreSides (Maybe Double -> ScoreReading
reading Maybe Double
fixedHighest) (Maybe Double -> ScoreReading
reading Maybe Double
remainingHighest), [LimitShortfall]
unscored [LimitShortfall] -> [LimitShortfall] -> [LimitShortfall]
forall a. Semigroup a => a -> a -> a
<> [LimitShortfall]
aboveCeiling [LimitShortfall] -> [LimitShortfall] -> [LimitShortfall]
forall a. Semigroup a => a -> a -> a
<> [LimitShortfall]
gap)
where
score :: AdvisoryScore
score = ScoreCheck -> AdvisoryScore
ckScore ScoreCheck
check
scoreOf :: AdvisoryRange -> Maybe Double
scoreOf = Double -> Maybe Double
finite (Double -> Maybe Double)
-> (AdvisoryRange -> Maybe Double) -> AdvisoryRange -> Maybe Double
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< ScoreCheck -> AdvisoryRange -> Maybe Double
ckRead ScoreCheck
check
fixedHighest :: Maybe Double
fixedHighest = (NonEmpty Double -> Double) -> [Double] -> Maybe Double
forall a b. (NonEmpty a -> b) -> [a] -> Maybe b
viaNonEmpty NonEmpty Double -> Double
forall a. Ord a => NonEmpty a -> a
forall (t :: * -> *) a. (Foldable1 t, Ord a) => t a -> a
Foldable1.maximum ((AdvisoryRange -> Maybe Double) -> [AdvisoryRange] -> [Double]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe AdvisoryRange -> Maybe Double
scoreOf (PartialFix -> [AdvisoryRange]
pfFixedRows PartialFix
partial))
perAdvisory :: [(Text, Maybe [Double])]
perAdvisory = [(Text
advisory, (AdvisoryRange -> Maybe Double)
-> [AdvisoryRange] -> Maybe [Double]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse AdvisoryRange -> Maybe Double
scoreOf ([AdvisoryRange]
-> Text -> Map Text [AdvisoryRange] -> [AdvisoryRange]
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault [] Text
advisory (PartialFix -> Map Text [AdvisoryRange]
pfRemainingRows PartialFix
partial))) | Text
advisory <- NonEmpty Text -> [Text]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (AdvisoryIds -> NonEmpty Text
unAdvisoryIds (PartialFix -> AdvisoryIds
pfRemaining PartialFix
partial))]
remainingHighest :: Maybe Double
remainingHighest = (NonEmpty Double -> Double) -> [Double] -> Maybe Double
forall a b. (NonEmpty a -> b) -> [a] -> Maybe b
viaNonEmpty NonEmpty Double -> Double
forall a. Ord a => NonEmpty a -> a
forall (t :: * -> *) a. (Foldable1 t, Ord a) => t a -> a
Foldable1.maximum ([[Double]] -> [Double]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Double]
scores | (Text
_, Just [Double]
scores) <- [(Text, Maybe [Double])]
perAdvisory])
unscored :: [LimitShortfall]
unscored = [AdvisoryScore -> AdvisoryIds -> LimitShortfall
RemainingUnscored AdvisoryScore
score (NonEmpty Text -> AdvisoryIds
mkAdvisoryIds NonEmpty Text
ids) | Just NonEmpty Text
ids <- [[Text] -> Maybe (NonEmpty Text)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty [Text
advisory | (Text
advisory, Maybe [Double]
Nothing) <- [(Text, Maybe [Double])]
perAdvisory]]]
aboveCeiling :: [LimitShortfall]
aboveCeiling = [AdvisoryScore -> LimitShortfall
RemainingAboveCeiling AdvisoryScore
score | Just Double
highest <- [Maybe Double
remainingHighest], Bool -> Bool
not (ScoreCheck -> Scientific -> Bool
ckWithinCeiling ScoreCheck
check (Double -> Scientific
forall a. RealFloat a => a -> Scientific
fromFloatDigits Double
highest))]
gap :: [LimitShortfall]
gap = case (ScoreCheck -> Maybe (Scientific -> Scientific -> Bool)
ckGapHolds ScoreCheck
check, Maybe Double
fixedHighest, Maybe Double
remainingHighest) of
(Maybe (Scientific -> Scientific -> Bool)
Nothing, Maybe Double
_, Maybe Double
_) -> []
(Just Scientific -> Scientific -> Bool
_, Maybe Double
Nothing, Maybe Double
_) -> [AdvisoryScore -> LimitShortfall
FixedUnscored AdvisoryScore
score]
(Just Scientific -> Scientific -> Bool
holds, Just Double
fixed, Just Double
remaining) | Bool -> Bool
not (Scientific -> Scientific -> Bool
holds (Double -> Scientific
forall a. RealFloat a => a -> Scientific
fromFloatDigits Double
fixed) (Double -> Scientific
forall a. RealFloat a => a -> Scientific
fromFloatDigits Double
remaining)) -> [AdvisoryScore -> LimitShortfall
GapBelowMinimum AdvisoryScore
score]
(Just Scientific -> Scientific -> Bool
_, Just Double
_, Maybe Double
_) -> []
reading :: Maybe Double -> ScoreReading
reading :: Maybe Double -> ScoreReading
reading = ScoreReading
-> (Double -> ScoreReading) -> Maybe Double -> ScoreReading
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ScoreReading
Unscored Double -> ScoreReading
Scored
finite :: Double -> Maybe Double
finite :: Double -> Maybe Double
finite Double
x = Double
x Double -> Maybe () -> Maybe Double
forall a b. a -> Maybe b -> Maybe a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not (Double -> Bool
forall a. RealFloat a => a -> Bool
isNaN Double
x Bool -> Bool -> Bool
|| Double -> Bool
forall a. RealFloat a => a -> Bool
isInfinite Double
x))
decimal :: Double -> Maybe Scientific
decimal :: Double -> Maybe Scientific
decimal = (Double -> Scientific) -> Maybe Double -> Maybe Scientific
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Double -> Scientific
forall a. RealFloat a => a -> Scientific
fromFloatDigits (Maybe Double -> Maybe Scientific)
-> (Double -> Maybe Double) -> Double -> Maybe Scientific
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Maybe Double
finite