Skip to content

Commit 3243579

Browse files
Update hsec-tools parser for the cwe: name syntax
1 parent 1d58822 commit 3243579

File tree

1 file changed

+12
-2
lines changed
  • code/hsec-tools/src/Security/Advisories

1 file changed

+12
-2
lines changed

code/hsec-tools/src/Security/Advisories/Parse.hs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import qualified Data.Map as Map
3737
import Data.Sequence (Seq((:<|)))
3838
import qualified Data.Set as Set
3939
import qualified Data.Text as T
40+
import qualified Data.Text.Read as T
4041
import qualified Data.Text.Lazy as T (toStrict)
4142
import Data.Time (LocalTime(..), ZonedTime(..), midnight, utc)
4243
import Distribution.Parsec (eitherParsec)
@@ -420,8 +421,17 @@ onKey tbl k absent present =
420421

421422
isCWE :: TOML.Value -> TableParser CWE.CWEID
422423
isCWE (TOML.Integer i) | Just cwe <- CWE.mkCWEID i, Map.member cwe CWE.cweNames = pure cwe
423-
isCWE (TOML.String txt) | Just cwe <- Map.lookup txt CWE.cweIds = pure cwe
424-
isCWE other = throwError $ InvalidFormat "Integer" (describeValue other) "42"
424+
isCWE (TOML.String txt) = case T.breakOn ":" txt of
425+
(numTxt, name) -> case T.decimal numTxt of
426+
Right (num, "") -> do
427+
cwe <- isCWE (TOML.Integer num)
428+
case T.strip (T.drop 1 name) of
429+
"" -> pure cwe
430+
expectedName -> case Map.lookup cwe CWE.cweNames of
431+
Just cweName | expectedName == cweName -> pure cwe
432+
_ -> error ("bad name: " <> show cwe <> ", " <> show expectedName)
433+
_ -> error ("bad num: "<> show numTxt)
434+
isCWE other = throwError $ InvalidFormat "CWE ID" (describeValue other) "42"
425435

426436
isString :: TOML.Value -> TableParser T.Text
427437
isString (TOML.String txt) = pure txt

0 commit comments

Comments
 (0)