use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct Cwe(pub u16);
macro_rules! appendix_a {
($($konst:ident = $num:literal, $name:literal;)*) => {
impl Cwe {
$(
#[doc = $name]
pub const $konst: Self = Self($num);
)*
}
pub const APPENDIX_A: &[(Cwe, &str)] = &[$((Cwe($num), $name)),*];
};
}
appendix_a! {
PATH_TRAVERSAL = 22, "Path traversal";
SQL_INJECTION = 89, "SQL injection";
CODE_INJECTION = 94, "Code injection";
OUT_OF_BOUNDS_READ = 125, "Out-of-bounds read";
FORMAT_STRING = 134, "Attacker-controlled format string";
UNICODE_HANDLING = 176, "Improper handling of Unicode";
URL_ENCODING = 177, "Improper handling of URL encoding";
INCORRECT_REGEX = 185, "Incorrect regex";
OFF_BY_ONE = 193, "Off-by-one";
INFO_LEAK = 200, "Info leak";
TIMING_DIFFERENCE = 208, "Observable timing difference";
PARAMETER_HANDLING = 233, "Improper handling of parameters";
UNNECESSARY_PRIVILEGES = 250, "Execution with unnecessary privileges";
PLAINTEXT_PASSWORD = 256, "Plaintext storage of password";
NONCE_REUSE = 323, "Nonce/key-pair reuse";
WEAK_ENCRYPTION_STRENGTH = 326, "Inadequate encryption strength";
BROKEN_CRYPTO_ALGORITHM = 327, "Broken or risky cryptographic algorithm";
WEAK_HASH = 328, "Weak hash";
WEAK_PRNG_SEED = 335, "Weak PRNG seed";
WEAK_PRNG_ALGORITHM = 338, "Weak PRNG algorithm";
PREDICTABLE_RANDOM_IDS = 340, "Predictable random IDs";
BAD_ORIGIN_VALIDATION = 346, "Bad origin validation";
RACE_CONDITION = 362, "Race condition";
USE_AFTER_FREE = 416, "Use after free";
UNRESTRICTED_UPLOAD = 434, "Unrestricted upload of dangerous file type";
UNINITIALIZED_VARIABLE = 457, "Uninitialized variable";
UNSAFE_DESERIALIZATION = 502, "Deserialization of untrusted data";
SENSITIVE_CACHE = 524, "Use of cache containing sensitive information";
OUT_OF_BOUNDS_WRITE = 787, "Out-of-bounds write";
DANGLING_POINTER = 825, "Dangling pointer";
MISSING_MINIMUM_RANGE_CHECK = 839, "Numeric range check without minimum";
TYPE_CONFUSION = 843, "Type confusion";
MISSING_AUTHORIZATION = 862, "Missing authorization";
NOSQL_INJECTION = 943, "Data query injection";
WRONG_COMPARISON_FACTORS = 1025, "Comparison using wrong factors";
WEAK_PASSWORD = 1391, "Weak password";
}
impl Cwe {
pub const UNSPECIFIED: Self = Self(0);
#[must_use]
pub fn name(self) -> Option<&'static str> {
APPENDIX_A
.iter()
.find_map(|&(cwe, name)| (cwe == self).then_some(name))
}
#[must_use]
pub fn in_appendix_a(self) -> bool {
self.name().is_some()
}
}
impl fmt::Display for Cwe {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "CWE-{}", self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum Technique {
MitigationBypass,
MemoryLeak,
MemoryShaping,
ProbabilisticAttack,
OsSpecific,
ArchSpecific,
CryptographicCompute,
Unspecified,
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum Domain {
Web,
ComputerNetworks,
LowLevel,
Cloud,
SpecialSystems,
Cryptography,
DataAndFormats,
MachineLearning,
Other(String),
}
pub const NON_DOMAINS: &[&str] = &[
"Hypervisors",
"Browsers",
"VMs and sandboxed execution environments",
"Cryptocurrency",
"Difficult code analysis",
"Reverse engineering in general",
"Databases, excluding the data storage part",
];