diff --git a/BinaryObjectScanner/Scanner.cs b/BinaryObjectScanner/Scanner.cs index de1f27c3..44e06f70 100644 --- a/BinaryObjectScanner/Scanner.cs +++ b/BinaryObjectScanner/Scanner.cs @@ -46,6 +46,22 @@ namespace BinaryObjectScanner #endregion + #region Static Variables + + /// + /// Path to the temporary directory + /// + /// Value has trailing directory separators trimmed + private static readonly string TempFilePath = Path.GetTempPath().TrimEnd('\\', '/'); + + /// + /// Template for determining if a file is a temporary extracted path + /// + /// Value has trailing directory separators trimmed and the GUID value itself does not matter + private static readonly string TempFilePathWithGuid = Path.Combine(TempFilePath, Guid.NewGuid().ToString()).TrimEnd('\\', '/'); + + #endregion + /// /// Constructor /// @@ -123,10 +139,6 @@ namespace BinaryObjectScanner // Checkpoint _fileProgress?.Report(new ProtectionProgress(null, depth, 0, null)); - // Temp variables for reporting - string tempFilePath = Path.GetTempPath(); - string tempFilePathWithGuid = Path.Combine(tempFilePath, Guid.NewGuid().ToString()); - // Loop through each path and get the returned values var protections = new ProtectionDictionary(); foreach (string path in paths) @@ -152,13 +164,7 @@ namespace BinaryObjectScanner string file = files[i]; // Get the reportable file name - string reportableFileName = file; - if (reportableFileName.StartsWith(tempFilePath)) -#if NETCOREAPP || NETSTANDARD2_1_OR_GREATER - reportableFileName = reportableFileName[tempFilePathWithGuid.Length..]; -#else - reportableFileName = reportableFileName.Substring(tempFilePathWithGuid.Length); -#endif + string reportableFileName = GetReportableFileName(file); // Checkpoint _fileProgress?.Report(new ProtectionProgress(reportableFileName, depth, i / (float)files.Count, "Checking file" + (file != reportableFileName ? " from archive" : string.Empty))); @@ -193,13 +199,7 @@ namespace BinaryObjectScanner else if (File.Exists(path)) { // Get the reportable file name - string reportableFileName = path; - if (reportableFileName.StartsWith(tempFilePath)) -#if NETCOREAPP || NETSTANDARD2_1_OR_GREATER - reportableFileName = reportableFileName[tempFilePathWithGuid.Length..]; -#else - reportableFileName = reportableFileName.Substring(tempFilePathWithGuid.Length); -#endif + string reportableFileName = GetReportableFileName(path); // Checkpoint _fileProgress?.Report(new ProtectionProgress(reportableFileName, depth, 0, "Checking file" + (path != reportableFileName ? " from archive" : string.Empty))); @@ -517,6 +517,44 @@ namespace BinaryObjectScanner }; } + /// + /// Get the reportable file name by determining if the file was + /// likely extracted as a part of the scanning run. + /// + /// File path to check + /// Trimmed file path if it should be an extracted file, the full path as passed in otherwise + /// + /// This will be a false positive if intentionally scanning a path that + /// fits the pattern of '%TEMP%/{GUID}/{file}'. + private static string GetReportableFileName(string path) + { + // If the file is not in the temp directory + if (!path.StartsWith(TempFilePath)) + return path; + + // If the filename isn't longer than the template path + if (path.Length < TempFilePathWithGuid.Length) + return path; + + // Check if we're in a GUID subdirectory + string possibleGuid = path.Substring(TempFilePath.Length + 1, 36); +#if NET20 || NET35 + if (System.Text.RegularExpressions.Regex.IsMatch(possibleGuid, @"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}")) +#else + if (Guid.TryParse(possibleGuid, out _)) +#endif + { +#if NETCOREAPP || NETSTANDARD2_1_OR_GREATER + return path[TempFilePathWithGuid.Length..]; +#else + return path.Substring(TempFilePathWithGuid.Length); +#endif + } + + // Otherwise, return the path as given + return path; + } + #endregion } }