Desktop alerts when suspicious unicode characters found in Recipe (#4080)

This commit is contained in:
Amed Rodriguez
2025-08-19 14:05:46 -07:00
committed by GitHub
parent 33bf5a44ed
commit 4f4e8ace33
11 changed files with 256 additions and 15 deletions
+22 -5
View File
@@ -1,17 +1,23 @@
use tokio_util::sync::CancellationToken;
use unicode_normalization::UnicodeNormalization;
/// Check if a character is in the Unicode Tags Block range (U+E0000-U+E007F)
/// These characters are invisible and can be used for steganographic attacks
fn is_in_unicode_tag_range(c: char) -> bool {
matches!(c, '\u{E0000}'..='\u{E007F}')
}
pub fn contains_unicode_tags(text: &str) -> bool {
text.chars().any(is_in_unicode_tag_range)
}
/// Sanitize Unicode Tags Block characters from text
/// Used to prevent Unicode-based prompt injection attacks
///
/// This function removes invisible Unicode Tags Block characters (U+E0000-U+E007F)
/// that can be used for steganographic attacks while preserving legitimate Unicode.
pub fn sanitize_unicode_tags(text: &str) -> String {
let normalized: String = text.nfc().collect();
normalized
.chars()
.filter(|&c| !matches!(c, '\u{E0000}'..='\u{E007F}'))
.filter(|&c| !is_in_unicode_tag_range(c))
.collect()
}
@@ -45,6 +51,17 @@ pub fn is_token_cancelled(cancellation_token: &Option<CancellationToken>) -> boo
mod tests {
use super::*;
#[test]
fn test_contains_unicode_tags() {
// Test detection of Unicode Tags Block characters
assert!(contains_unicode_tags("Hello\u{E0041}world"));
assert!(contains_unicode_tags("\u{E0000}"));
assert!(contains_unicode_tags("\u{E007F}"));
assert!(!contains_unicode_tags("Hello world"));
assert!(!contains_unicode_tags("Hello 世界 🌍"));
assert!(!contains_unicode_tags(""));
}
#[test]
fn test_sanitize_unicode_tags() {
// Test that Unicode Tags Block characters are removed