@@ -36,20 +36,17 @@ import qualified Commonmark.Parser as Commonmark
36
36
import Commonmark.Types (HasAttributes (.. ), IsBlock (.. ), IsInline (.. ), Rangeable (.. ), SourceRange (.. ))
37
37
import Commonmark.Pandoc (Cm (unCm ))
38
38
import qualified Toml
39
- import qualified Toml.Pretty as Toml
40
- import qualified Toml.FromValue as Toml
41
- import qualified Toml.FromValue.Matcher as Toml
42
- import qualified Toml.ToValue as Toml
39
+ import qualified Toml.Syntax as Toml (startPos )
40
+ import qualified Toml.Schema as Toml
43
41
import Text.Pandoc.Builder (Blocks , Many (.. ))
44
42
import Text.Pandoc.Definition (Block (.. ), Inline (.. ), Pandoc (.. ))
45
43
import Text.Pandoc.Walk (query )
46
44
import Text.Parsec.Pos (sourceLine )
47
45
48
46
import Security.Advisories.Core.HsecId
49
47
import Security.Advisories.Core.Advisory
50
- import Security.OSV (Reference (.. ), referenceTypes )
48
+ import Security.OSV (Reference (.. ), ReferenceType , referenceTypes )
51
49
import qualified Security.CVSS as CVSS
52
-
53
50
-- | A source of attributes supplied out of band from the advisory
54
51
-- content. Values provided out of band are treated according to
55
52
-- the 'AttributeOverridePolicy'.
@@ -80,7 +77,7 @@ data ParseAdvisoryError
80
77
= MarkdownError Commonmark. ParseError T. Text
81
78
| MarkdownFormatError T. Text
82
79
| TomlError String T. Text
83
- | AdvisoryError [Toml. MatchMessage ] T. Text
80
+ | AdvisoryError [Toml. MatchMessage Toml. Position ] T. Text
84
81
deriving stock (Eq , Show , Generic )
85
82
86
83
-- | The main parsing function. 'OutOfBandAttributes' are handled
@@ -99,7 +96,7 @@ parseAdvisory policy attrs raw = do
99
96
(frontMatter, rest) <- first MarkdownFormatError $ advisoryDoc markdown
100
97
let doc = Pandoc mempty rest
101
98
! summary <- first MarkdownFormatError $ parseAdvisorySummary doc
102
- table <- case Toml. parse ( T. unpack frontMatter) of
99
+ table <- case Toml. parse frontMatter of
103
100
Left e -> Left (TomlError e (T. pack e))
104
101
Right t -> Right t
105
102
@@ -129,10 +126,8 @@ parseAdvisory policy attrs raw = do
129
126
(Commonmark. commonmark " input" raw :: Either Commonmark. ParseError (Html () ))
130
127
131
128
case parseAdvisoryTable attrs policy doc summary details html table of
132
- Toml. Failure es -> Left (AdvisoryError es (T. pack (unlines (map Toml. prettyMatchMessage es))))
133
- Toml. Success warnings adv
134
- | null warnings -> pure adv
135
- | otherwise -> Left (AdvisoryError warnings (T. pack (unlines (map Toml. prettyMatchMessage warnings)))) -- treat warnings as errors
129
+ Left es -> Left (AdvisoryError es (T. pack (unlines (map Toml. prettyMatchMessage es))))
130
+ Right adv -> pure adv
136
131
137
132
where
138
133
firstPretty
@@ -156,11 +151,11 @@ parseAdvisoryTable
156
151
-> T. Text -- ^ summary
157
152
-> T. Text -- ^ details
158
153
-> T. Text -- ^ rendered HTML
159
- -> Toml. Table
160
- -> Toml. Result Toml. MatchMessage Advisory
154
+ -> Toml. Table' Toml. Position
155
+ -> Either [ Toml. MatchMessage Toml. Position ] Advisory
161
156
parseAdvisoryTable oob policy doc summary details html tab =
162
- Toml. runMatcher $
163
- do fm <- Toml. fromValue (Toml. Table tab)
157
+ Toml. runMatcherFatalWarn $
158
+ do fm <- Toml. fromValue (Toml. Table' Toml. startPos tab)
164
159
published <-
165
160
mergeOobMandatory policy
166
161
(oobPublished oob)
@@ -211,7 +206,7 @@ instance Toml.ToValue FrontMatter where
211
206
toValue = Toml. defaultTableToValue
212
207
213
208
instance Toml. ToTable FrontMatter where
214
- toTable x = Map. fromList
209
+ toTable x = Toml. table
215
210
[ " advisory" Toml. .= frontMatterAdvisory x
216
211
, " affected" Toml. .= frontMatterAffected x
217
212
, " references" Toml. .= frontMatterReferences x
@@ -253,7 +248,7 @@ instance Toml.ToValue AdvisoryMetadata where
253
248
toValue = Toml. defaultTableToValue
254
249
255
250
instance Toml. ToTable AdvisoryMetadata where
256
- toTable x = Map. fromList $
251
+ toTable x = Toml. table $
257
252
[" id" Toml. .= amdId x] ++
258
253
[" modified" Toml. .= y | Just y <- [amdModified x]] ++
259
254
[" date" Toml. .= y | Just y <- [amdPublished x]] ++
@@ -283,7 +278,7 @@ instance Toml.ToValue Affected where
283
278
toValue = Toml. defaultTableToValue
284
279
285
280
instance Toml. ToTable Affected where
286
- toTable x = Map. fromList $
281
+ toTable x = Toml. table $
287
282
[ " package" Toml. .= affectedPackage x
288
283
, " cvss" Toml. .= affectedCVSS x
289
284
, " versions" Toml. .= affectedVersions x
@@ -307,7 +302,7 @@ instance Toml.ToValue AffectedVersionRange where
307
302
toValue = Toml. defaultTableToValue
308
303
309
304
instance Toml. ToTable AffectedVersionRange where
310
- toTable x = Map. fromList $
305
+ toTable x = Toml. table $
311
306
(" introduced" Toml. .= affectedVersionRangeIntroduced x) :
312
307
[" fixed" Toml. .= y | Just y <- [affectedVersionRangeFixed x]]
313
308
@@ -316,7 +311,7 @@ instance Toml.FromValue HsecId where
316
311
fromValue v =
317
312
do s <- Toml. fromValue v
318
313
case parseHsecId s of
319
- Nothing -> fail " invalid HSEC-ID: expected HSEC-[0-9]{4,}-[0-9]{4,}"
314
+ Nothing -> Toml. failAt ( Toml. valueAnn v) " invalid HSEC-ID: expected HSEC-[0-9]{4,}-[0-9]{4,}"
320
315
Just x -> pure x
321
316
322
317
instance Toml. ToValue HsecId where
@@ -335,11 +330,11 @@ instance Toml.ToValue Keyword where
335
330
toValue (Keyword x) = Toml. toValue x
336
331
337
332
-- | Get a datetime with the timezone defaulted to UTC and the time defaulted to midnight
338
- getDefaultedZonedTime :: Toml. Value -> Toml. Matcher ZonedTime
339
- getDefaultedZonedTime (Toml. ZonedTime x) = pure x
340
- getDefaultedZonedTime (Toml. LocalTime x) = pure (ZonedTime x utc)
341
- getDefaultedZonedTime (Toml. Day x) = pure (ZonedTime (LocalTime x midnight) utc)
342
- getDefaultedZonedTime _ = fail " expected a date with optional time and timezone"
333
+ getDefaultedZonedTime :: Toml. Value' l -> Toml. Matcher l ZonedTime
334
+ getDefaultedZonedTime (Toml. ZonedTime' _ x) = pure x
335
+ getDefaultedZonedTime (Toml. LocalTime' _ x) = pure (ZonedTime x utc)
336
+ getDefaultedZonedTime (Toml. Day' _ x) = pure (ZonedTime (LocalTime x midnight) utc)
337
+ getDefaultedZonedTime v = Toml. failAt ( Toml. valueAnn v) " expected a date with optional time and timezone"
343
338
344
339
advisoryDoc :: Blocks -> Either T. Text (T. Text , [Block ])
345
340
advisoryDoc (Many blocks) = case blocks of
@@ -375,21 +370,22 @@ inlineText = query f
375
370
376
371
instance Toml. FromValue Reference where
377
372
fromValue = Toml. parseTableFromValue $
378
- do refTypeStr <- Toml. reqKey " type"
379
- refType <- case lookup refTypeStr (fmap swap referenceTypes) of
380
- Just a -> pure a
381
- Nothing ->
382
- fail $
383
- " Invalid format for reference.type: " ++ T. unpack refTypeStr ++
384
- " should be one of: " ++ intercalate " , " (T. unpack . snd <$> referenceTypes)
385
- url <- Toml. reqKey " url"
386
- pure $ Reference refType url
373
+ do refType <- Toml. reqKey " type"
374
+ url <- Toml. reqKey " url"
375
+ pure (Reference refType url)
376
+
377
+ instance Toml. FromValue ReferenceType where
378
+ fromValue (Toml. Text' _ refTypeStr)
379
+ | Just a <- lookup refTypeStr (fmap swap referenceTypes) = pure a
380
+ fromValue v =
381
+ Toml. failAt (Toml. valueAnn v) $
382
+ " reference.type should be one of: " ++ intercalate " , " (T. unpack . snd <$> referenceTypes)
387
383
388
384
instance Toml. ToValue Reference where
389
385
toValue = Toml. defaultTableToValue
390
386
391
387
instance Toml. ToTable Reference where
392
- toTable x = Map. fromList
388
+ toTable x = Toml. table
393
389
[ " type" Toml. .= fromMaybe " UNKNOWN" (lookup (referencesType x) referenceTypes)
394
390
, " url" Toml. .= referencesUrl x
395
391
]
@@ -405,7 +401,7 @@ instance Toml.FromValue OS where
405
401
" mingw32" -> pure Windows
406
402
" netbsd" -> pure NetBSD
407
403
" openbsd" -> pure OpenBSD
408
- other -> fail (" Invalid OS: " ++ show other)
404
+ other -> Toml. failAt ( Toml. valueAnn v) (" Invalid OS: " ++ show other)
409
405
410
406
instance Toml. ToValue OS where
411
407
toValue x =
@@ -448,7 +444,7 @@ instance Toml.FromValue Architecture where
448
444
" sparc64" -> pure SPARC64
449
445
" vax" -> pure VAX
450
446
" x86_64" -> pure X86_64
451
- other -> fail (" Invalid architecture: " ++ show other)
447
+ other -> Toml. failAt ( Toml. valueAnn v) (" Invalid architecture: " ++ show other)
452
448
453
449
instance Toml. ToValue Architecture where
454
450
toValue x =
@@ -484,7 +480,7 @@ instance Toml.FromValue Version where
484
480
fromValue v =
485
481
do s <- Toml. fromValue v
486
482
case eitherParsec s of
487
- Left err -> fail (" parse error in version range: " ++ err)
483
+ Left err -> Toml. failAt ( Toml. valueAnn v) (" parse error in version range: " ++ err)
488
484
Right affected -> pure affected
489
485
490
486
instance Toml. ToValue Version where
@@ -494,7 +490,7 @@ instance Toml.FromValue VersionRange where
494
490
fromValue v =
495
491
do s <- Toml. fromValue v
496
492
case eitherParsec s of
497
- Left err -> fail (" parse error in version range: " ++ err)
493
+ Left err -> Toml. failAt ( Toml. valueAnn v) (" parse error in version range: " ++ err)
498
494
Right affected -> pure affected
499
495
500
496
instance Toml. ToValue VersionRange where
@@ -504,7 +500,7 @@ instance Toml.FromValue CVSS.CVSS where
504
500
fromValue v =
505
501
do s <- Toml. fromValue v
506
502
case CVSS. parseCVSS s of
507
- Left err -> fail (" parse error in cvss: " ++ show err)
503
+ Left err -> Toml. failAt ( Toml. valueAnn v) (" parse error in cvss: " ++ show err)
508
504
Right cvss -> pure cvss
509
505
510
506
instance Toml. ToValue CVSS. CVSS where
0 commit comments