1use core::fmt;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15#[cfg_attr(feature = "serde", serde(transparent))]
16pub struct Cwe(pub u16);
17
18macro_rules! appendix_a {
19 ($($konst:ident = $num:literal, $name:literal;)*) => {
20 impl Cwe {
21 $(
22 #[doc = $name]
23 pub const $konst: Self = Self($num);
24 )*
25 }
26
27 pub const APPENDIX_A: &[(Cwe, &str)] = &[$((Cwe($num), $name)),*];
29 };
30}
31
32appendix_a! {
33 PATH_TRAVERSAL = 22, "Path traversal";
34 SQL_INJECTION = 89, "SQL injection";
35 CODE_INJECTION = 94, "Code injection";
36 OUT_OF_BOUNDS_READ = 125, "Out-of-bounds read";
37 FORMAT_STRING = 134, "Attacker-controlled format string";
38 UNICODE_HANDLING = 176, "Improper handling of Unicode";
39 URL_ENCODING = 177, "Improper handling of URL encoding";
40 INCORRECT_REGEX = 185, "Incorrect regex";
41 OFF_BY_ONE = 193, "Off-by-one";
42 INFO_LEAK = 200, "Info leak";
43 TIMING_DIFFERENCE = 208, "Observable timing difference";
44 PARAMETER_HANDLING = 233, "Improper handling of parameters";
45 UNNECESSARY_PRIVILEGES = 250, "Execution with unnecessary privileges";
46 PLAINTEXT_PASSWORD = 256, "Plaintext storage of password";
47 NONCE_REUSE = 323, "Nonce/key-pair reuse";
48 WEAK_ENCRYPTION_STRENGTH = 326, "Inadequate encryption strength";
49 BROKEN_CRYPTO_ALGORITHM = 327, "Broken or risky cryptographic algorithm";
50 WEAK_HASH = 328, "Weak hash";
51 WEAK_PRNG_SEED = 335, "Weak PRNG seed";
52 WEAK_PRNG_ALGORITHM = 338, "Weak PRNG algorithm";
53 PREDICTABLE_RANDOM_IDS = 340, "Predictable random IDs";
54 BAD_ORIGIN_VALIDATION = 346, "Bad origin validation";
55 RACE_CONDITION = 362, "Race condition";
56 USE_AFTER_FREE = 416, "Use after free";
57 UNRESTRICTED_UPLOAD = 434, "Unrestricted upload of dangerous file type";
58 UNINITIALIZED_VARIABLE = 457, "Uninitialized variable";
59 UNSAFE_DESERIALIZATION = 502, "Deserialization of untrusted data";
60 SENSITIVE_CACHE = 524, "Use of cache containing sensitive information";
61 OUT_OF_BOUNDS_WRITE = 787, "Out-of-bounds write";
62 DANGLING_POINTER = 825, "Dangling pointer";
63 MISSING_MINIMUM_RANGE_CHECK = 839, "Numeric range check without minimum";
64 TYPE_CONFUSION = 843, "Type confusion";
65 MISSING_AUTHORIZATION = 862, "Missing authorization";
66 NOSQL_INJECTION = 943, "Data query injection";
67 WRONG_COMPARISON_FACTORS = 1025, "Comparison using wrong factors";
68 WEAK_PASSWORD = 1391, "Weak password";
69}
70
71impl Cwe {
72 pub const UNSPECIFIED: Self = Self(0);
78
79 #[must_use]
81 pub fn name(self) -> Option<&'static str> {
82 APPENDIX_A
83 .iter()
84 .find_map(|&(cwe, name)| (cwe == self).then_some(name))
85 }
86
87 #[must_use]
89 pub fn in_appendix_a(self) -> bool {
90 self.name().is_some()
91 }
92}
93
94impl fmt::Display for Cwe {
95 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96 write!(f, "CWE-{}", self.0)
97 }
98}
99
100#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
105#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
106#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
107pub enum Technique {
108 MitigationBypass,
110 MemoryLeak,
112 MemoryShaping,
114 ProbabilisticAttack,
116 OsSpecific,
118 ArchSpecific,
120 CryptographicCompute,
122 Unspecified,
124 Other(String),
126}
127
128#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
134#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
135#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
136pub enum Domain {
137 Web,
139 ComputerNetworks,
141 LowLevel,
144 Cloud,
146 SpecialSystems,
148 Cryptography,
150 DataAndFormats,
152 MachineLearning,
154 Other(String),
156}
157
158pub const NON_DOMAINS: &[&str] = &[
163 "Hypervisors",
164 "Browsers",
165 "VMs and sandboxed execution environments",
166 "Cryptocurrency",
167 "Difficult code analysis",
168 "Reverse engineering in general",
169 "Databases, excluding the data storage part",
170];