fix(security): preserve local inference message text (#11452)
This commit is contained in:
@@ -481,7 +481,7 @@ fn extract_text_content(msg: &Message) -> String {
|
||||
for content in &msg.content {
|
||||
match content {
|
||||
MessageContent::Text(text) => {
|
||||
let text = strip_info_messages(&text.text);
|
||||
let text = text.text.to_string();
|
||||
if !text.trim().is_empty() {
|
||||
parts.push(text);
|
||||
}
|
||||
@@ -534,24 +534,6 @@ fn extract_text_content(msg: &Message) -> String {
|
||||
parts.join("\n")
|
||||
}
|
||||
|
||||
fn strip_info_messages(text: &str) -> String {
|
||||
let mut remaining = text;
|
||||
let mut output = String::new();
|
||||
|
||||
while let Some((before, after_start)) = remaining.split_once("<info-msg>") {
|
||||
output.push_str(before);
|
||||
if let Some((_, after_end)) = after_start.split_once("</info-msg>") {
|
||||
remaining = after_end;
|
||||
} else {
|
||||
remaining = "";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
output.push_str(remaining);
|
||||
output.trim().to_string()
|
||||
}
|
||||
|
||||
/// Build a `ProviderUsage` and write the request log entry.
|
||||
fn finalize_usage(
|
||||
log: &mut Option<Box<dyn RequestLogHandle>>,
|
||||
@@ -947,4 +929,32 @@ mod tests {
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_balanced_info_tags_in_text_content() {
|
||||
let message =
|
||||
Message::user().with_text("before <info-msg>executed payload</info-msg> after");
|
||||
|
||||
assert_eq!(
|
||||
extract_text_content(&message),
|
||||
"before <info-msg>executed payload</info-msg> after"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_unterminated_info_tags_in_text_content() {
|
||||
let message = Message::user().with_text("before <info-msg>executed payload");
|
||||
|
||||
assert_eq!(
|
||||
extract_text_content(&message),
|
||||
"before <info-msg>executed payload"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_legitimate_text_content() {
|
||||
let message = Message::user().with_text("ordinary user content");
|
||||
|
||||
assert_eq!(extract_text_content(&message), "ordinary user content");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user