refactor(dynamic): enhance framework bindings with SSA receiver type checks, add tests for Laravel, Symfony, Rabbit, Kafka, and Pub/Sub

This commit is contained in:
elipeter 2026-05-23 14:32:48 -05:00
parent aaa1fd7ede
commit 17fa611b63
17 changed files with 1255 additions and 167 deletions

View file

@ -4,6 +4,7 @@
use crate::dynamic::framework::{FrameworkAdapter, FrameworkBinding};
use crate::evidence::EntryKind;
use crate::summary::FuncSummary;
use crate::summary::ssa_summary::SsaFuncSummary;
use crate::symbol::Lang;
pub struct RabbitPythonAdapter;
@ -56,32 +57,64 @@ impl FrameworkAdapter for RabbitPythonAdapter {
fn detect(
&self,
summary: &FuncSummary,
_ast: tree_sitter::Node<'_>,
ast: tree_sitter::Node<'_>,
file_bytes: &[u8],
) -> Option<FrameworkBinding> {
let matches_call = super::any_callee_matches(summary, callee_is_rabbit);
let matches_source = source_imports_rabbit(file_bytes);
if matches_call || matches_source {
Some(FrameworkBinding {
adapter: ADAPTER_NAME.to_owned(),
kind: EntryKind::MessageHandler {
queue: extract_queue(file_bytes),
message_schema: None,
},
route: None,
request_params: Vec::new(),
response_writer: None,
middleware: Vec::new(),
})
} else {
None
}
detect_rabbit_python(summary, None, ast, file_bytes)
}
fn detect_with_context(
&self,
summary: &FuncSummary,
ssa_summary: Option<&SsaFuncSummary>,
ast: tree_sitter::Node<'_>,
file_bytes: &[u8],
) -> Option<FrameworkBinding> {
detect_rabbit_python(summary, ssa_summary, ast, file_bytes)
}
}
fn detect_rabbit_python(
summary: &FuncSummary,
ssa_summary: Option<&SsaFuncSummary>,
_ast: tree_sitter::Node<'_>,
file_bytes: &[u8],
) -> Option<FrameworkBinding> {
let matches_call = super::any_callee_matches(summary, callee_is_rabbit);
let matches_source = source_imports_rabbit(file_bytes);
if !(matches_call || matches_source) {
return None;
}
if !super::typed_receiver_facts_allow(
summary,
ssa_summary,
callee_is_rabbit,
typed_container_allows_rabbit,
) {
return None;
}
Some(FrameworkBinding {
adapter: ADAPTER_NAME.to_owned(),
kind: EntryKind::MessageHandler {
queue: extract_queue(file_bytes),
message_schema: None,
},
route: None,
request_params: Vec::new(),
response_writer: None,
middleware: Vec::new(),
})
}
fn typed_container_allows_rabbit(container: &str) -> bool {
let lc = container.to_ascii_lowercase();
lc.contains("rabbit") || lc.contains("pika") || lc.contains("amqp") || lc.contains("channel")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::summary::CalleeSite;
fn parse_python(src: &[u8]) -> tree_sitter::Tree {
let mut parser = tree_sitter::Parser::new();
@ -108,4 +141,53 @@ mod tests {
assert_eq!(queue, "work");
}
}
#[test]
fn ssa_receiver_type_rejects_non_rabbit_process_collision() {
let src: &[u8] = b"import pika\n\
def on_message(ch, method, properties, body):\n worker.process(body)\n";
let tree = parse_python(src);
let mut summary = FuncSummary {
name: "on_message".into(),
..Default::default()
};
summary.callees.push(CalleeSite {
name: "worker.process".to_owned(),
receiver: Some("worker".to_owned()),
ordinal: 0,
..Default::default()
});
let mut ssa = SsaFuncSummary::default();
ssa.typed_call_receivers.push((0, "Worker".to_owned()));
assert!(
RabbitPythonAdapter
.detect_with_context(&summary, Some(&ssa), tree.root_node(), src)
.is_none()
);
}
#[test]
fn ssa_receiver_type_keeps_rabbit_channel() {
let src: &[u8] = b"import pika\n\
def on_message(ch, method, properties, body):\n channel.basic_consume(queue='work')\n";
let tree = parse_python(src);
let mut summary = FuncSummary {
name: "on_message".into(),
..Default::default()
};
summary.callees.push(CalleeSite {
name: "channel.basic_consume".to_owned(),
receiver: Some("channel".to_owned()),
ordinal: 0,
..Default::default()
});
let mut ssa = SsaFuncSummary::default();
ssa.typed_call_receivers
.push((0, "BlockingChannel".to_owned()));
assert!(
RabbitPythonAdapter
.detect_with_context(&summary, Some(&ssa), tree.root_node(), src)
.is_some()
);
}
}