From f4481be9db02e8804d9b9bbffc533ba8ba9e0257 Mon Sep 17 00:00:00 2001 From: James Moore Date: Tue, 9 Sep 2025 09:59:50 +0100 Subject: [PATCH] Validate asset search pattern and add tests --- ChatGPTExport/Assets/ExistingAssetLocator.cs | 12 ++++- .../Assets/ExistingAssetLocatorTests.cs | 47 +++++++++++++++++++ ChatGTPExportTests/ChatGTPExportTests.csproj | 1 + 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 ChatGTPExportTests/Assets/ExistingAssetLocatorTests.cs diff --git a/ChatGPTExport/Assets/ExistingAssetLocator.cs b/ChatGPTExport/Assets/ExistingAssetLocator.cs index 1d56f6f..53c8a2c 100644 --- a/ChatGPTExport/Assets/ExistingAssetLocator.cs +++ b/ChatGPTExport/Assets/ExistingAssetLocator.cs @@ -25,7 +25,17 @@ public void Add(string newFile) public string? GetMarkdownMediaAsset(AssetRequest assetRequest) { - // it may already exist in the destination directory from a previous export + var invalidChars = fileSystem.Path.GetInvalidFileNameChars() + .Concat(new[] { '*', '?', fileSystem.Path.DirectorySeparatorChar, fileSystem.Path.AltDirectorySeparatorChar }) + .Distinct() + .ToArray(); + + if (assetRequest.SearchPattern.IndexOfAny(invalidChars) >= 0) + { + return null; + } + + // it may already exist in the destination directory from a previous export var destinationMatches = GetCachedDestinationFiles(assetRequest.SearchPattern).ToList(); if (destinationMatches.Count == 0) { diff --git a/ChatGTPExportTests/Assets/ExistingAssetLocatorTests.cs b/ChatGTPExportTests/Assets/ExistingAssetLocatorTests.cs new file mode 100644 index 0000000..8eae3ee --- /dev/null +++ b/ChatGTPExportTests/Assets/ExistingAssetLocatorTests.cs @@ -0,0 +1,47 @@ +using ChatGPTExport.Assets; +using System.IO.Abstractions.TestingHelpers; + +namespace ChatGTPExportTests.Assets +{ + public class ExistingAssetLocatorTests + { + [Theory] + [InlineData("image*")] + [InlineData("image?")] + [InlineData("image/")] + [InlineData("image\\")] + [InlineData("invali +