Skip to content

Commit e15ae16

Browse files
jstar0rogerbarreto
andauthored
.Net: Reject mixed-separator UNC paths in file plugins (#14166)
### Motivation and Context `FileIOPlugin` and `WebFileDownloadPlugin` rejected only some UNC path prefixes before path canonicalization. On Windows, mixed `/\` and `\/` prefixes resolve to UNC paths too, so those forms could reach filesystem path resolution before the allow-list rejected them. Fixes #14157. ### Description - Treat any two leading `/` or `\` characters as a UNC or extended-path prefix in both plugins. - Reject those paths before canonicalization, `File.Exists`, or `File.GetAttributes` can probe the filesystem. - Add read, write, and download regressions for all four separator combinations. The change is limited to the two plugins named in the issue. Single-separator rooted paths, normal local paths, allow-list behavior, symlink handling, overwrite checks, and download limits are unchanged. ### Verification ```bash dotnet test dotnet/src/Plugins/Plugins.UnitTests/Plugins.UnitTests.csproj --filter "FullyQualifiedName~ItRejectsUncOrExtendedPathsOnReadAsync|FullyQualifiedName~ItRejectsUncOrExtendedPathsOnWriteAsync|FullyQualifiedName~DownloadToFileRejectsUncOrExtendedPathsAsync" dotnet test dotnet/src/Plugins/Plugins.UnitTests/Plugins.UnitTests.csproj --filter "FullyQualifiedName~FileIOPluginTests" dotnet test dotnet/src/Plugins/Plugins.UnitTests/Plugins.UnitTests.csproj --filter "FullyQualifiedName!~DownloadToFileSucceedsAsync&FullyQualifiedName!~DownloadToFileFailsForInvalidParametersAsync" dotnet format dotnet/src/Plugins/Plugins.UnitTests/Plugins.UnitTests.csproj --verify-no-changes --no-restore ``` The focused regressions pass 12/12, all `FileIOPluginTests` pass 21/21, and the deterministic Plugins unit-test suite passes 409/409. The two excluded Web tests use a live HTTPS request and fail on unmodified `main` in this macOS environment because certificate revocation status is unavailable. ### Contribution Checklist - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible (409 deterministic tests pass; two live-network baseline tests are environment-limited as noted above) - [x] I didn't break anyone 😄 --------- Signed-off-by: King Star <[email protected]> Co-authored-by: Roger Barreto <[email protected]>
1 parent 33a3e55 commit e15ae16

4 files changed

Lines changed: 88 additions & 9 deletions

File tree

‎dotnet/src/Plugins/Plugins.Core/FileIOPlugin.cs‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private bool TryGetAllowedFilePath(string path, out string canonicalPath)
113113
Verify.NotNullOrWhiteSpace(path);
114114
canonicalPath = string.Empty;
115115

116-
if (path.StartsWith("\\\\", StringComparison.OrdinalIgnoreCase))
116+
if (IsUncOrExtendedPath(path))
117117
{
118118
throw new ArgumentException("Invalid file path, UNC paths are not supported.", nameof(path));
119119
}
@@ -125,7 +125,17 @@ private bool TryGetAllowedFilePath(string path, out string canonicalPath)
125125
throw new ArgumentException("Invalid file path, a fully qualified file location must be specified.", nameof(path));
126126
}
127127

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

130140
if (File.Exists(canonicalPath) && File.GetAttributes(canonicalPath).HasFlag(FileAttributes.ReadOnly))
131141
{
@@ -162,5 +172,10 @@ private bool TryGetAllowedFilePath(string path, out string canonicalPath)
162172

163173
return false;
164174
}
175+
176+
private static bool IsUncOrExtendedPath(string path) =>
177+
path.Length >= 2 &&
178+
(path[0] is '/' or '\\') &&
179+
(path[1] is '/' or '\\');
165180
#endregion
166181
}

‎dotnet/src/Plugins/Plugins.UnitTests/Core/FileIOPluginTests.cs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,41 @@ public async Task ItCannotReadFromDisallowedFoldersAsync()
144144
await Assert.ThrowsAsync<ArgumentException>(async () => await plugin.ReadAsync(Path.Combine("", Path.GetRandomFileName())));
145145
}
146146

147+
[Theory]
148+
[InlineData("\\\\UNC\\server\\folder\\myfile.txt")]
149+
[InlineData("//UNC/server/folder/myfile.txt")]
150+
[InlineData("/\\UNC\\server\\folder\\myfile.txt")]
151+
[InlineData("\\/UNC/server/folder/myfile.txt")]
152+
public async Task ItRejectsUncOrExtendedPathsOnReadAsync(string path)
153+
{
154+
// Arrange
155+
var plugin = new FileIOPlugin()
156+
{
157+
AllowedFolders = [Path.GetTempPath()]
158+
};
159+
160+
// Act & Assert
161+
await Assert.ThrowsAsync<ArgumentException>(() => plugin.ReadAsync(path));
162+
}
163+
164+
[Theory]
165+
[InlineData("\\\\UNC\\server\\folder\\myfile.txt")]
166+
[InlineData("//UNC/server/folder/myfile.txt")]
167+
[InlineData("/\\UNC\\server\\folder\\myfile.txt")]
168+
[InlineData("\\/UNC/server/folder/myfile.txt")]
169+
public async Task ItRejectsUncOrExtendedPathsOnWriteAsync(string path)
170+
{
171+
// Arrange
172+
var plugin = new FileIOPlugin()
173+
{
174+
AllowedFolders = [Path.GetTempPath()],
175+
DisableFileOverwrite = false
176+
};
177+
178+
// Act & Assert
179+
await Assert.ThrowsAsync<ArgumentException>(() => plugin.WriteAsync(path, "hello world"));
180+
}
181+
147182
[Fact]
148183
public async Task ItCannotReadThroughSymlinkOutsideAllowedFoldersAsync()
149184
{

‎dotnet/src/Plugins/Plugins.UnitTests/Web/WebFileDownloadPluginTests.cs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,26 @@ public async Task DownloadToFileFailsForInvalidParametersAsync()
230230
await Assert.ThrowsAsync<InvalidOperationException>(async () => await webFileDownload.DownloadToFileAsync(validUri, "myfile.txt"));
231231
}
232232

233+
[Theory]
234+
[InlineData("\\\\UNC\\server\\folder\\myfile.txt")]
235+
[InlineData("//UNC/server/folder/myfile.txt")]
236+
[InlineData("/\\UNC\\server\\folder\\myfile.txt")]
237+
[InlineData("\\/UNC/server/folder/myfile.txt")]
238+
public async Task DownloadToFileRejectsUncOrExtendedPathsAsync(string filePath)
239+
{
240+
// Arrange
241+
var uri = new Uri("https://raw.githubusercontent.com/microsoft/semantic-kernel/refs/heads/main/docs/images/sk_logo.png");
242+
var folderPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
243+
var webFileDownload = new WebFileDownloadPlugin()
244+
{
245+
AllowedDomains = ["raw.githubusercontent.com"],
246+
AllowedFolders = [folderPath]
247+
};
248+
249+
// Act & Assert
250+
await Assert.ThrowsAsync<ArgumentException>(() => webFileDownload.DownloadToFileAsync(uri, filePath));
251+
}
252+
233253
[Fact]
234254
public async Task DownloadToFileUsesCaseSensitiveAllowListComparisonOnLinuxAsync()
235255
{

‎dotnet/src/Plugins/Plugins.Web/WebFileDownloadPlugin.cs‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,24 @@ private static string CanonicalizePath(string path)
221221
throw new ArgumentException("Invalid file path, UNC paths are not supported.", nameof(path));
222222
}
223223

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

227-
private static bool IsUncOrExtendedPath(string path)
228-
{
229-
return path.StartsWith("\\\\", StringComparison.OrdinalIgnoreCase) ||
230-
path.StartsWith("//", StringComparison.OrdinalIgnoreCase);
234+
return PathUtilities.GetSafeFullPath(fullPath);
231235
}
232236

237+
private static bool IsUncOrExtendedPath(string path) =>
238+
path.Length >= 2 &&
239+
(path[0] is '/' or '\\') &&
240+
(path[1] is '/' or '\\');
241+
233242
/// <summary>
234243
/// If a list of allowed folder has been provided, the folder of the provided filePath is checked
235244
/// to verify it is in the allowed folder list. Paths are canonicalized before comparison.
@@ -239,7 +248,7 @@ private bool IsFilePathAllowed(string path)
239248
{
240249
Verify.NotNullOrWhiteSpace(path);
241250

242-
if (path.StartsWith("\\\\", StringComparison.OrdinalIgnoreCase))
251+
if (IsUncOrExtendedPath(path))
243252
{
244253
throw new ArgumentException("Invalid file path, UNC paths are not supported.", nameof(path));
245254
}

0 commit comments

Comments
 (0)