From a2ac791bb779297bedb608825602912228d415c1 Mon Sep 17 00:00:00 2001 From: Ronan Charles-Lorel Date: Tue, 31 Jan 2023 15:20:57 +0100 Subject: [PATCH 1/6] Add a way to add more invalid characters when sanitizing a filename --- Emby.Server.Implementations/IO/ManagedFileSystem.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Emby.Server.Implementations/IO/ManagedFileSystem.cs b/Emby.Server.Implementations/IO/ManagedFileSystem.cs index 55f384ae8f..9b8831a1fe 100644 --- a/Emby.Server.Implementations/IO/ManagedFileSystem.cs +++ b/Emby.Server.Implementations/IO/ManagedFileSystem.cs @@ -294,7 +294,9 @@ namespace Emby.Server.Implementations.IO /// The filename is null. public string GetValidFilename(string filename) { - var invalid = Path.GetInvalidFileNameChars(); + //necessary because (as per the doc) GetInvalidFileNameChars is not exhaustive and may not return all invalid chars, which creates issues + char[] genericInvalidChars = {':'}; + var invalid = Path.GetInvalidFileNameChars().Concat(genericInvalidChars).ToArray(); var first = filename.IndexOfAny(invalid); if (first == -1) { From 31ac861b8560547c7e0c46513077abf76e6bc618 Mon Sep 17 00:00:00 2001 From: Ronan Charles-Lorel Date: Tue, 31 Jan 2023 15:47:47 +0100 Subject: [PATCH 2/6] Formatting --- Emby.Server.Implementations/IO/ManagedFileSystem.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Emby.Server.Implementations/IO/ManagedFileSystem.cs b/Emby.Server.Implementations/IO/ManagedFileSystem.cs index 9b8831a1fe..7b8c79e8a9 100644 --- a/Emby.Server.Implementations/IO/ManagedFileSystem.cs +++ b/Emby.Server.Implementations/IO/ManagedFileSystem.cs @@ -294,8 +294,8 @@ namespace Emby.Server.Implementations.IO /// The filename is null. public string GetValidFilename(string filename) { - //necessary because (as per the doc) GetInvalidFileNameChars is not exhaustive and may not return all invalid chars, which creates issues - char[] genericInvalidChars = {':'}; + // necessary because (as per the doc) GetInvalidFileNameChars is not exhaustive and may not return all invalid chars, which creates issues + char[] genericInvalidChars = { ':' }; var invalid = Path.GetInvalidFileNameChars().Concat(genericInvalidChars).ToArray(); var first = filename.IndexOfAny(invalid); if (first == -1) From 46763b7661948b463303fd5b12831a146e987886 Mon Sep 17 00:00:00 2001 From: Ronan Charles-Lorel Date: Thu, 29 Jun 2023 15:21:39 +0200 Subject: [PATCH 3/6] Remove call to Path.GetInvalidFileNameChars Superseded by a static char list to avoid platform-dependent issues --- Emby.Server.Implementations/IO/ManagedFileSystem.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Emby.Server.Implementations/IO/ManagedFileSystem.cs b/Emby.Server.Implementations/IO/ManagedFileSystem.cs index 60ab668cde..e9e75166bc 100644 --- a/Emby.Server.Implementations/IO/ManagedFileSystem.cs +++ b/Emby.Server.Implementations/IO/ManagedFileSystem.cs @@ -275,9 +275,14 @@ namespace Emby.Server.Implementations.IO /// The filename is null. public string GetValidFilename(string filename) { - // necessary because (as per the doc) GetInvalidFileNameChars is not exhaustive and may not return all invalid chars, which creates issues - char[] genericInvalidChars = { ':' }; - var invalid = Path.GetInvalidFileNameChars().Concat(genericInvalidChars).ToArray(); + // using a character list instead of GetInvalidFileNameChars, as it is not exhaustive and may not return all invalid chars + char[] invalid = { + '\"', '<', '>', '|', '\0', + (char)1, (char)2, (char)3, (char)4, (char)5, (char)6, (char)7, (char)8, (char)9, (char)10, + (char)11, (char)12, (char)13, (char)14, (char)15, (char)16, (char)17, (char)18, (char)19, (char)20, + (char)21, (char)22, (char)23, (char)24, (char)25, (char)26, (char)27, (char)28, (char)29, (char)30, + (char)31, ':', '*', '?', '\\', '/' + }; var first = filename.IndexOfAny(invalid); if (first == -1) { From 07c142d5bd8d1dbea61cb16d5089f04f00b2ca3a Mon Sep 17 00:00:00 2001 From: Ronan Charles-Lorel Date: Thu, 29 Jun 2023 16:04:45 +0200 Subject: [PATCH 4/6] Moving invalid chars list at class level with a better name --- .../IO/ManagedFileSystem.cs | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/Emby.Server.Implementations/IO/ManagedFileSystem.cs b/Emby.Server.Implementations/IO/ManagedFileSystem.cs index e9e75166bc..193185dfea 100644 --- a/Emby.Server.Implementations/IO/ManagedFileSystem.cs +++ b/Emby.Server.Implementations/IO/ManagedFileSystem.cs @@ -20,7 +20,13 @@ namespace Emby.Server.Implementations.IO private readonly List _shortcutHandlers = new List(); private readonly string _tempPath; private static readonly bool _isEnvironmentCaseInsensitive = OperatingSystem.IsWindows(); - + private static readonly char[] _invalidPathCharacters = { + '\"', '<', '>', '|', '\0', + (char)1, (char)2, (char)3, (char)4, (char)5, (char)6, (char)7, (char)8, (char)9, (char)10, + (char)11, (char)12, (char)13, (char)14, (char)15, (char)16, (char)17, (char)18, (char)19, (char)20, + (char)21, (char)22, (char)23, (char)24, (char)25, (char)26, (char)27, (char)28, (char)29, (char)30, + (char)31, ':', '*', '?', '\\', '/' + }; /// /// Initializes a new instance of the class. /// @@ -275,15 +281,7 @@ namespace Emby.Server.Implementations.IO /// The filename is null. public string GetValidFilename(string filename) { - // using a character list instead of GetInvalidFileNameChars, as it is not exhaustive and may not return all invalid chars - char[] invalid = { - '\"', '<', '>', '|', '\0', - (char)1, (char)2, (char)3, (char)4, (char)5, (char)6, (char)7, (char)8, (char)9, (char)10, - (char)11, (char)12, (char)13, (char)14, (char)15, (char)16, (char)17, (char)18, (char)19, (char)20, - (char)21, (char)22, (char)23, (char)24, (char)25, (char)26, (char)27, (char)28, (char)29, (char)30, - (char)31, ':', '*', '?', '\\', '/' - }; - var first = filename.IndexOfAny(invalid); + var first = filename.IndexOfAny(_invalidPathCharacters); if (first == -1) { // Fast path for clean strings @@ -292,7 +290,7 @@ namespace Emby.Server.Implementations.IO return string.Create( filename.Length, - (filename, invalid, first), + (filename, _invalidPathCharacters, first), (chars, state) => { state.filename.AsSpan().CopyTo(chars); @@ -300,7 +298,7 @@ namespace Emby.Server.Implementations.IO chars[state.first++] = ' '; var len = chars.Length; - foreach (var c in state.invalid) + foreach (var c in state._invalidPathCharacters) { for (int i = state.first; i < len; i++) { From c21140eeb54fe628de957d07a06bd800ad518f42 Mon Sep 17 00:00:00 2001 From: Ronan Charles-Lorel Date: Sat, 1 Jul 2023 03:24:19 +0200 Subject: [PATCH 5/6] Formatting Fixes debug build? Co-authored-by: Bond-009 --- .../IO/ManagedFileSystem.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Emby.Server.Implementations/IO/ManagedFileSystem.cs b/Emby.Server.Implementations/IO/ManagedFileSystem.cs index 193185dfea..d27024f8ae 100644 --- a/Emby.Server.Implementations/IO/ManagedFileSystem.cs +++ b/Emby.Server.Implementations/IO/ManagedFileSystem.cs @@ -20,13 +20,15 @@ namespace Emby.Server.Implementations.IO private readonly List _shortcutHandlers = new List(); private readonly string _tempPath; private static readonly bool _isEnvironmentCaseInsensitive = OperatingSystem.IsWindows(); - private static readonly char[] _invalidPathCharacters = { - '\"', '<', '>', '|', '\0', - (char)1, (char)2, (char)3, (char)4, (char)5, (char)6, (char)7, (char)8, (char)9, (char)10, - (char)11, (char)12, (char)13, (char)14, (char)15, (char)16, (char)17, (char)18, (char)19, (char)20, - (char)21, (char)22, (char)23, (char)24, (char)25, (char)26, (char)27, (char)28, (char)29, (char)30, - (char)31, ':', '*', '?', '\\', '/' - }; + private static readonly char[] _invalidPathCharacters = + { + '\"', '<', '>', '|', '\0', + (char)1, (char)2, (char)3, (char)4, (char)5, (char)6, (char)7, (char)8, (char)9, (char)10, + (char)11, (char)12, (char)13, (char)14, (char)15, (char)16, (char)17, (char)18, (char)19, (char)20, + (char)21, (char)22, (char)23, (char)24, (char)25, (char)26, (char)27, (char)28, (char)29, (char)30, + (char)31, ':', '*', '?', '\\', '/' + }; + /// /// Initializes a new instance of the class. /// From 4dc87a6f93249b15d6660781c0026b77f1066c37 Mon Sep 17 00:00:00 2001 From: Ronan Charles-Lorel Date: Sat, 1 Jul 2023 03:37:18 +0200 Subject: [PATCH 6/6] Align indentation on bottom brace of new list Should stop error SA1137 in debug build --- Emby.Server.Implementations/IO/ManagedFileSystem.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Emby.Server.Implementations/IO/ManagedFileSystem.cs b/Emby.Server.Implementations/IO/ManagedFileSystem.cs index d27024f8ae..0ba4a488b0 100644 --- a/Emby.Server.Implementations/IO/ManagedFileSystem.cs +++ b/Emby.Server.Implementations/IO/ManagedFileSystem.cs @@ -27,7 +27,7 @@ namespace Emby.Server.Implementations.IO (char)11, (char)12, (char)13, (char)14, (char)15, (char)16, (char)17, (char)18, (char)19, (char)20, (char)21, (char)22, (char)23, (char)24, (char)25, (char)26, (char)27, (char)28, (char)29, (char)30, (char)31, ':', '*', '?', '\\', '/' - }; + }; /// /// Initializes a new instance of the class.