Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure disk access is safe from malicious input #20

Merged
merged 1 commit into from
Aug 14, 2024
Merged
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
24 changes: 24 additions & 0 deletions dotnet/WorkbenchConnector/Storage/AgentServiceStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ public class AgentServiceStorage : IAgentServiceStorage
{
private static readonly JsonSerializerOptions s_jsonOptions = new() { WriteIndented = true };

private static readonly char[] s_notSafe =
[
'\0', '\n', '\r',
Path.PathSeparator, // ':' (nix) or ';' (win)
Path.DirectorySeparatorChar, // '/' (nix) or '\' (win)
Path.VolumeSeparatorChar, // '/' (nix) or ':' (win)
Path.AltDirectorySeparatorChar, // '/'
];

private static readonly char[] s_notSafe2 = Path.GetInvalidPathChars();

private readonly ILogger<AgentServiceStorage> _log;
private readonly string _path;

Expand Down Expand Up @@ -133,6 +144,7 @@ private async Task<List<T>> GetAllAsync<T>(string prefix, string suffix, Cancell

private string GetAgentFilename(AgentBase agent)
{
EnsureSafe(agent.Id);
return Path.Join(this._path, $"{agent.Id}.agent.json");
}

Expand All @@ -143,11 +155,23 @@ private string GetConversationFilename(Conversation conversation)

private string GetConversationFilename(string agentId, string conversationId)
{
EnsureSafe(agentId);
EnsureSafe(conversationId);
return Path.Join(this._path, $"{agentId}.{conversationId}.conversation.json");
}

private string GetInsightFilename(string agentId, string conversationId, string insightId)
{
EnsureSafe(agentId);
EnsureSafe(conversationId);
EnsureSafe(insightId);
return Path.Join(this._path, $"{agentId}.{conversationId}.{insightId}.insight.json");
}

private static void EnsureSafe(string input)
{
if (input.IndexOfAny(s_notSafe) < 0 && input.IndexOfAny(s_notSafe2) < 0) { return; }

throw new ArgumentException("The file or path value contains invalid chars");
}
}