Skip to content

Commit 8709ce3

Browse files
committed
feat: add image URL validation and handle nullable Uri safely
1 parent f2146b1 commit 8709ce3

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

OsmoDoc/Word/WordDocumentGenerator.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,16 @@ private async static Task ReplaceImagePlaceholders(string inputFilePath, string
315315

316316
string imagePath = imagePlaceholders[docProperty.Name!];
317317

318+
// Validate URL before downloading
319+
if (!Uri.TryCreate(imagePath, UriKind.Absolute, out Uri? tempUri))
320+
{
321+
throw new ArgumentException($"Invalid image URL: {imagePath}");
322+
}
323+
318324
// Asynchronously download image data using HttpClient
319325
using HttpClient httpClient = new HttpClient();
320-
byte[] imageData = await httpClient.GetByteArrayAsync(imagePath);
326+
Uri imageUri = tempUri!;
327+
byte[] imageData = await httpClient.GetByteArrayAsync(imageUri);
321328

322329
using (Stream partStream = imagePart.GetStream(FileMode.OpenOrCreate, FileAccess.Write))
323330
{

0 commit comments

Comments
 (0)