|
| 1 | +using System.Text.Json.Nodes; |
| 2 | +using DotCraft.Configuration; |
| 3 | +using DotCraft.Security; |
| 4 | +using DotCraft.Skills; |
| 5 | +using DotCraft.Tools; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace DotCraft.Tests.Tools; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Verifies that the thread-scoped RequireApprovalOutsideWorkspace override reaches the |
| 12 | +/// core file/shell tool assembly. A thread that disables it must hard-reject |
| 13 | +/// outside-workspace operations instead of routing them through an (auto-approving) |
| 14 | +/// approval service. |
| 15 | +/// </summary> |
| 16 | +public sealed class CoreToolSourceWorkspaceBoundaryTests : IDisposable |
| 17 | +{ |
| 18 | + private readonly string _tempRoot = Path.Combine( |
| 19 | + Path.GetTempPath(), "dotcraft-coretool-boundary-tests", Guid.NewGuid().ToString("N")); |
| 20 | + private readonly string _workspace; |
| 21 | + private readonly string _outsideFile; |
| 22 | + |
| 23 | + public CoreToolSourceWorkspaceBoundaryTests() |
| 24 | + { |
| 25 | + _workspace = Path.Combine(_tempRoot, "workspace"); |
| 26 | + Directory.CreateDirectory(_workspace); |
| 27 | + _outsideFile = Path.Combine(_tempRoot, "outside.txt"); |
| 28 | + File.WriteAllText(_outsideFile, "TOP-PRIVATE-CONTENT"); |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public async Task Thread_override_false_hard_rejects_outside_workspace_read() |
| 33 | + { |
| 34 | + var registrations = await GetRegistrationsAsync(requireApprovalOutsideWorkspace: false); |
| 35 | + var readFile = Assert.Single(registrations, item => item.Definition.Name.Name == "ReadFile"); |
| 36 | + |
| 37 | + Assert.False(readFile.Definition.PolicyHints.RequiresApproval); |
| 38 | + Assert.False(readFile.Definition.Annotations.ContainsKey("dotcraft/nativeApproval")); |
| 39 | + |
| 40 | + var result = await InvokeAsync(readFile, new JsonObject { ["path"] = _outsideFile }); |
| 41 | + |
| 42 | + Assert.Contains("outside workspace", result.Content, StringComparison.OrdinalIgnoreCase); |
| 43 | + Assert.DoesNotContain("TOP-PRIVATE-CONTENT", result.Content, StringComparison.Ordinal); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public async Task Thread_override_false_hard_rejects_exec_referencing_outside_paths() |
| 48 | + { |
| 49 | + var registrations = await GetRegistrationsAsync(requireApprovalOutsideWorkspace: false); |
| 50 | + var exec = Assert.Single(registrations, item => item.Definition.Name.Name == "Exec"); |
| 51 | + |
| 52 | + Assert.False(exec.Definition.PolicyHints.RequiresApproval); |
| 53 | + |
| 54 | + var result = await InvokeAsync(exec, new JsonObject |
| 55 | + { |
| 56 | + ["command"] = $"cat \"{_outsideFile}\"" |
| 57 | + }); |
| 58 | + |
| 59 | + Assert.Contains("outside workspace", result.Content, StringComparison.OrdinalIgnoreCase); |
| 60 | + Assert.DoesNotContain("TOP-PRIVATE-CONTENT", result.Content, StringComparison.Ordinal); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public async Task Unset_thread_override_keeps_the_workspace_default_approval_routing() |
| 65 | + { |
| 66 | + var registrations = await GetRegistrationsAsync(requireApprovalOutsideWorkspace: null); |
| 67 | + var readFile = Assert.Single(registrations, item => item.Definition.Name.Name == "ReadFile"); |
| 68 | + var exec = Assert.Single(registrations, item => item.Definition.Name.Name == "Exec"); |
| 69 | + |
| 70 | + Assert.True(readFile.Definition.PolicyHints.RequiresApproval); |
| 71 | + Assert.True(readFile.Definition.Annotations.ContainsKey("dotcraft/nativeApproval")); |
| 72 | + Assert.True(exec.Definition.PolicyHints.RequiresApproval); |
| 73 | + Assert.True(exec.Definition.Annotations.ContainsKey("dotcraft/nativeApproval")); |
| 74 | + } |
| 75 | + |
| 76 | + public void Dispose() |
| 77 | + { |
| 78 | + try |
| 79 | + { |
| 80 | + if (Directory.Exists(_tempRoot)) |
| 81 | + Directory.Delete(_tempRoot, recursive: true); |
| 82 | + } |
| 83 | + catch |
| 84 | + { |
| 85 | + // Best-effort cleanup for temp test directories. |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private async Task<IReadOnlyList<ToolRegistration>> GetRegistrationsAsync( |
| 90 | + bool? requireApprovalOutsideWorkspace) |
| 91 | + { |
| 92 | + var config = AppConfigTestFactory.CreateOpenAI(); |
| 93 | + Assert.True(config.Tools.File.RequireApprovalOutsideWorkspace); |
| 94 | + var skillsLoader = new SkillsLoader(_workspace); |
| 95 | + var source = new CoreToolSource( |
| 96 | + config, |
| 97 | + TestModelProviderRegistry.Create(), |
| 98 | + skillsLoader, |
| 99 | + new AutoApproveApprovalService(), |
| 100 | + new StubBackgroundTerminalService()); |
| 101 | + return await source.GetRegistrationsAsync(new ToolPlanningContext( |
| 102 | + "thread-analyst", |
| 103 | + null, |
| 104 | + _workspace, |
| 105 | + Path.Combine(_workspace, ".craft"), |
| 106 | + "agent", |
| 107 | + null, |
| 108 | + [], |
| 109 | + 1, |
| 110 | + workspaceRoots: [_workspace], |
| 111 | + requireApprovalOutsideWorkspace: requireApprovalOutsideWorkspace)); |
| 112 | + } |
| 113 | + |
| 114 | + private static async Task<ToolExecutionResult> InvokeAsync( |
| 115 | + ToolRegistration registration, |
| 116 | + JsonObject arguments) => |
| 117 | + await registration.Binding.Runtime.InvokeAsync( |
| 118 | + new ToolInvocationContext( |
| 119 | + "thread-analyst", |
| 120 | + null, |
| 121 | + "call-1", |
| 122 | + ToolInvocationAudience.Model, |
| 123 | + registration.Definition.Name, |
| 124 | + registration.Definition.Id, |
| 125 | + registration.Binding.Id, |
| 126 | + registration.Binding.Revision, |
| 127 | + DateTimeOffset.UtcNow), |
| 128 | + arguments); |
| 129 | +} |
0 commit comments