//! PHP [`super::super::FrameworkAdapter`] matching weak-crypto sink //! constructions (`md5()` / `sha1()` for message digests, //! `mt_rand()` / `rand()` for key material, `mcrypt_encrypt()` and //! `mcrypt_create_iv()` legacy primitives, `hash('md5'|'sha1', …)`). //! //! Phase 11 (Track L.9). Fires when the function body invokes one //! of the canonical PHP weak-crypto entry points and the surrounding //! source is plausibly a PHP script (starts with ` bool { let last = name .rsplit_once("::") .map(|(_, s)| s) .unwrap_or(name) .rsplit_once('\\') .map(|(_, s)| s) .unwrap_or(name); let last = last.rsplit_once('.').map(|(_, s)| s).unwrap_or(last); matches!( last, "md5" | "sha1" | "md5_file" | "sha1_file" | "mt_rand" | "rand" | "mt_srand" | "srand" | "crc32" | "mcrypt_create_iv" | "mcrypt_encrypt" | "mcrypt_decrypt" | "uniqid" ) } fn source_is_php_script(file_bytes: &[u8]) -> bool { const NEEDLES: &[&[u8]] = &[b" bool { const NEEDLES: &[&[u8]] = &[ b"random_bytes(", b"random_int(", b"openssl_random_pseudo_bytes(", b"sodium_crypto_", b"hash('sha256'", b"hash(\"sha256\"", b"hash('sha384'", b"hash(\"sha384\"", b"hash('sha512'", b"hash(\"sha512\"", b"hash('sha3-256'", b"hash(\"sha3-256\"", b"'aes-256-gcm'", b"\"aes-256-gcm\"", b"'chacha20-poly1305'", b"\"chacha20-poly1305\"", b"password_hash(", ]; NEEDLES .iter() .any(|n| file_bytes.windows(n.len()).any(|w| w == *n)) } impl FrameworkAdapter for CryptoPhpAdapter { fn name(&self) -> &'static str { ADAPTER_NAME } fn lang(&self) -> Lang { Lang::Php } fn detect( &self, summary: &FuncSummary, _ast: tree_sitter::Node<'_>, file_bytes: &[u8], ) -> Option { if source_routed_through_strong_path(file_bytes) { return None; } let matches_call = super::any_callee_matches(summary, callee_is_weak_crypto); let matches_source = source_is_php_script(file_bytes); if matches_call && matches_source { Some(FrameworkBinding { adapter: ADAPTER_NAME.to_owned(), kind: EntryKind::Function, route: None, request_params: Vec::new(), response_writer: None, middleware: Vec::new(), }) } else { None } } } #[cfg(test)] mod tests { use super::*; fn parse_php(src: &[u8]) -> tree_sitter::Tree { let mut parser = tree_sitter::Parser::new(); let lang = tree_sitter::Language::from(tree_sitter_php::LANGUAGE_PHP); parser.set_language(&lang).unwrap(); parser.parse(src, None).unwrap() } #[test] fn fires_on_md5() { let src: &[u8] = b"