Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/goose/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ tempfile = "3.15.0"
dashmap = "6.1"
ahash = "0.8"
tokio-util = "0.7.15"
unicode-normalization = "0.1"

# Vector database for tool selection
lancedb = "0.13"
Expand Down
49 changes: 48 additions & 1 deletion crates/goose/src/conversation/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use serde::{Deserialize, 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;
Expand Down Expand Up @@ -409,9 +410,27 @@ impl Message {
self
}

fn sanitize_unicode_tags(text: &str) -> String {
let normalized: String = text.nfc().collect();

// Remove Unicode Tags Block characters only
normalized
.chars()
.filter(|&c| !matches!(c, '\u{E0000}'..='\u{E007F}'))
.collect()
}

/// Add text content to the message
pub fn with_text<S: Into<String>>(self, text: S) -> Self {
self.with_content(MessageContent::text(text))
let raw_text = text.into();
let sanitized_text = Self::sanitize_unicode_tags(&raw_text);

self.with_content(MessageContent::Text(
RawTextContent {
text: sanitized_text,
}
.no_annotation(),
))
}

/// Add image content to the message
Expand Down Expand Up @@ -565,6 +584,34 @@ mod tests {
};
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 = Message::sanitize_unicode_tags(malicious);
assert_eq!(cleaned, "Helloworld");
}

#[test]
fn test_no_sanitize_unicode_tags() {
let clean_text = "Hello world 世界 🌍";
let cleaned = Message::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"
let message = Message::user().with_text(malicious);
assert_eq!(message.as_concat_text(), "Helloworld");
}

#[test]
fn test_no_sanitize_with_text() {
let clean_text = "Hello world 世界 🌍";
let message = Message::user().with_text(clean_text);
assert_eq!(message.as_concat_text(), clean_text);
}

#[test]
fn test_message_serialization() {
let message = Message::assistant()
Expand Down
Loading