From 5391908ab339c9431d3f87fe201cf75e7ceecec0 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 19 Sep 2025 14:16:21 +0100 Subject: Update tests for disabling external file access by default --- Mammoth.Tests/DocumentConverterTests.cs | 24 +++++++++++++++++++----- Mammoth/DocumentConverter.cs | 24 +++++++++++++++--------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/Mammoth.Tests/DocumentConverterTests.cs b/Mammoth.Tests/DocumentConverterTests.cs index c61ac04..607d27f 100644 --- a/Mammoth.Tests/DocumentConverterTests.cs +++ b/Mammoth.Tests/DocumentConverterTests.cs @@ -69,23 +69,37 @@ namespace Mammoth.Tests { } [Fact] - public void ImagesStoredOutsideOfDocumentAreIncludedInOutput() { + public void WhenExternalFileAccessIsEnabledThenImagesStoredOutsideOfDocumentAreIncludedInOutput() { AssertSuccessfulConversion( - ConvertToHtml("external-picture.docx"), + ConvertToHtml("external-picture.docx", mammoth => mammoth.EnableExternalFileAccess()), "

"); } [Fact] - public void WarnIfDocumentHasImagesStoredOutsideOfDocumentWhenPathOfDocumentIsUnknown() { + public void WhenExternalFileAccessIsEnabledThenWarnIfDocumentHasImagesStoredOutsideOfDocumentWhenPathOfDocumentIsUnknown() { using (var file = File.OpenRead(TestFilePath("external-picture.docx"))) { - var result = new DocumentConverter().ConvertToHtml(file); + var result = new DocumentConverter() + .EnableExternalFileAccess() + .ConvertToHtml(file); + Assert.Equal("", result.Value); Assert.Equal(new[]{"could not open external image 'tiny-picture.png': path of document is unknown, but is required for relative URI"}, result.Warnings); } } [Fact] - public void WarnIfImagesStoredOutsideOfDocumentAreNotFound() { + public void GivenExternalFileAccessIsDisabledByDefaultThenWarnIfDocumentHasImagesStoredOutsideOfDocument() + { + using (var file = File.OpenRead(TestFilePath("external-picture.docx"))) { + var result = new DocumentConverter().ConvertToHtml(file); + + Assert.Equal("", result.Value); + Assert.Equal(new[]{"could not open external image 'tiny-picture.png': external file access is disabled"}, result.Warnings); + } + } + + [Fact] + public void WhenExternalFileAccessIsEnabledThenWarnIfImagesStoredOutsideOfDocumentAreNotFound() { var tempDirectory = Path.Combine(Path.GetTempPath(), "mammoth-" + Guid.NewGuid()); Directory.CreateDirectory(tempDirectory); try { diff --git a/Mammoth/DocumentConverter.cs b/Mammoth/DocumentConverter.cs index 4995e54..1866e87 100644 --- a/Mammoth/DocumentConverter.cs +++ b/Mammoth/DocumentConverter.cs @@ -31,34 +31,40 @@ namespace Mammoth { public DocumentConverter DisableDefaultStyleMap() { return new DocumentConverter(options.disableDefaultStyleMap()); } - + public DocumentConverter DisableEmbeddedStyleMap() { return new DocumentConverter(options.disableEmbeddedStyleMap()); } - - public DocumentConverter ImageConverter(Func> imageConverter) { + + public DocumentConverter EnableExternalFileAccess() + { + return new DocumentConverter(options.enableExternalFileAccess()); + } + + public DocumentConverter ImageConverter(Func> imageConverter) + { return new DocumentConverter(options.imageConverter(new ImageConverterShim(imageConverter))); } - + internal class ImageConverterShim : Mammoth.Couscous.org.zwobble.mammoth.images.ImageConverter__ImgElement { private readonly Func> func; - + internal ImageConverterShim(Func> func) { this.func = func; } - + public Mammoth.Couscous.java.util.Map convert(Mammoth.Couscous.org.zwobble.mammoth.images.Image image) { return ToJava.DictionaryToMap(func(new Image(image))); } } - + internal class Image : IImage { private readonly Mammoth.Couscous.org.zwobble.mammoth.images.Image image; - + internal Image(Mammoth.Couscous.org.zwobble.mammoth.images.Image image) { this.image = image; } - + public string AltText { get { return image.getAltText().orElse(null); } } public string ContentType { get { return image.getContentType(); } } public Stream GetStream() { return image.getInputStream().Stream; } -- cgit v1.2.3