fix: validate OPF cover items as images (#2062)

This commit is contained in:
Leopoldo Pla Sempere
2026-05-25 16:40:41 -04:00
committed by GitHub
parent e3298c6e43
commit f69650fb86
3 changed files with 68 additions and 14 deletions
+3 -2
View File
@@ -104,8 +104,9 @@ bool Epub::parseContentOpf(BookMetadataCache::BookMetadata& bookMetadata) {
const auto endPos = coverPageHtml.find('"', pos);
if (endPos != std::string::npos) {
const auto ref = std::string_view{coverPageHtml}.substr(pos, endPos - pos);
// Check if it's an image file
if (FsHelpers::hasPngExtension(ref) || FsHelpers::hasJpgExtension(ref) || FsHelpers::hasGifExtension(ref)) {
// Cover BMP generation supports JPG/PNG only; skip GIF so an unsupported wrapper image
// does not block a later supported cover reference.
if (FsHelpers::hasPngExtension(ref) || FsHelpers::hasJpgExtension(ref)) {
imageRef = ref;
break;
}
+27 -1
View File
@@ -5,12 +5,31 @@
#include <Serialization.h>
#include <XmlParserUtils.h>
#include <cctype>
#include "Epub/BookMetadataCache.h"
namespace {
constexpr char MEDIA_TYPE_NCX[] = "application/x-dtbncx+xml";
constexpr char MEDIA_TYPE_CSS[] = "text/css";
constexpr char MEDIA_TYPE_IMAGE_PREFIX[] = "image/";
constexpr char itemCacheFile[] = "/.items.bin";
bool startsWithImageMediaType(const std::string& mediaType) {
constexpr size_t prefixLen = sizeof(MEDIA_TYPE_IMAGE_PREFIX) - 1;
if (mediaType.size() < prefixLen) {
return false;
}
for (size_t i = 0; i < prefixLen; ++i) {
const char c = static_cast<char>(std::tolower(static_cast<unsigned char>(mediaType[i])));
if (c != MEDIA_TYPE_IMAGE_PREFIX[i]) {
return false;
}
}
return true;
}
} // namespace
bool ContentOpfParser::setup() {
@@ -189,7 +208,14 @@ void XMLCALL ContentOpfParser::startElement(void* userData, const XML_Char* name
serialization::writeString(self->tempItemStore, href);
if (itemId == self->coverItemId) {
self->coverItemHref = href;
// Some EPUBs set meta name="cover" to an XHTML wrapper item.
// Only treat it as a cover image when the manifest media-type is image/*.
if (startsWithImageMediaType(mediaType)) {
self->coverItemHref = href;
} else {
LOG_DBG("COF", "Ignoring meta cover item '%s' with non-image media type: %s", itemId.c_str(),
mediaType.c_str());
}
}
if (mediaType == MEDIA_TYPE_NCX) {