Skip to content

Commit a1668d0

Browse files
committed
Refine image validation flow
1 parent aafeb7a commit a1668d0

3 files changed

Lines changed: 24 additions & 18 deletions

File tree

src/PrivatePdfConverter/Commands/DirToPdf.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ public static void ConvertDirectoryToOnePdf(string path, string? output)
2323
}
2424

2525
using var images = new MagickImageCollection();
26-
try
26+
foreach (var file in supportedFiles)
2727
{
28-
supportedFiles.ForEach(x => images.Add(FileService.LoadValidatedImage(x)));
29-
}
30-
catch (InvalidDataException ex)
31-
{
32-
Log.Logger.Error(ex, ex.Message);
33-
return;
28+
var image = FileService.LoadValidatedImage(file);
29+
if (image is null)
30+
{
31+
return;
32+
}
33+
34+
images.Add(image);
3435
}
3536

3637
SaveAsPdf(path, images, output);

src/PrivatePdfConverter/Commands/ImgToPdf.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@ public static void ConvertImageToOnePdf(string path, string? output)
2323
Log.Logger.Information("Read 1 file with name: {FileName}, Full path: '{Path}'", Path.GetFileName(path), path);
2424

2525
using var images = new MagickImageCollection();
26-
try
26+
var image = FileService.LoadValidatedImage(path);
27+
if (image is null)
2728
{
28-
images.Add(FileService.LoadValidatedImage(path));
29-
}
30-
catch (InvalidDataException ex)
31-
{
32-
Log.Logger.Error(ex, ex.Message);
3329
return;
3430
}
3531

32+
images.Add(image);
3633
SaveAsPdf(path, images, output);
3734
}
3835

src/PrivatePdfConverter/Services/FileService.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace PrivatePdfConverter.Services;
55

66
public static class FileService
77
{
8+
// Conservative defaults chosen to allow typical high-resolution images
9+
// while rejecting obviously pathological dimensions before full decode.
810
private const ulong MaxWidth = 10_000;
911
private const ulong MaxHeight = 10_000;
1012
private const ulong MaxArea = 100_000_000;
@@ -17,7 +19,7 @@ public static class FileService
1719
public static bool IsImage(this string? extension)
1820
=> !string.IsNullOrEmpty(extension) && ValidExtensions.Contains(extension.ToLower()[1..]);
1921

20-
public static MagickImage LoadValidatedImage(string path)
22+
public static MagickImage? LoadValidatedImage(string path)
2123
{
2224
using var headerImage = new MagickImage();
2325
headerImage.Ping(path);
@@ -28,13 +30,19 @@ public static MagickImage LoadValidatedImage(string path)
2830

2931
if (width == 0 || height == 0)
3032
{
31-
throw new InvalidDataException($"Image '{path}' has invalid dimensions {width}x{height}.");
33+
Log.Logger.Error("Image '{Path}' has invalid dimensions {Width}x{Height}.", path, width, height);
34+
return null;
3235
}
3336

34-
if ((ulong)width > MaxWidth || (ulong)height > MaxHeight || area > MaxArea)
37+
if (width > MaxWidth || height > MaxHeight || area > MaxArea)
3538
{
36-
throw new InvalidDataException(
37-
$"Image '{path}' exceeds the supported limits ({width}x{height}, area {area}).");
39+
Log.Logger.Error(
40+
"Image '{Path}' exceeds the supported limits ({Width}x{Height}, area {Area}).",
41+
path,
42+
width,
43+
height,
44+
area);
45+
return null;
3846
}
3947

4048
return new MagickImage(path);

0 commit comments

Comments
 (0)