-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Fix: Small update in how ML-based prompt injection determines final result #6439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
fece726
836884b
ce9e44f
7143412
5a703ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| pub explanation: String, | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| struct DetailedScanResult { | ||
| confidence: f32, | ||
| pattern_matches: Vec<PatternMatch>, | ||
|
|
@@ -106,20 +107,23 @@ | |
| self.scan_conversation(messages) | ||
| ); | ||
|
|
||
| let highest_confidence_result = | ||
| self.select_highest_confidence_result(tool_result?, context_result?); | ||
| let tool_result = tool_result?; | ||
| let context_result = context_result?; | ||
| let threshold = self.get_threshold_from_config(); | ||
|
|
||
| let final_result = | ||
| self.select_result_with_context_awareness(tool_result, context_result, threshold); | ||
|
|
||
| tracing::info!( | ||
| "✅ Security analysis complete: confidence={:.3}, malicious={}", | ||
| highest_confidence_result.confidence, | ||
| highest_confidence_result.confidence >= threshold | ||
| "Security analysis complete: confidence={:.3}, malicious={}", | ||
| final_result.confidence, | ||
| final_result.confidence >= threshold | ||
| ); | ||
|
|
||
| Ok(ScanResult { | ||
| is_malicious: highest_confidence_result.confidence >= threshold, | ||
| confidence: highest_confidence_result.confidence, | ||
| explanation: self.build_explanation(&highest_confidence_result, threshold), | ||
| is_malicious: final_result.confidence >= threshold, | ||
| confidence: final_result.confidence, | ||
| explanation: self.build_explanation(&final_result, threshold), | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -169,12 +173,39 @@ | |
| }) | ||
| } | ||
|
|
||
| fn select_highest_confidence_result( | ||
| fn select_result_with_context_awareness( | ||
| &self, | ||
| tool_result: DetailedScanResult, | ||
| context_result: DetailedScanResult, | ||
| threshold: f32, | ||
| ) -> DetailedScanResult { | ||
| if tool_result.confidence >= context_result.confidence { | ||
| let context_is_safe = context_result | ||
| .ml_confidence | ||
| .is_some_and(|conf| conf < threshold); | ||
|
|
||
| let tool_has_only_non_critical = !tool_result.pattern_matches.is_empty() | ||
| && tool_result | ||
| .pattern_matches | ||
| .iter() | ||
| .all(|m| m.threat.risk_level != crate::security::patterns::RiskLevel::Critical); | ||
|
|
||
| if context_is_safe && tool_has_only_non_critical { | ||
| tracing::info!( | ||
| "Suppressing non-critical pattern match due to safe context evaluation" | ||
| ); | ||
| tracing::debug!( | ||
| context_ml_confidence = ?context_result.ml_confidence, | ||
| pattern_count = tool_result.pattern_matches.len(), | ||
| max_pattern_risk = ?tool_result.pattern_matches.first().map(|m| &m.threat.risk_level), | ||
| "Suppression conditions met: safe context + non-critical patterns" | ||
| ); | ||
|
|
||
| DetailedScanResult { | ||
| confidence: 0.0, | ||
| pattern_matches: Vec::new(), | ||
| ml_confidence: context_result.ml_confidence, | ||
| } | ||
|
Comment on lines
+208
to
+213
|
||
| } else if tool_result.confidence >= context_result.confidence { | ||
|
Comment on lines
+208
to
+214
|
||
| tool_result | ||
| } else { | ||
| context_result | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you want to info & debug log here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, this was helpful for local testing but can probably get rid of these for now, ty!