Building 70 Projects ranging from beginner to advanced so anyone can β learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company π
Go aicertificationcloud-engineeringcybercybersecuritycybersecurity-analystcybersecurity-certificationcybersecurity-certificationscybersecurity-educationcybersecurity-jobscybersecurity-portfoliocybersecurity-projectscybersecurity-toolsgrchackinglearningpentestingproject-repositoryprojetcspython
fix(lisdex): B54 + B55 - brotli's include root, and half the "lengths" were not lengths
CarterPerez-dev1465cb978134
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/headers.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/headers.rsindex d74d7d60..eee684b8 100644--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/headers.rs+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/headers.rs@@ -3,9 +3,12 @@use std::path::{Path, PathBuf};+use crate::extract::MIN_PROJECT_STEM;+pub const HEADER_EXTENSIONS: &[&str] = &["h", "hpp", "hh", "hxx"];pub const INCLUDE_DIR: &str = "include";pub const MAX_DEPTH: usize = 12;+pub const NESTED_INCLUDE_DEPTH: usize = 3;pub const EXCLUDED_DIRS: &[&str] = &["test",@@ -249,6 +252,55 @@ pub fn generated_headers(source: &Path) -> Vec<PathBuf> {found}+fn holds_a_directory_named_after(dir: &Path, project: &str) -> bool {+ // `kin` is a bare prefix test, and this decision redirects the WHOLE analysis to a new+ // header root, so a short stem here is the B61 hazard with a bigger blast radius: zlib+ // cloned as `libz` tokenises to `z`, and every `include/z*/` in the tree would answer to it.+ if project.len() < MIN_PROJECT_STEM {+ return false;+ }+ let Ok(entries) = std::fs::read_dir(dir) else {+ return false;+ };+ entries.flatten().any(|entry| {+ entry.file_type().is_ok_and(|kind| kind.is_dir())+ && entry+ .file_name()+ .to_str()+ .is_some_and(|name| kin(&project_token(name), project))+ })+}++// brotli keeps its public API in `c/include/brotli/`, so the `<source>/include` probe misses+// it, the tree walk parses `c/common/constants.h` without `-I c/include`, and every header+// is refused: brotli was the one clone of eight that could not be analysed at all. The+// project-named subdirectory is what makes this safe to search for - openjpeg ships+// `thirdparty/include` and libarchive `contrib/android/include`, and both hold nothing named+// after their project, so neither is mistaken for a public API root.+fn nested_include_root(dir: &Path, project: &str, depth: usize) -> Option<PathBuf> {+ if depth > NESTED_INCLUDE_DEPTH {+ return None;+ }+ let mut found = Vec::new();+ for entry in std::fs::read_dir(dir).ok()?.flatten() {+ let path = entry.path();+ if !path.is_dir() {+ continue;+ }+ let name = entry.file_name().to_string_lossy().to_lowercase();+ if is_excluded(&name) || is_vendored_tree(&path, project) {+ continue;+ }+ if name == INCLUDE_DIR && holds_a_directory_named_after(&path, project) {+ found.push(path);+ } else if let Some(nested) = nested_include_root(&path, project, depth + 1) {+ found.push(nested);+ }+ }+ found.sort();+ found.into_iter().next()+}+fn gathered(root: &Path, project: &str) -> Result<Vec<PathBuf>, HeaderError> {let mut found = Vec::new();collect(root, project, 0, &mut found)?;@@ -297,6 +349,17 @@ pub fn discover(source: &Path, install: Option<&Path>) -> Result<HeaderSet, Head}}+ if let Some(nested) = nested_include_root(source, &project, 0) {+ let headers = gathered(&nested, &project)?;+ if !headers.is_empty() {+ return Ok(HeaderSet {+ root: nested,+ origin: HeaderOrigin::IncludeDir,+ headers,+ });+ }+ }+let mut headers = collect_shallow(source)?;headers.sort();if !headers.is_empty() {@@ -610,6 +673,62 @@ mod tests {assert!(matches!(error, HeaderError::NotADirectory(_)), "{error:?}");}+ #[test]+ fn an_include_root_nested_one_level_down_is_still_the_public_api() {+ let root = fixture(+ "nestedinclude",+ &[+ "c/include/brotli/decode.h",+ "c/include/brotli/port.h",+ "c/common/constants.h",+ ],+ );+ let renamed = named_as_project(&root, "brotli");+ let set = discover(&renamed, None).unwrap();+ assert_eq!(set.origin, HeaderOrigin::IncludeDir);+ assert_eq!(+ set.root,+ renamed.join("c/include"),+ "brotli's public API lives at c/include, so the tree walk parsed \+ c/common/constants.h without -I c/include and refused every header"+ );+ assert_eq!(names(&set), vec!["decode.h", "port.h"]);+ }++ // The isolating control. A rule that took ANY nested include directory would pass the+ // test above and still be wrong: openjpeg ships thirdparty/include and libarchive ships+ // contrib/android/include. The project-named subdirectory is the whole discriminator.+ #[test]+ fn a_nested_include_holding_nothing_named_after_the_project_is_not_the_api() {+ let root = fixture(+ "foreignnested",+ &["src/include/lcms2/lcms2.h", "src/openjpeg/openjpeg.h"],+ );+ let renamed = named_as_project(&root, "openjpeg");+ let set = discover(&renamed, None).unwrap();+ assert_eq!(+ set.origin,+ HeaderOrigin::Tree,+ "src/include holds lcms2, not openjpeg, so it is a dependency's headers"+ );+ }++ #[test]+ fn a_project_stem_too_short_to_be_evidence_claims_no_nested_include() {+ let root = fixture(+ "shortstem",+ &["src/include/zoo/zoo.h", "src/zlib/deflate.h"],+ );+ let renamed = named_as_project(&root, "libz");+ let set = discover(&renamed, None).unwrap();+ assert_ne!(+ set.origin,+ HeaderOrigin::IncludeDir,+ "libz tokenises to `z`, and a bare prefix test would hand the whole \+ analysis to any include/z*/ in the tree"+ );+ }+#[test]fn an_install_tree_without_headers_falls_back_to_the_source() {let root = fixture("installempty", &["install/lib/libpng.a", "include/png.h"]);diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/signature.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/signature.rsindex c40ee9a9..7d10648b 100644--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/signature.rs+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/signature.rs@@ -24,11 +24,36 @@ pub const LENGTH_NAMES: &[&str] = &["nbytes","nbyte","num_bytes",+ "bytes","count",+ "capacity",+ "avail",+ "nmemb","sz","n",];+// An extent is a count of bytes. These typedefs name a different quantity, so one standing+// beside a buffer is never that buffer's length. libarchive declares+// `time_t archive_parse_date(time_t now, const char *datestr)`, and grading `now` as an+// extent satisfied the char-buffer-needs-a-length guard, so the planner handed a date+// parser to a user who asked to fuzz an archive reader.+pub const NON_EXTENT_TYPES: &[&str] = &[+ "time_t",+ "clock_t",+ "suseconds_t",+ "useconds_t",+ "pid_t",+ "uid_t",+ "gid_t",+ "dev_t",+ "ino_t",+ "mode_t",+ "nlink_t",+ "bool",+ "_Bool",+];+pub const DIMENSION_NAMES: &[&str] = &["width", "height", "stride", "channels", "depth", "pitch", "rows", "cols", "bpp",];@@ -161,6 +186,17 @@ fn name_has_token(name: Option<&String>, candidates: &[&str]) -> bool {.any(|candidate| tokens.iter().any(|token| token == candidate))}+fn names_a_non_extent_type(spelling: &str) -> bool {+ spelling+ .split(|character: char| !character.is_ascii_alphanumeric() && character != '_')+ .any(|token| NON_EXTENT_TYPES.contains(&token))+}++// Any integral standing beside a buffer used to be taken as its extent. Measured across+// five upstream libraries, half the parameters that won the role were not extents at all -+// `time_t now`, `int _flag`, `int align`, `int rgb_step`, `int fd`. Every genuine one was+// named for what it is, so a NAMED integral now has to say so; an unnamed one still+// qualifies on type alone, because libtiff and libarchive declare parameters without names.fn is_length_like(param: &Param) -> bool {let carries_a_count = matches!(param.shape, Shape::Integral)|| matches!(@@ -170,7 +206,25 @@ fn is_length_like(param: &Param) -> bool {..});- carries_a_count && !name_matches(param.name.as_ref(), DIMENSION_NAMES)+ if !carries_a_count || names_a_non_extent_type(¶m.spelling) {+ return false;+ }+ if name_matches(param.name.as_ref(), DIMENSION_NAMES) {+ return false;+ }+ param.name.is_none() || name_matches(param.name.as_ref(), LENGTH_NAMES)+}++fn shared_leading_run(buffer: Option<&String>, extent: Option<&String>) -> usize {+ let (Some(buffer), Some(extent)) = (buffer, extent) else {+ return 0;+ };+ let extent = tokenize(extent);+ tokenize(buffer)+ .into_iter()+ .zip(extent)+ .take_while(|(one, other)| one == other)+ .count()}fn carries_extent(role: ParamRole, param: &Param) -> bool {@@ -313,7 +367,21 @@ fn grade_signature(params: &[Param]) -> Analysis {let leader = index.checked_sub(1).filter(|prior| carries_extent(roles[*prior], ¶ms[*prior]));- if let Some(extent) = follower.or(leader)+ // When a buffer is flanked on both sides, the follower used to win unconditionally.+ // BrotliDecoderDecompress(size_t encoded_size, const uint8_t encoded_buffer[],+ // size_t *decoded_size, uint8_t decoded_buffer[]) gave `encoded_buffer` the extent of+ // the OUTPUT buffer and left its own beside it as a config, so the planner refused+ // the canonical decode entry of the library. The name says which one owns it.+ let flanked = match (follower, leader) {+ (Some(after), Some(before)) => {+ let buffer = params[index].name.as_ref();+ let leader_owns_it = shared_leading_run(buffer, params[before].name.as_ref())+ > shared_leading_run(buffer, params[after].name.as_ref());+ Some(if leader_owns_it { before } else { after })+ }+ (after, before) => after.or(before),+ };+ if let Some(extent) = flanked&& quality >= best{if let Some(previous) = paired@@ -649,6 +717,37 @@ mod tests {);}+ #[test]+ fn a_buffer_flanked_by_two_extents_takes_the_one_that_shares_its_name() {+ let params = [+ integral("encoded_size"),+ pointer("encoded_buffer", Pointee::UnsignedChar, true),+ integral_pointer("decoded_size"),+ pointer("decoded_buffer", Pointee::UnsignedChar, false),+ ];+ let analysis = analyze(¶ms);+ assert_eq!(+ analysis.roles[0],+ ParamRole::Length,+ "encoded_buffer's extent is encoded_size, not the output's decoded_size"+ );+ assert_ne!(analysis.roles[2], ParamRole::Length);+ }++ // The control: with no name affinity to go on, the follower still wins. zlib's+ // uncompress(dest, destLen, source, sourceLen) is flanked on both sides too.+ #[test]+ fn a_flanked_buffer_with_no_affinity_either_way_still_takes_its_follower() {+ let params = [+ integral_pointer("outlen"),+ pointer("in", Pointee::UnsignedChar, true),+ integral("n"),+ ];+ let analysis = analyze(¶ms);+ assert_eq!(analysis.roles[2], ParamRole::Length);+ assert_ne!(analysis.roles[0], ParamRole::Length);+ }+#[test]fn a_pointer_to_a_length_counts_as_the_extent() {let params = [@@ -810,6 +909,82 @@ mod tests {]}+ fn typed_integral(name: Option<&str>, spelling: &str) -> Param {+ Param {+ name: name.map(str::to_owned),+ spelling: spelling.to_owned(),+ shape: Shape::Integral,+ }+ }++ #[test]+ fn a_time_is_not_the_extent_of_the_string_beside_it() {+ let params = [+ typed_integral(Some("now"), "time_t"),+ pointer("datestr", Pointee::Char, true),+ ];+ let analysis = analyze(¶ms);+ assert_ne!(+ analysis.roles[0],+ ParamRole::Length,+ "archive_parse_date(time_t now, const char *datestr) planned as a decoder"+ );+ }++ // Isolates the type rule from the name rule: `count` passes LENGTH_NAMES, so only the+ // spelling can reject this one. Without it the name rule alone would alibi the type rule.+ #[test]+ fn a_time_typedef_is_not_an_extent_even_when_it_is_named_like_one() {+ let params = [+ typed_integral(Some("count"), "time_t"),+ pointer("datestr", Pointee::Char, true),+ ];+ let analysis = analyze(¶ms);+ assert_ne!(analysis.roles[0], ParamRole::Length);+ }++ // The other half of the pair: `int` is a perfectly good extent type, so only the name+ // can reject libarchive's archive_match_include_date(struct archive *, int _flag,+ // const char *_datestr), which ranked 4th on the strength of that bogus pairing.+ #[test]+ fn an_integral_not_named_like_an_extent_never_becomes_one() {+ let params = [+ pointer("a", Pointee::Record("archive".to_owned()), false),+ typed_integral(Some("_flag"), "int"),+ pointer("_datestr", Pointee::Char, true),+ ];+ let analysis = analyze(¶ms);+ assert_ne!(+ analysis.roles[1],+ ParamRole::Length,+ "a flag is not a length"+ );+ }++ // The control that keeps the name rule honest: demanding a length-like NAME must not+ // cost the libraries that ship no parameter names at all.+ #[test]+ fn an_unnamed_integral_beside_a_buffer_is_still_its_extent() {+ let params = [+ Param {+ name: None,+ spelling: "const void *".to_owned(),+ shape: Shape::Pointer {+ pointee: Pointee::Void,+ is_const: true,+ },+ },+ typed_integral(None, "size_t"),+ ];+ let analysis = analyze(¶ms);+ assert_eq!(+ analysis.roles[1],+ ParamRole::Length,+ "libtiff and libarchive declare their parameters unnamed"+ );+ assert_eq!(analysis.fuzzability, Fuzzability::CastBuffer);+ }+#[test]fn the_in_memory_sibling_of_a_path_entry_is_still_a_buffer() {let params = [