-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle CWE name #116
Open
TristanCacqueray
wants to merge
10
commits into
haskell:main
Choose a base branch
from
TristanCacqueray:cwe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Handle CWE name #116
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1ba67af
Add CWE library
TristanCacqueray 2445532
Replace cwe numbers with their names
TristanCacqueray 24d40a9
Update cwe module names and import the full list
TristanCacqueray e0d8ac6
Use pattern synonyms to simplify xml match
TristanCacqueray 608c594
Add CWE names to existing advisories
TristanCacqueray 3d9785e
Update hsec-tool to use the new CWE data and validation
TristanCacqueray f5ac50e
Apply hlint suggestion
TristanCacqueray 5a35e2d
Regenerate haskell-ci
TristanCacqueray ab25544
Add cwe to the hsec-core build dependencies
TristanCacqueray 0a5658b
Update cwe parser for latest toml library
TristanCacqueray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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
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 |
---|---|---|
|
@@ -3,4 +3,5 @@ packages: code/*/*.cabal | |
package hsec-core | ||
package hsec-tools | ||
package cvss | ||
package cwe | ||
package osv |
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 |
---|---|---|
|
@@ -10,7 +10,9 @@ author: Tristan de Cacqueray | |
maintainer: [email protected] | ||
category: Data | ||
extra-doc-files: CHANGELOG.md | ||
tested-with: GHC ==8.10.7 || ==9.0.2 || ==9.2.8 || ==9.4.8 || ==9.6.3 || ==9.8.1 | ||
|
||
tested-with: | ||
GHC ==8.10.7 || ==9.0.2 || ==9.2.8 || ==9.4.8 || ==9.6.3 || ==9.8.1 | ||
|
||
library | ||
exposed-modules: Security.CVSS | ||
|
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,5 @@ | ||
# Changelog | ||
|
||
## 0.1 | ||
|
||
- Initial version |
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 @@ | ||
#!/usr/bin/env cabal | ||
{- cabal: | ||
build-depends: base, xml | ||
-} | ||
{-# LANGUAGE NamedFieldPuns, PatternSynonyms #-} | ||
-- | Use this script to update the Security.CWE.Data module: | ||
-- Download and extract https://cwe.mitre.org/data/xml/cwec_latest.xml.zip | ||
-- 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 | ||
module Main where | ||
|
||
import Data.List | ||
import Data.Maybe | ||
import Text.Read | ||
|
||
import qualified Text.XML.Light as XML | ||
|
||
main :: IO () | ||
main = do | ||
db <- readXML <$> getContents | ||
putStrLn $ unlines $ renderSource $ db | ||
|
||
data Weakness = Weakness | ||
{ wid :: Word | ||
, wname :: String | ||
} | ||
|
||
pattern XElement name content <- XML.Element (XML.QName name _ _) _ content _ | ||
|
||
readXML :: String -> [Weakness] | ||
readXML str = case XML.parseXMLDoc str of | ||
Just (XElement "Weakness_Catalog" (_ : (XML.Elem (XElement "Weaknesses" xs)) : _)) -> | ||
mapMaybe toWeakness xs | ||
n -> error $ "Couldn't match: " <> take 512 (show n) | ||
where | ||
toWeakness (XML.Elem (XML.Element (XML.QName "Weakness" _ _) attrs _ _)) = Just (Weakness{wid, wname}) | ||
where | ||
wid = fromMaybe (error "invalid num") $ readMaybe =<< XML.lookupAttrBy ((==) "ID" . XML.qName) attrs | ||
wname = fromMaybe (error "missing name") $ XML.lookupAttrBy ((==) "Name" . XML.qName) attrs | ||
toWeakness e = Nothing | ||
|
||
renderSource :: [Weakness] -> [String] | ||
renderSource xs = | ||
[ "{-# LANGUAGE OverloadedStrings #-}" | ||
, "module Security.CWE.Data where" | ||
, "import Data.Text" | ||
, "cweData :: [(Word, Text)]" | ||
, "cweData = [" | ||
] | ||
<> map renderEntry (zip [0 ..] (sortOn wid xs)) | ||
<> [" ]"] | ||
where | ||
renderEntry (pos, weakness) = " " <> sep <> " (" <> show (wid weakness) <> ", \"" <> name <> "\")" | ||
where | ||
sep = if pos == 0 then " " else "," | ||
-- Remove extra info in parenthesis | ||
name = dropWhileEnd (== ' ') $ takeWhile (/= '(') $ escape $ wname weakness | ||
escape ('\\':rest) = '\\' : '\\' : escape rest | ||
escape (x:rest) = x : escape rest | ||
escape [] = [] | ||
renderEntry _ = "" |
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,27 @@ | ||
cabal-version: 2.4 | ||
name: cwe | ||
version: 0.1 | ||
synopsis: Common Weakness Enumaration database | ||
description: Use this library to lookup or validate CWE numbers. | ||
license: BSD-3-Clause | ||
author: Tristan de Cacqueray | ||
maintainer: [email protected] | ||
category: Data | ||
extra-doc-files: CHANGELOG.md | ||
|
||
tested-with: | ||
GHC ==8.10.7 || ==9.0.2 || ==9.2.8 || ==9.4.8 || ==9.6.3 || ==9.8.1 | ||
|
||
library | ||
exposed-modules: Security.CWE | ||
other-modules: Security.CWE.Data | ||
build-depends: | ||
, base >=4.14 && <5 | ||
, containers >=0.6 && <0.7 | ||
, text >=1.2 && <3 | ||
|
||
hs-source-dirs: src | ||
default-language: Haskell2010 | ||
ghc-options: | ||
-Wall -Wcompat -Widentities -Wincomplete-record-updates | ||
-Wincomplete-uni-patterns -Wpartial-fields -Wredundant-constraints |
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,28 @@ | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
module Security.CWE (CWEID, unCWEID, mkCWEID, cweNames, cweIds) where | ||
|
||
import Security.CWE.Data | ||
import Data.Text (Text) | ||
import Data.Coerce | ||
import Data.Map.Strict as Map | ||
import Data.Bits | ||
|
||
-- | A CWE identifier. | ||
newtype CWEID = CWEID Word | ||
deriving newtype (Eq, Ord, Show) | ||
|
||
-- | Access the underlying data. | ||
unCWEID :: CWEID -> Word | ||
unCWEID (CWEID cwe) = cwe | ||
|
||
mkCWEID :: (Integral a, Bits a) => a -> Maybe CWEID | ||
mkCWEID num = CWEID <$> toIntegralSized num | ||
|
||
-- | A map to lookup CWE names. | ||
cweNames :: Map CWEID Text | ||
cweNames = Map.fromList (coerce cweData) | ||
|
||
-- | A map to lookup CWEID. | ||
cweIds :: Map Text CWEID | ||
cweIds = Map.fromList $ (\(k, v) -> (v, k)) <$> coerce cweData |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's unusual
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it's just for one-shot codegen I figured it was not necessary to add this to the cabal file.