From 13e1ee14a8eb219e41b969fa446cee11408260fa Mon Sep 17 00:00:00 2001 From: Alexandre Mutel Date: Sat, 12 Nov 2022 17:36:11 +0100 Subject: [PATCH] Fix issue #70, do not allow control characters in path --- src/Zio.Tests/FileSystems/TestFileSystemBase.cs | 3 ++- src/Zio/FileSystems/FileSystem.cs | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Zio.Tests/FileSystems/TestFileSystemBase.cs b/src/Zio.Tests/FileSystems/TestFileSystemBase.cs index 5630024..3c22da3 100644 --- a/src/Zio.Tests/FileSystems/TestFileSystemBase.cs +++ b/src/Zio.Tests/FileSystems/TestFileSystemBase.cs @@ -266,7 +266,8 @@ protected void AssertCommonRead(IFileSystem fs, bool isReadOnly = false) Assert.Throws(() => fs.EnumeratePaths("/", null)); Assert.Throws(() => fs.ConvertPathFromInternal(null)); - + + Assert.Throws(() => fs.FileExists("/\0A.txt")); Assert.True(fs.FileExists("/A.txt")); Assert.True(fs.FileExists("/b.txt")); Assert.True(fs.FileExists("/b/b.i")); diff --git a/src/Zio/FileSystems/FileSystem.cs b/src/Zio/FileSystems/FileSystem.cs index 45aac8c..b06b960 100644 --- a/src/Zio/FileSystems/FileSystem.cs +++ b/src/Zio/FileSystems/FileSystem.cs @@ -584,6 +584,18 @@ protected UPath ValidatePath(UPath path, string name = "path", bool allowNull = { return path; } + + // Make sure that we don't have any control characters in the path. + var fullPath = path.FullName; + for (var i = 0; i < fullPath.Length; i++) + { + var c = fullPath[i]; + if (char.IsControl(c)) + { + throw new ArgumentException($"Invalid character found \\u{(int)c:X4} at index {i}", nameof(path)); + } + } + path.AssertAbsolute(name); return ValidatePathImpl(path, name);