Skip to content

Commit 8c5d3ab

Browse files
Update cwe module names and import the full list
1 parent 2d3144f commit 8c5d3ab

File tree

7 files changed

+1019
-569
lines changed

7 files changed

+1019
-569
lines changed

code/cwe/RenderCsvData.hs

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,68 @@
11
#!/usr/bin/env cabal
22
{- cabal:
3-
build-depends: base, csv
3+
build-depends: base, xml
44
-}
5-
-- | Use this script to update the CWE.Raw module:
6-
-- Go to https://cwe.mitre.org/data/downloads.html
7-
-- Download and extract the 'Software Development' and 'CWE Simplified Mapping' CSV.zip files
8-
-- Run the following command: ./RenderCsvData.hs | fourmolu --stdin-input-file ./src/CWE/Raw.hs > src/CWE/Raw.hs
5+
{-# LANGUAGE NamedFieldPuns #-}
6+
-- | Use this script to update the Security.CWE.Data module:
7+
-- Download and extract https://cwe.mitre.org/data/xml/cwec_latest.xml.zip
8+
-- Run the following command: cat cwec_v4.12.xml | ./RenderCsvData.hs | fourmolu --stdin-input-file ./src/Security/CWE/Data.hs > src/Security/CWE/Data.hs
99
module Main where
1010

1111
import Data.List
1212
import Data.Maybe
13-
import Text.CSV
1413
import Text.Read
1514

15+
import qualified Text.XML.Light as XML
16+
1617
main :: IO ()
1718
main = do
18-
dbs <- traverse readCSV ["699.csv", "1003.csv"]
19-
putStrLn $ unlines $ renderSource $ concat dbs
19+
db <- readXML <$> getContents
20+
putStrLn $ unlines $ renderSource $ db
21+
22+
data Weakness = Weakness
23+
{ wid :: Word
24+
, wname :: String
25+
}
2026

21-
readCSV :: FilePath -> IO CSV
22-
readCSV fp = do
23-
txt <- readFile fp
24-
case Text.CSV.parseCSV "stdin" txt of
25-
Left e -> error ("bad csv: " <> show e)
26-
Right records -> pure (drop 1 records)
27+
readXML :: String -> [Weakness]
28+
readXML str = case XML.parseXMLDoc str of
29+
Just
30+
( XML.Element
31+
(XML.QName "Weakness_Catalog" _ _)
32+
_
33+
( _
34+
: ( XML.Elem
35+
((XML.Element (XML.QName "Weaknesses" _ _) _ xs _))
36+
)
37+
: _
38+
)
39+
_
40+
) -> mapMaybe toWeakness xs
41+
n -> error (show n)
42+
where
43+
toWeakness (XML.Elem (XML.Element (XML.QName "Weakness" _ _) attrs _ _)) = Just (Weakness{wid, wname})
44+
where
45+
wid = fromMaybe (error "invalid num") $ readMaybe =<< XML.lookupAttrBy ((==) "ID" . XML.qName) attrs
46+
wname = fromMaybe (error "missing name") $ XML.lookupAttrBy ((==) "Name" . XML.qName) attrs
47+
toWeakness e = Nothing
2748

28-
renderSource :: [Record] -> [String]
49+
renderSource :: [Weakness] -> [String]
2950
renderSource xs =
3051
[ "{-# LANGUAGE OverloadedStrings #-}"
31-
, "module CWE.Data where"
52+
, "module Security.CWE.Data where"
3253
, "import Data.Text"
3354
, "cweData :: [(Word, Text)]"
3455
, "cweData = ["
3556
]
36-
<> map renderEntry (zip [0 ..] (sortOn byNum xs))
57+
<> map renderEntry (zip [0 ..] (sortOn wid xs))
3758
<> [" ]"]
3859
where
39-
byNum (num : _) = fromMaybe (42 :: Int) (readMaybe num)
40-
renderEntry (pos, (num : desc : _)) = " " <> sep <> " (" <> num <> ", \"" <> name <> "\")"
60+
renderEntry (pos, weakness) = " " <> sep <> " (" <> show (wid weakness) <> ", \"" <> name <> "\")"
4161
where
4262
sep = if pos == 0 then " " else ","
4363
-- Remove extra info in parenthesis
44-
name = dropWhileEnd (== ' ') $ takeWhile (/= '(') desc
64+
name = dropWhileEnd (== ' ') $ takeWhile (/= '(') $ escape $ wname weakness
65+
escape ('\\':rest) = '\\' : '\\' : escape rest
66+
escape (x:rest) = x : escape rest
67+
escape [] = []
4568
renderEntry _ = ""

code/cwe/cwe.cabal

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ extra-doc-files: CHANGELOG.md
1111
tested-with: GHC ==8.10.7 || ==9.0.2 || ==9.2.7 || ==9.4.5 || ==9.6.2
1212

1313
library
14-
exposed-modules: CWE
15-
other-modules: CWE.Data
14+
exposed-modules: Security.CWE
15+
other-modules: Security.CWE.Data
1616
build-depends:
1717
, base >=4.14 && <5
1818
, containers >=0.6 && <0.7
19-
, parsec >=3 && <4
20-
, text >= 1.2 && < 3
21-
19+
, text >=1.2 && <3
2220

2321
hs-source-dirs: src
2422
default-language: Haskell2010

0 commit comments

Comments
 (0)