Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion dotnet/src/Plugins/Plugins.Core/FileIOPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private bool TryGetAllowedFilePath(string path, out string canonicalPath)
Verify.NotNullOrWhiteSpace(path);
canonicalPath = string.Empty;

if (path.StartsWith("\\\\", StringComparison.OrdinalIgnoreCase))
if (IsUncOrExtendedPath(path))
{
throw new ArgumentException("Invalid file path, UNC paths are not supported.", nameof(path));
}
Comment on lines +116 to 119
Expand Down Expand Up @@ -162,5 +162,10 @@ private bool TryGetAllowedFilePath(string path, out string canonicalPath)

return false;
}

private static bool IsUncOrExtendedPath(string path) =>
path.Length >= 2 &&
(path[0] is '/' or '\\') &&
(path[1] is '/' or '\\');
#endregion
}
35 changes: 35 additions & 0 deletions dotnet/src/Plugins/Plugins.UnitTests/Core/FileIOPluginTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,41 @@ public async Task ItCannotReadFromDisallowedFoldersAsync()
await Assert.ThrowsAsync<ArgumentException>(async () => await plugin.ReadAsync(Path.Combine("", Path.GetRandomFileName())));
}

[Theory]
[InlineData("\\\\UNC\\server\\folder\\myfile.txt")]
[InlineData("//UNC/server/folder/myfile.txt")]
[InlineData("/\\UNC\\server\\folder\\myfile.txt")]
[InlineData("\\/UNC/server/folder/myfile.txt")]
public async Task ItRejectsUncOrExtendedPathsOnReadAsync(string path)
{
// Arrange
var plugin = new FileIOPlugin()
{
AllowedFolders = [Path.GetTempPath()]
};

// Act & Assert
await Assert.ThrowsAsync<ArgumentException>(() => plugin.ReadAsync(path));
}

[Theory]
[InlineData("\\\\UNC\\server\\folder\\myfile.txt")]
[InlineData("//UNC/server/folder/myfile.txt")]
[InlineData("/\\UNC\\server\\folder\\myfile.txt")]
[InlineData("\\/UNC/server/folder/myfile.txt")]
public async Task ItRejectsUncOrExtendedPathsOnWriteAsync(string path)
{
// Arrange
var plugin = new FileIOPlugin()
{
AllowedFolders = [Path.GetTempPath()],
DisableFileOverwrite = false
};

// Act & Assert
await Assert.ThrowsAsync<ArgumentException>(() => plugin.WriteAsync(path, "hello world"));
}

[Fact]
public async Task ItCannotReadThroughSymlinkOutsideAllowedFoldersAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,26 @@ public async Task DownloadToFileFailsForInvalidParametersAsync()
await Assert.ThrowsAsync<InvalidOperationException>(async () => await webFileDownload.DownloadToFileAsync(validUri, "myfile.txt"));
}

[Theory]
[InlineData("\\\\UNC\\server\\folder\\myfile.txt")]
[InlineData("//UNC/server/folder/myfile.txt")]
[InlineData("/\\UNC\\server\\folder\\myfile.txt")]
[InlineData("\\/UNC/server/folder/myfile.txt")]
public async Task DownloadToFileRejectsUncOrExtendedPathsAsync(string filePath)
{
// Arrange
var uri = new Uri("https://raw.githubusercontent.com/microsoft/semantic-kernel/refs/heads/main/docs/images/sk_logo.png");
var folderPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
var webFileDownload = new WebFileDownloadPlugin()
{
AllowedDomains = ["raw.githubusercontent.com"],
AllowedFolders = [folderPath]
};

// Act & Assert
await Assert.ThrowsAsync<ArgumentException>(() => webFileDownload.DownloadToFileAsync(uri, filePath));
}

[Fact]
public async Task DownloadToFileUsesCaseSensitiveAllowListComparisonOnLinuxAsync()
{
Expand Down
11 changes: 5 additions & 6 deletions dotnet/src/Plugins/Plugins.Web/WebFileDownloadPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,10 @@ private static string CanonicalizePath(string path)
return PathUtilities.GetSafeFullPath(expanded);
}

private static bool IsUncOrExtendedPath(string path)
{
return path.StartsWith("\\\\", StringComparison.OrdinalIgnoreCase) ||
path.StartsWith("//", StringComparison.OrdinalIgnoreCase);
}
private static bool IsUncOrExtendedPath(string path) =>
path.Length >= 2 &&
(path[0] is '/' or '\\') &&
(path[1] is '/' or '\\');

/// <summary>
/// If a list of allowed folder has been provided, the folder of the provided filePath is checked
Expand All @@ -239,7 +238,7 @@ private bool IsFilePathAllowed(string path)
{
Verify.NotNullOrWhiteSpace(path);

if (path.StartsWith("\\\\", StringComparison.OrdinalIgnoreCase))
if (IsUncOrExtendedPath(path))
{
throw new ArgumentException("Invalid file path, UNC paths are not supported.", nameof(path));
}
Expand Down
Loading