Skip to content

Commit 5049e5a

Browse files
committed
fix(chat): bound persisted argument decoding
Co-authored-by: Copilot App <[email protected]> Copilot-Session: fbba7380-93e7-44a4-bd0b-d0ac1ebba543
1 parent 472a6e6 commit 5049e5a

3 files changed

Lines changed: 99 additions & 2 deletions

File tree

‎src/OpenClaw.Tray.WinUI/Chat/NativeToolProjector.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal static class NativeToolProjector
2121
["command", "path", "file_path", "query", "url", "pattern"];
2222

2323
internal const int MaxDisplayValueChars = 240;
24+
internal const int MaxPersistedToolArgumentsChars = 64 * 1024;
2425
private const int MaxIdentityChars = 80;
2526
private const int ToolOutputMaxChars = 4000;
2627

@@ -181,8 +182,11 @@ internal static string GetStringProperty(JsonElement data, params string[] names
181182
return null;
182183

183184
var json = encoded.GetString();
184-
if (string.IsNullOrWhiteSpace(json))
185+
if (string.IsNullOrWhiteSpace(json)
186+
|| json.Length > MaxPersistedToolArgumentsChars)
187+
{
185188
return null;
189+
}
186190

187191
try
188192
{

‎tests/OpenClaw.Tray.Tests/NativeToolProjectorTests.cs‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,45 @@ public void ExtractSafeToolDisplayArgs_AllowlistsRedactsAndBoundsValues()
6868
Assert.DoesNotContain("json-must-not-render", args.ToJsonString(), StringComparison.Ordinal);
6969
}
7070

71+
[Fact]
72+
public void ExtractSafePersistedToolDisplayArgs_DecodesAllowlistedRedactedInput()
73+
{
74+
var value = JsonSerializer.SerializeToElement(
75+
"""{"command":"curl https://example.test --token abcdef1234567890ghij","workdir":"C:\\private","content":"must-not-render"}""");
76+
77+
var args = NativeToolProjector.ExtractSafePersistedToolDisplayArgs(value)!;
78+
79+
Assert.DoesNotContain(
80+
"abcdef1234567890ghij",
81+
args["command"]!.GetValue<string>(),
82+
StringComparison.Ordinal);
83+
Assert.False(args.ContainsKey("workdir"));
84+
Assert.False(args.ContainsKey("content"));
85+
Assert.DoesNotContain("must-not-render", args.ToJsonString(), StringComparison.Ordinal);
86+
}
87+
88+
[Theory]
89+
[InlineData("")]
90+
[InlineData("not-json")]
91+
[InlineData("[]")]
92+
[InlineData("null")]
93+
public void ExtractSafePersistedToolDisplayArgs_RejectsMalformedOrNonObjectInput(
94+
string encoded)
95+
{
96+
var value = JsonSerializer.SerializeToElement(encoded);
97+
98+
Assert.Null(NativeToolProjector.ExtractSafePersistedToolDisplayArgs(value));
99+
}
100+
101+
[Fact]
102+
public void ExtractSafePersistedToolDisplayArgs_RejectsOversizedInputBeforeParsing()
103+
{
104+
var value = JsonSerializer.SerializeToElement(
105+
new string('x', NativeToolProjector.MaxPersistedToolArgumentsChars + 1));
106+
107+
Assert.Null(NativeToolProjector.ExtractSafePersistedToolDisplayArgs(value));
108+
}
109+
71110
[Fact]
72111
public void ExtractToolCorrelationId_ExplicitToolCallIdIsOpaqueAndWins()
73112
{
@@ -131,5 +170,4 @@ public void Source_DoesNotUseInventedCorrelationField()
131170

132171
Assert.Empty(matches);
133172
}
134-
135173
}

‎tests/OpenClaw.Tray.Tests/OpenClawChatDataProviderTests.cs‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7362,6 +7362,61 @@ public async Task LoadHistoryAsync_StringEncodedToolArguments_PreservesSafeInput
73627362
Assert.False(entry.ToolArgs?.ContainsKey("workdir"));
73637363
}
73647364

7365+
[Fact]
7366+
public async Task LoadHistoryAsync_AfterRestart_ReplaysSanitizedArgumentsWithoutLocalPersistence()
7367+
{
7368+
using var tempDir = new TempDirectory();
7369+
var cachePath = Path.Combine(tempDir.DirectoryPath, "tool-metadata.json");
7370+
static ChatHistoryInfo History() => new()
7371+
{
7372+
SessionKey = "main",
7373+
Messages =
7374+
[
7375+
new ChatMessageInfo
7376+
{
7377+
Role = "assistant",
7378+
State = "final",
7379+
Ts = 1,
7380+
ToolContent =
7381+
[
7382+
new ChatToolContentInfo
7383+
{
7384+
Kind = ChatToolContentKind.Call,
7385+
CallId = "call-1",
7386+
ToolName = "exec",
7387+
Args = JsonSerializer.SerializeToElement(
7388+
"""{"command":"curl https://example.test --token abcdef1234567890ghij","workdir":"C:\\private"}"""),
7389+
},
7390+
],
7391+
},
7392+
],
7393+
};
7394+
7395+
var first = CreateProvider(
7396+
new[] { MainSession() },
7397+
toolMetaCachePath: cachePath);
7398+
first.bridge.HistoryBehavior = _ => Task.FromResult(History());
7399+
await first.provider.LoadHistoryAsync("main");
7400+
await first.provider.DisposeAsync();
7401+
7402+
var second = CreateProvider(
7403+
new[] { MainSession() },
7404+
toolMetaCachePath: cachePath);
7405+
second.bridge.HistoryBehavior = _ => Task.FromResult(History());
7406+
await second.provider.LoadHistoryAsync("main");
7407+
7408+
var entry = Assert.Single(second.snapshots[^1].Timelines["main"].Entries);
7409+
var command = entry.ToolArgs?["command"]?.GetValue<string>();
7410+
Assert.NotNull(command);
7411+
Assert.DoesNotContain(
7412+
"abcdef1234567890ghij",
7413+
command,
7414+
StringComparison.Ordinal);
7415+
Assert.False(entry.ToolArgs?.ContainsKey("workdir"));
7416+
Assert.False(File.Exists(cachePath));
7417+
await second.provider.DisposeAsync();
7418+
}
7419+
73657420
[Theory]
73667421
[InlineData("toolResult")]
73677422
[InlineData("tool_result")]

0 commit comments

Comments
 (0)