Skip to content

Commit

Permalink
[ImageResizer]Sanitize target file name (#14040)
Browse files Browse the repository at this point in the history
* [ImageResizer] Sanitize target file name

* Add a test

* Avoid not recommended file names
  • Loading branch information
jaimecbernardo committed Nov 8, 2021
1 parent 06882b4 commit c2adab0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/modules/imageresizer/tests/Models/ResizeOperationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,41 @@ public void StripMetadataWhenNoMetadataPresent()
image => Assert.IsNull(((BitmapMetadata)image.Frames[0].Metadata).GetQuerySafe("System.Photo.Orientation")));
}

[TestMethod]
public void VerifyFileNameIsSanitized()
{
var operation = new ResizeOperation(
"Test.png",
_directory,
Settings(
s =>
{
s.FileName = @"Directory\%1:*?""<>|(%2)";
s.SelectedSize.Name = "Test\\/";
}));

operation.Execute();

Assert.IsTrue(File.Exists(_directory + @"\Directory\Test_______(Test__).png"));
}

[TestMethod]
public void VerifyNotRecommendedNameIsChanged()
{
var operation = new ResizeOperation(
"Test.png",
_directory,
Settings(
s =>
{
s.FileName = @"nul";
}));

operation.Execute();

Assert.IsTrue(File.Exists(_directory + @"\nul_.png"));
}

private static Settings Settings(Action<Settings> action = null)
{
var settings = new Settings()
Expand Down
33 changes: 32 additions & 1 deletion src/modules/imageresizer/ui/Models/ResizeOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ internal class ResizeOperation
private readonly string _destinationDirectory;
private readonly Settings _settings;

// Filenames to avoid according to https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#file-and-directory-names
private static readonly string[] _avoidFilenames =
{
"CON", "PRN", "AUX", "NUL",
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
};

public ResizeOperation(string file, string destinationDirectory, Settings settings)
{
_file = file;
Expand Down Expand Up @@ -205,16 +213,39 @@ private string GetDestinationPath(BitmapEncoder encoder)
extension = supportedExtensions.FirstOrDefault();
}

// Remove directory characters from the size's name.
string sizeNameSanitized = _settings.SelectedSize.Name;
sizeNameSanitized = sizeNameSanitized
.Replace('\\', '_')
.Replace('/', '_');

// Using CurrentCulture since this is user facing
var fileName = string.Format(
CultureInfo.CurrentCulture,
_settings.FileNameFormat,
originalFileName,
_settings.SelectedSize.Name,
sizeNameSanitized,
_settings.SelectedSize.Width,
_settings.SelectedSize.Height,
encoder.Frames[0].PixelWidth,
encoder.Frames[0].PixelHeight);

// Remove invalid characters from the final file name.
fileName = fileName
.Replace(':', '_')
.Replace('*', '_')
.Replace('?', '_')
.Replace('"', '_')
.Replace('<', '_')
.Replace('>', '_')
.Replace('|', '_');

// Avoid creating not recommended filenames
if (_avoidFilenames.Contains(fileName.ToUpperInvariant()))
{
fileName = fileName + "_";
}

var path = _fileSystem.Path.Combine(directory, fileName + extension);
var uniquifier = 1;
while (_fileSystem.File.Exists(path))
Expand Down

0 comments on commit c2adab0

Please sign in to comment.