Skip to content

Commit 2c76420

Browse files
committed
feat: check that the manifest is not self-referencing
This commit adds a new check, reported as `OPF-099` (error), to verify that the package document `manifest` does not include an `item` element that refers to the package document itself. This statement was apparently not checked by EPUBCheck previously. Worse, the code was entering an infinite loop, as the package document checker was creating a new checker for itself, recursively. Fix #1453
1 parent 54b5f1f commit 2c76420

File tree

11 files changed

+93
-0
lines changed

11 files changed

+93
-0
lines changed

src/main/java/com/adobe/epubcheck/messages/DefaultSeverities.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ private void initialize()
293293
severities.put(MessageId.OPF_096b, Severity.USAGE);
294294
severities.put(MessageId.OPF_097, Severity.USAGE);
295295
severities.put(MessageId.OPF_098, Severity.ERROR);
296+
severities.put(MessageId.OPF_099, Severity.ERROR);
296297

297298
// PKG
298299
severities.put(MessageId.PKG_001, Severity.WARNING);

src/main/java/com/adobe/epubcheck/messages/MessageId.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ public enum MessageId implements Comparable<MessageId>
287287
OPF_096b("OPF-096b"),
288288
OPF_097("OPF-097"),
289289
OPF_098("OPF-098"),
290+
OPF_099("OPF-099"),
290291

291292
// Messages relating to the entire package
292293
PKG_001("PKG-001"),

src/main/java/com/adobe/epubcheck/opf/OPFChecker.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.w3c.epubcheck.core.Checker;
3636
import org.w3c.epubcheck.core.CheckerFactory;
3737
import org.w3c.epubcheck.core.references.ResourceReferencesChecker;
38+
import org.w3c.epubcheck.util.url.URLUtils;
3839

3940
import com.adobe.epubcheck.api.EPUBLocation;
4041
import com.adobe.epubcheck.api.EPUBProfile;
@@ -215,6 +216,11 @@ protected boolean checkContent()
215216
// FIXME 2022 check duplicates at build time (in OPFHandler)
216217
report.message(MessageId.OPF_074, item.getLocation(), item.getPath());
217218
}
219+
// check that the manifest does not include the package document itself
220+
else if (item.getURL().equals(context.url))
221+
{
222+
report.message(MessageId.OPF_099, item.getLocation());
223+
}
218224
else
219225
{
220226
checkItem(item, opfHandler);
@@ -361,6 +367,12 @@ protected void checkItemContent(OPFItem item)
361367
{
362368
return;
363369
}
370+
// Abort if the item is this package document
371+
// (this is disallowed, and reported elsewhere)
372+
if (URLUtils.docURL(item.getURL()).equals(context.url))
373+
{
374+
return;
375+
}
364376
// Create a new validation context for the OPF item
365377
// FIXME 2022 set context OPFItem here
366378
// (instead of from XRefChecker in the builder code)

src/main/resources/com/adobe/epubcheck/messages/MessageBundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ OPF_096=Non-linear content must be reachable, but found no hyperlink to "%1$s".
215215
OPF_096b=No hyperlink was found to non-linear document "%1$s", please check that it can be reached from scripted content.
216216
OPF_097=Resource "%1$s" is listed in the manifest, but no reference to it was found in content documents.
217217
OPF_098=The "href" attribute must reference resources, not elements in the package document, but found URL "%1$s".
218+
OPF_099=The manifest must not list the package document.
218219

219220
#Package
220221
PKG_001=Validating the EPUB against version %1$s but detected version %2$s.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" xml:lang="en" unique-identifier="q">
3+
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
4+
<dc:title id="title">Minimal EPUB 3.0</dc:title>
5+
<dc:language>en</dc:language>
6+
<dc:identifier id="q">NOID</dc:identifier>
7+
<meta property="dcterms:modified">2017-06-14T00:00:01Z</meta>
8+
</metadata>
9+
<manifest>
10+
<item id="self" href="./manifest-self-referencing-error.opf" media-type="application/oebps-package+xml"/>
11+
<item id="content_001" href="content_001.xhtml" media-type="application/xhtml+xml"/>
12+
<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
13+
</manifest>
14+
<spine>
15+
<itemref idref="content_001"/>
16+
</spine>
17+
</package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html xmlns:epub="http://www.idpf.org/2007/ops" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3+
<head>
4+
<meta charset="utf-8"/>
5+
<title>Minimal EPUB</title>
6+
</head>
7+
<body>
8+
<h1>Loomings</h1>
9+
<p>Call me Ishmael.</p>
10+
</body>
11+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en" lang="en">
3+
<head>
4+
<meta charset="utf-8"/>
5+
<title>Minimal Nav</title>
6+
</head>
7+
<body>
8+
<nav epub:type="toc">
9+
<ol>
10+
<li><a href="content_001.xhtml">content 001</a></li>
11+
</ol>
12+
</nav>
13+
</body>
14+
</html>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" xml:lang="en" unique-identifier="q">
3+
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
4+
<dc:title id="title">Minimal EPUB 3.0</dc:title>
5+
<dc:language>en</dc:language>
6+
<dc:identifier id="q">NOID</dc:identifier>
7+
<meta property="dcterms:modified">2017-06-14T00:00:01Z</meta>
8+
</metadata>
9+
<manifest>
10+
<item id="self" href="./package.opf" media-type="application/oebps-package+xml"/>
11+
<item id="content_001" href="content_001.xhtml" media-type="application/xhtml+xml"/>
12+
<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
13+
</manifest>
14+
<spine>
15+
<itemref idref="content_001"/>
16+
</spine>
17+
</package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
3+
<rootfiles>
4+
<rootfile full-path="EPUB/package.opf" media-type="application/oebps-package+xml"/>
5+
</rootfiles>
6+
</container>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
application/epub+zip

0 commit comments

Comments
 (0)