Skip to content
Merged
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
19 changes: 17 additions & 2 deletions 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 thread
rogerbarreto marked this conversation as resolved.
Expand All @@ -125,7 +125,17 @@ private bool TryGetAllowedFilePath(string path, out string canonicalPath)
throw new ArgumentException("Invalid file path, a fully qualified file location must be specified.", nameof(path));
}

canonicalPath = PathUtilities.GetSafeFullPath(path);
// Resolve the full path first (a pure string operation that does not touch the
// filesystem). A relative path can still resolve to a UNC path here, for example
// when the current directory is a UNC share, so re-check before GetSafeFullPath
// probes the filesystem while resolving symbolic links.
canonicalPath = Path.GetFullPath(path);
if (IsUncOrExtendedPath(canonicalPath))
{
throw new ArgumentException("Invalid file path, UNC paths are not supported.", nameof(path));
}

canonicalPath = PathUtilities.GetSafeFullPath(canonicalPath);

if (File.Exists(canonicalPath) && File.GetAttributes(canonicalPath).HasFlag(FileAttributes.ReadOnly))
{
Expand Down Expand Up @@ -162,5 +172,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
23 changes: 16 additions & 7 deletions dotnet/src/Plugins/Plugins.Web/WebFileDownloadPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,24 @@ private static string CanonicalizePath(string path)
throw new ArgumentException("Invalid file path, UNC paths are not supported.", nameof(path));
}

return PathUtilities.GetSafeFullPath(expanded);
}
// Resolve the full path first (a pure string operation that does not touch the
// filesystem). A relative path can still resolve to a UNC path here, for example
// when the current directory is a UNC share, so re-check before GetSafeFullPath
// probes the filesystem while resolving symbolic links.
var fullPath = Path.GetFullPath(expanded);
if (IsUncOrExtendedPath(fullPath))
{
throw new ArgumentException("Invalid file path, UNC paths are not supported.", nameof(path));
}

private static bool IsUncOrExtendedPath(string path)
{
return path.StartsWith("\\\\", StringComparison.OrdinalIgnoreCase) ||
path.StartsWith("//", StringComparison.OrdinalIgnoreCase);
return PathUtilities.GetSafeFullPath(fullPath);
}

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
/// to verify it is in the allowed folder list. Paths are canonicalized before comparison.
Expand All @@ -239,7 +248,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