IPI protection
This commit is contained in:
@@ -1851,6 +1851,95 @@ func TestFetchContextWindowFallbackConfig(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterText(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "ASCII printable, spaces, tabs, newlines",
|
||||
input: "Hello World!\t123\nLine 2 ~`@#$%",
|
||||
expected: "Hello World!\t123\nLine 2 ~`@#$%",
|
||||
},
|
||||
{
|
||||
name: "CRLF normalization",
|
||||
input: "line1\r\nline2\r\n",
|
||||
expected: "line1\nline2\n",
|
||||
},
|
||||
{
|
||||
name: "Unicode printable letters, numbers, punctuation",
|
||||
input: "こんにちは世界! Привет мир! 123 αβγ €$¥",
|
||||
expected: "こんにちは世界! Привет мир! 123 αβγ €$¥",
|
||||
},
|
||||
{
|
||||
name: "Control characters stripped",
|
||||
input: "null\x00bell\x07esc\x1b[31mred\x1b[0m\x7fdel",
|
||||
expected: "nullbellesc[31mred[0mdel",
|
||||
},
|
||||
{
|
||||
name: "Zero-width and format characters stripped",
|
||||
input: "hidden\u200Binjection\u200Cand\u200Djoiner\uFEFFbom\u202Ebidi\U000E0001tag",
|
||||
expected: "hiddeninjectionandjoinerbombiditag",
|
||||
},
|
||||
{
|
||||
name: "Space-like unicode characters stripped",
|
||||
input: "nbsp\u00A0space\u2000enquad\u2001emquad\u2009thin\u202Fnarrow\u3000ideo\u1680ogham\u2028lsep\u2029psep",
|
||||
expected: "nbspspaceenquademquadthinnarrowideooghamlseppsep",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := filterText(tc.input)
|
||||
if got != tc.expected {
|
||||
t.Errorf("filterText(%q) = %q, expected %q", tc.input, got, tc.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeMessagesWithInvisibles(t *testing.T) {
|
||||
msgs := []Message{
|
||||
{
|
||||
Role: "assistant",
|
||||
ToolCalls: []ToolCall{
|
||||
{
|
||||
ID: "call_1",
|
||||
Type: "function",
|
||||
Function: struct {
|
||||
Name string `json:"name"`
|
||||
Arguments string `json:"arguments"`
|
||||
}{
|
||||
Name: "shell_exec",
|
||||
Arguments: "{\"command\": \"cat\u200B \u00A0file.txt\"}",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Role: "tool",
|
||||
ToolCallID: "call_1",
|
||||
Content: strp("output\u200B\x00with\u00A0invisible\r\nexit: 0"),
|
||||
},
|
||||
}
|
||||
|
||||
sanitizeMessages(msgs)
|
||||
|
||||
tcArgs := msgs[0].ToolCalls[0].Function.Arguments
|
||||
if strings.Contains(tcArgs, "\u200B") || strings.Contains(tcArgs, "\u00A0") {
|
||||
t.Errorf("Tool call arguments still contain invisible characters: %q", tcArgs)
|
||||
}
|
||||
|
||||
toolContent := *msgs[1].Content
|
||||
if strings.Contains(toolContent, "\u200B") || strings.Contains(toolContent, "\x00") || strings.Contains(toolContent, "\u00A0") || strings.Contains(toolContent, "\r") {
|
||||
t.Errorf("Tool content still contains invisible characters: %q", toolContent)
|
||||
}
|
||||
if !strings.Contains(toolContent, "outputwithinvisible\nexit: 0") {
|
||||
t.Errorf("Tool content unexpected: %q", toolContent)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user