diff options
| author | tanaka takashi <isazaka@gmail.com> | 2026-08-09 21:08:20 +0900 |
|---|---|---|
| committer | tanaka takashi <isazaka@gmail.com> | 2026-08-09 21:08:20 +0900 |
| commit | b07b41cd435991cd254957af79055080c85b346e (patch) | |
| tree | 27720d3ce14e4d0b84670a55ac26a2f877ce3a20 /src/Agent.Windows/Security/ExecutablePolicy.cs | |
| parent | 2fefd29e86c3b926d0cf4a362251d3f119688612 (diff) | |
feat: AI Agent Sandbox MCP Server v1を実装
Specifications、Design、ImplementationPlanの優先順位を整理し、単一Task実行モデル、typed option優先、設定可能なresource上限、local Ollamaを必須とするRelease Gateを文書化。
.NET 10/C# 14の5 production project、3 test project、E2E Clientを構築。typed YAML、en/ja locale、fixed/user prompt、stdioおよびloopback限定Streamable HTTP、agent_run/agent_status、Session、Ollama text/tool/Vision、TaskGraphとtoken accountingを実装。
Windows sandboxとしてfixed AppContainer、canonical/reparse検査、能力別ACL、path+SHA-256 executable approval、child-process restriction、Job Object、task journal、atomic file transaction、crash recoveryを実装。WFPのIPv4/IPv6 default denyとexact allow、dynamic session、temporary loopback exemption、管理者/BFE診断を追加。
Security/Recovery test、MCP/Ollama integration test、stdio/HTTP Operational E2E、Release Bundle生成・checksum・manifest・fresh extraction検査を追加。Requirement TraceabilityとPhase 0〜11の進捗、初回セットアップ可能なREADMEを整備。
Debug/Release/publish/objをrepository rootのbuild/へ集約し、旧project-local bin/objへの依存を除去。artifacts/とbuild/をGit管理外に設定し、clean checkoutでもsolution buildからE2E Clientまで生成可能にした。
Diffstat (limited to 'src/Agent.Windows/Security/ExecutablePolicy.cs')
| -rw-r--r-- | src/Agent.Windows/Security/ExecutablePolicy.cs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/Agent.Windows/Security/ExecutablePolicy.cs b/src/Agent.Windows/Security/ExecutablePolicy.cs new file mode 100644 index 0000000..3d20bfa --- /dev/null +++ b/src/Agent.Windows/Security/ExecutablePolicy.cs @@ -0,0 +1,51 @@ +using System.Security.Cryptography; + +namespace Agent.Windows.Security; + +public sealed record ExecutableApproval(string Path, string Sha256); +public sealed record VerifiedExecutable(string CanonicalPath, string Sha256); + +public sealed class ExecutablePolicy(IEnumerable<ExecutableApproval> approvals, bool allowShell) +{ + private readonly IReadOnlyDictionary<string, string> _approvals = approvals.ToDictionary( + approval => CanonicalPath.Resolve(approval.Path), approval => NormalizeHash(approval.Sha256), StringComparer.OrdinalIgnoreCase); + + public VerifiedExecutable Verify(string executable) + { + string canonical = CanonicalPath.Resolve(executable); + if (!allowShell && IsShell(canonical)) throw new SandboxPolicyException("process.shell_denied", "Shell execution is disabled."); + if (!_approvals.TryGetValue(canonical, out string? approvedHash)) throw new SandboxPolicyException("process.path_denied", "Executable path is not approved."); + using FileStream stream = new(canonical, FileMode.Open, FileAccess.Read, FileShare.Read); + string actualHash = Convert.ToHexString(SHA256.HashData(stream)); + if (!CryptographicOperations.FixedTimeEquals(Convert.FromHexString(approvedHash), Convert.FromHexString(actualHash))) + throw new SandboxPolicyException("process.hash_denied", "Executable SHA-256 does not match its approval."); + return new(canonical, actualHash); + } + + public static string? ManagedEntryPoint(string executable, IReadOnlyList<string> arguments) + { + if (!Path.GetFileName(executable).Equals("dotnet.exe", StringComparison.OrdinalIgnoreCase) || arguments.Count == 0) return null; + if (arguments[0].Equals("exec", StringComparison.OrdinalIgnoreCase)) + { + if (arguments.Count < 2 || arguments[1].StartsWith("-", StringComparison.Ordinal)) + throw new SandboxPolicyException("process.managed_entry_invalid", "dotnet exec requires an explicit managed entry assembly as its next argument."); + return arguments[1]; + } + return arguments[0].EndsWith(".dll", StringComparison.OrdinalIgnoreCase) ? arguments[0] : null; + } + + public static string ComputeSha256(string executable) + { + using FileStream stream = new(CanonicalPath.Resolve(executable), FileMode.Open, FileAccess.Read, FileShare.Read); + return Convert.ToHexString(SHA256.HashData(stream)); + } + + private static bool IsShell(string path) => Path.GetFileName(path) is string name && + (name.Equals("cmd.exe", StringComparison.OrdinalIgnoreCase) || name.Equals("powershell.exe", StringComparison.OrdinalIgnoreCase) || name.Equals("pwsh.exe", StringComparison.OrdinalIgnoreCase)); + private static string NormalizeHash(string hash) + { + string normalized = hash.Replace("-", string.Empty, StringComparison.Ordinal).Trim().ToUpperInvariant(); + if (normalized.Length != 64 || !normalized.All(Uri.IsHexDigit)) throw new ArgumentException("SHA-256 approval must contain 64 hexadecimal characters.", nameof(hash)); + return normalized; + } +} |
