Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

fix: confine ebook resources to package

Stamina94481583a7c92
diff --git a/src/Filee.Engines/Ebooks/ChapterReader.cs b/src/Filee.Engines/Ebooks/ChapterReader.cs
index 34230d2b..b66e92c7 100644
--- a/src/Filee.Engines/Ebooks/ChapterReader.cs
+++ b/src/Filee.Engines/Ebooks/ChapterReader.cs
@@ -29,6 +29,7 @@ internal static class ChapterReader
{
BaseFolder = Path.GetDirectoryName(chapter)!,
MediaFolder = mediaFolder,
+ ResourceRoot = Path.GetDirectoryName(mediaFolder)!,
MapId = id => Anchor(number, id),
MapLink = href => Link(href, chapter, number, index),
});
diff --git a/src/Filee.Engines/Hwp/Hwpx/HtmlReader.cs b/src/Filee.Engines/Hwp/Hwpx/HtmlReader.cs
index 7e2383ad..7d76e0a3 100644
--- a/src/Filee.Engines/Hwp/Hwpx/HtmlReader.cs
+++ b/src/Filee.Engines/Hwp/Hwpx/HtmlReader.cs
@@ -27,6 +27,9 @@ internal sealed class HtmlReadOptions
/// <summary>Folder that receives images embedded as data: URIs (created on demand).</summary>
public required string MediaFolder { get; init; }
+ /// <summary>When set, local images and style sheets must be inside this folder (e.g. an unpacked e-book).</summary>
+ public string? ResourceRoot { get; init; }
+
/// <summary>Maps an href to the link target to write, or null to keep only the link text. Default: unchanged.</summary>
public Func<string, string?>? MapLink { get; init; }
@@ -1058,7 +1061,7 @@ internal sealed partial class HtmlReader
try
{
if (reference.StartsWith("file:", StringComparison.OrdinalIgnoreCase))
- return new Uri(reference).LocalPath is var local && File.Exists(local) ? local : null;
+ return new Uri(reference).LocalPath is var local && AllowedResource(local) && File.Exists(local) ? local : null;
if (reference.Contains("://", StringComparison.Ordinal) || reference.StartsWith("//", StringComparison.Ordinal)
|| reference.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase))
return null;
@@ -1067,7 +1070,7 @@ internal sealed partial class HtmlReader
if (relative.Length == 0)
return null;
var candidate = Path.GetFullPath(Path.Combine(_options.BaseFolder, relative));
- return File.Exists(candidate) ? candidate : null;
+ return AllowedResource(candidate) && File.Exists(candidate) ? candidate : null;
}
catch (Exception ex) when (ex is ArgumentException or UriFormatException or NotSupportedException or PathTooLongException)
{
@@ -1075,6 +1078,16 @@ internal sealed partial class HtmlReader
}
}
+ /// <summary>Packaged books may only refer to files in their own unpacked folder.</summary>
+ private bool AllowedResource(string path)
+ {
+ if (_options.ResourceRoot is null)
+ return true;
+ var root = Path.TrimEndingDirectorySeparator(Path.GetFullPath(_options.ResourceRoot)) + Path.DirectorySeparatorChar;
+ var comparison = OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
+ return Path.GetFullPath(path).StartsWith(root, comparison);
+ }
+
/// <summary>Writes the image of a data: URI to the media folder.</summary>
private string? SaveDataUri(string uri)
{
diff --git a/tests/Filee.Engines.Tests/EbookTests.cs b/tests/Filee.Engines.Tests/EbookTests.cs
index f8339011..89a4c221 100644
--- a/tests/Filee.Engines.Tests/EbookTests.cs
+++ b/tests/Filee.Engines.Tests/EbookTests.cs
@@ -116,6 +116,28 @@ public class EbookTests(EngineFixture fx) : IClassFixture<EngineFixture>
// ───────────────────────── EPUB ─────────────────────────
+ [Fact]
+ public void Book_chapters_only_include_images_from_the_unpacked_book()
+ {
+ var folder = fx.NewFolder();
+ var book = Path.Combine(folder, "book");
+ var chapterFolder = Path.Combine(book, "Text");
+ var imageFolder = Path.Combine(book, "Images");
+ Directory.CreateDirectory(chapterFolder);
+ Directory.CreateDirectory(imageFolder);
+ var inside = Path.Combine(imageFolder, "inside.png");
+ var outside = Path.Combine(folder, "private.png");
+ File.WriteAllBytes(inside, EbookBuilders.Png(4, 4, MagickColors.Teal));
+ File.WriteAllBytes(outside, EbookBuilders.Png(4, 4, MagickColors.Red));
+ var chapter = Path.Combine(chapterFolder, "chapter.xhtml");
+ File.WriteAllText(chapter, $"""
+ <html><body><p><img src="../Images/inside.png"><img src="../../private.png"><img src="{new Uri(outside).AbsoluteUri}"></p></body></html>
+ """);
+
+ var document = ChapterReader.Read([chapter], Path.Combine(book, "~media"), TestContext.Current.CancellationToken);
+ Assert.Equal(inside, Assert.Single(Hwp.Hwpx.HDocumentWalker.Images(document)).Path);
+ }
+
[Fact]
public void Epub_reader_keeps_chapters_links_pictures_and_metadata()
{