1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::{constants::CWE_REGEX, Formalize};

#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
pub struct Vulnerability {
    #[serde(alias = "Name", alias = "NAME")]
    pub name: String,
    #[serde(alias = "Description", alias = "DESCRIPTION")]
    pub description: String,
    #[serde(default, alias = "Technical", alias = "TECHNICAL")]
    pub technical: bool,
    #[serde(alias = "Class", alias = "CLASS")]
    pub class: String,
}

impl Formalize for Vulnerability {
    fn formalize(&mut self) -> Result<()> {
        if !CWE_REGEX.is_match(&self.class) {
            return Err(anyhow!("Vulnerability class must match /CWE-\\d+/"));
        }

        Ok(())
    }
}

pub type Vulnerabilities = HashMap<String, Vulnerability>;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parse_sdl;

    #[test]
    fn vulnerability_is_parsed() {
        let sdl = r#"
            name: Some vulnerability
            description: some-description
            class: CWE-123
        "#;
        serde_yaml::from_str::<Vulnerability>(sdl).unwrap();
    }

    #[test]
    fn default_technical_field_is_false() {
        let sdl = r#"
            name: Some vulnerability
            description: some-description
            class: CWE-123
        "#;
        let vulnerability = serde_yaml::from_str::<Vulnerability>(sdl).unwrap();
        assert!(!vulnerability.technical);
    }

    #[test]
    fn vulnerability_is_parsed_in_scenario() {
        let sdl = r#"
            name: test-scenario
            description: some-description
            start: 2022-01-20T13:00:00Z
            end: 2022-01-20T23:00:00Z
            vulnerabilities:
                vuln-1:
                    name: Some vulnerability
                    description: some-description
                    class: CWE-1341
                vuln-2:
                    name: Some other vulnerability
                    description: some-description
                    technical: false
                    class: CWE-1343
        "#;
        let vulnerabilities = parse_sdl(sdl).unwrap().vulnerabilities;
        insta::with_settings!({sort_maps => true}, {
                insta::assert_yaml_snapshot!(vulnerabilities);
        });
    }

    #[test]
    fn parses_scenario_with_vulnerability_in_node() {
        let sdl = r#"
            name: test-scenario
            description: some-description
            start: 2022-01-20T13:00:00Z
            end: 2022-01-20T23:00:00Z
            vulnerabilities:
                vuln-1:
                    name: Some vulnerability
                    description: some-description
                    technical: true
                    class: CWE-1341
                vuln-2:
                    name: Some other vulnerability
                    description: some-description
                    technical: false
                    class: CWE-1343
            nodes:
                win-10:
                    type: VM
                    resources:
                        ram: 2 gib
                        cpu: 2
                    source: windows10
                    vulnerabilities:
                        - vuln-2
                        - vuln-1
        "#;
        parse_sdl(sdl).unwrap();
    }

    #[test]
    #[should_panic(expected = "Vulnerability \"vuln-4\" not found under Scenario Vulnerabilities")]
    fn missing_vulnerability_for_node() {
        let sdl = r#"
            name: test-scenario
            description: some-description
            start: 2022-01-20T13:00:00Z
            end: 2022-01-20T23:00:00Z
            vulnerabilities:
                vuln-1:
                    name: Some vulnerability
                    description: some-description
                    technical: true
                    class: CWE-1343
                vuln-2:
                    name: Some other vulnerability
                    description: some-description
                    technical: false
                    class: CWE-1343
            nodes:
                win-10:
                    type: VM
                    resources:
                        ram: 2 gib
                        cpu: 2
                    source: windows10
                    vulnerabilities:
                        - vuln-4
        "#;
        parse_sdl(sdl).unwrap();
    }

    #[test]
    fn parses_vulnerability_for_feature() {
        let sdl = r#"
            name: test-scenario
            description: some-description
            start: 2022-01-20T13:00:00Z
            end: 2022-01-20T23:00:00Z
            features:
                my-less-cool-feature:
                    type: Configuration
                    source:
                        name: cool-config
                        version: 1.0.0
                    vulnerabilities:
                        - vuln-1
                        - vuln-2
            vulnerabilities:
                vuln-1:
                    name: Some vulnerability
                    description: some-description
                    technical: true
                    class: CWE-1341
                vuln-2:
                    name: Some other vulnerability
                    description: some-description
                    technical: false
                    class: CWE-1343
        "#;
        parse_sdl(sdl).unwrap();
    }

    #[test]
    #[should_panic(expected = "Vulnerability \"vuln-4\" not found under Scenario Vulnerabilities")]
    fn missing_vulnerability_for_feature() {
        let sdl = r#"
            name: test-scenario
            description: some-description
            start: 2022-01-20T13:00:00Z
            end: 2022-01-20T23:00:00Z
            features:
                my-less-cool-feature:
                    type: configuration
                    source:
                        name: cool-config
                        version: 1.0.0
                    vulnerabilities:
                        - vuln-4
            vulnerabilities:
                vuln-1:
                    name: Some vulnerability
                    description: some-description
                    technical: true
                    class: CWE-1341
                vuln-2:
                    name: Some other vulnerability
                    description: some-description
                    technical: false
                    class: CWE-1343
            nodes:
                win-10:
                    type: VM
                    resources:
                        ram: 2 gib
                        cpu: 2
                    source: windows10
                    vulnerabilities:
                        - vuln-4
        "#;
        parse_sdl(sdl).unwrap();
    }
}