From ce18ce3e229448ed1b0e3151b582cfdfd736144f Mon Sep 17 00:00:00 2001 From: James Ward Date: Sun, 13 Sep 2026 11:26:34 -0400 Subject: [PATCH] fix(epub): handle literal `+` when decoding URLs (#2618) --- .../metadata/writer/EpubMetadataWriter.java | 6 ++ .../writer/EpubMetadataWriterTest.java | 62 +++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/backend/src/main/java/org/booklore/service/metadata/writer/EpubMetadataWriter.java b/backend/src/main/java/org/booklore/service/metadata/writer/EpubMetadataWriter.java index b9f5a7a27..7e44cf165 100644 --- a/backend/src/main/java/org/booklore/service/metadata/writer/EpubMetadataWriter.java +++ b/backend/src/main/java/org/booklore/service/metadata/writer/EpubMetadataWriter.java @@ -494,6 +494,12 @@ public class EpubMetadataWriter implements MetadataWriter { private Path getManifestItemPath(Path tempDir, Path opfDir, Element element) throws IOException { String href = element.getAttribute("href"); + + // Technically, epub specification only refers to `href` as supporting percent-encoding. + // Unfortunately, the Java URLDecoder.decode method will also do `+` -> space decoding. + // To work around this, we can replace `+` with the percent-encoded version of a plus. + href = href.replaceAll("\\+", "%2b"); + String decodedHref = URLDecoder.decode(href, StandardCharsets.UTF_8); if (decodedHref == null || decodedHref.isBlank()) { throw new IOException("Manifest item has no href attribute"); diff --git a/backend/src/test/java/org/booklore/service/metadata/writer/EpubMetadataWriterTest.java b/backend/src/test/java/org/booklore/service/metadata/writer/EpubMetadataWriterTest.java index b5487db49..8401f910c 100644 --- a/backend/src/test/java/org/booklore/service/metadata/writer/EpubMetadataWriterTest.java +++ b/backend/src/test/java/org/booklore/service/metadata/writer/EpubMetadataWriterTest.java @@ -518,6 +518,68 @@ class EpubMetadataWriterTest { assertThat(mimetypeCount).isEqualTo(1); } } + + @Test + @DisplayName("Should handle file names with a literal plus sign") + void saveMetadata_handlesPlus() throws Exception { + String opfContent = """ + + + + Original Title + + + + + + """; + + byte[] coverImageA = new byte[]{0x0A}; + byte[] coverImageB = new byte[]{0x0B}; + + File coverImageFile = tempDir.resolve("new-cover-" + System.nanoTime() + ".bin").toFile(); + Files.write(coverImageFile.toPath(), coverImageB); + + File epubFile = tempDir.resolve("test-duplicate-" + System.nanoTime() + ".epub").toFile(); + + try (var zos = new ZipArchiveOutputStream(new FileOutputStream(epubFile))) { + String containerXml = """ + + + + + + + """; + + zos.putArchiveEntry(new ZipArchiveEntry("mimetype")); + zos.write("application/epub+zip".getBytes(StandardCharsets.UTF_8)); + zos.closeArchiveEntry(); + + zos.putArchiveEntry(new ZipArchiveEntry("META-INF/container.xml")); + zos.write(containerXml.getBytes(StandardCharsets.UTF_8)); + zos.closeArchiveEntry(); + + zos.putArchiveEntry(new ZipArchiveEntry("content.opf")); + zos.write(opfContent.getBytes(StandardCharsets.UTF_8)); + zos.closeArchiveEntry(); + + zos.putArchiveEntry(new ZipArchiveEntry("cover+example.bin")); + zos.write(coverImageA); + zos.closeArchiveEntry(); + } + + writer.saveMetadataToFile(epubFile, metadata, coverImageFile.toString(), new MetadataClearFlags()); + + try (ZipFile zf = new ZipFile(epubFile)) { + var entry = zf.getEntry("cover+example.bin"); + + try (InputStream is = zf.getInputStream(entry)) { + assertThat(is.readAllBytes()).isEqualTo(coverImageB); + } + } + } + } @Nested