Sanitize Tags Unicode Block at prompt level (#4047)

This commit is contained in:
Amed Rodriguez
2025-08-15 09:12:05 -07:00
committed by GitHub
parent 85963689ce
commit 6e022a974a
3 changed files with 166 additions and 30 deletions
+1 -25
View File
@@ -8,20 +8,10 @@ use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value;
use std::collections::HashSet;
use std::fmt;
use unicode_normalization::UnicodeNormalization;
use utoipa::ToSchema;
use crate::conversation::tool_result_serde;
/// Sanitize Unicode Tags Block characters from text
fn sanitize_unicode_tags(text: &str) -> String {
let normalized: String = text.nfc().collect();
normalized
.chars()
.filter(|&c| !matches!(c, '\u{E0000}'..='\u{E007F}'))
.collect()
}
use crate::utils::sanitize_unicode_tags;
/// Custom deserializer for MessageContent that sanitizes Unicode Tags in text content
fn deserialize_sanitized_content<'de, D>(deserializer: D) -> Result<Vec<MessageContent>, D::Error>
@@ -611,20 +601,6 @@ mod tests {
use rmcp::model::{ErrorCode, ErrorData};
use serde_json::{json, Value};
#[test]
fn test_sanitize_unicode_tags() {
let malicious = "Hello\u{E0041}\u{E0042}\u{E0043}world"; // Invisible "ABC"
let cleaned = super::sanitize_unicode_tags(malicious);
assert_eq!(cleaned, "Helloworld");
}
#[test]
fn test_no_sanitize_unicode_tags() {
let clean_text = "Hello world 世界 🌍";
let cleaned = super::sanitize_unicode_tags(clean_text);
assert_eq!(cleaned, clean_text);
}
#[test]
fn test_sanitize_with_text() {
let malicious = "Hello\u{E0041}\u{E0042}\u{E0043}world"; // Invisible "ABC"