diff --git a/src/main/java/com/adobe/epubcheck/messages/DefaultSeverities.java b/src/main/java/com/adobe/epubcheck/messages/DefaultSeverities.java index af871ac41..b9c6629dd 100644 --- a/src/main/java/com/adobe/epubcheck/messages/DefaultSeverities.java +++ b/src/main/java/com/adobe/epubcheck/messages/DefaultSeverities.java @@ -302,6 +302,7 @@ private void initialize() severities.put(MessageId.PKG_023, Severity.USAGE); severities.put(MessageId.PKG_024, Severity.INFO); severities.put(MessageId.PKG_025, Severity.ERROR); + severities.put(MessageId.PKG_026, Severity.ERROR); // Resources severities.put(MessageId.RSC_001, Severity.ERROR); diff --git a/src/main/java/com/adobe/epubcheck/messages/MessageId.java b/src/main/java/com/adobe/epubcheck/messages/MessageId.java index 06fb120aa..040920da4 100644 --- a/src/main/java/com/adobe/epubcheck/messages/MessageId.java +++ b/src/main/java/com/adobe/epubcheck/messages/MessageId.java @@ -296,6 +296,7 @@ public enum MessageId implements Comparable PKG_023("PKG-023"), PKG_024("PKG-024"), PKG_025("PKG-025"), + PKG_026("PKG-026"), // Messages relating to resources RSC_001("RSC-001"), diff --git a/src/main/java/com/adobe/epubcheck/ocf/EncryptionHandler.java b/src/main/java/com/adobe/epubcheck/ocf/EncryptionHandler.java index a92caadf5..648e17da2 100644 --- a/src/main/java/com/adobe/epubcheck/ocf/EncryptionHandler.java +++ b/src/main/java/com/adobe/epubcheck/ocf/EncryptionHandler.java @@ -22,14 +22,15 @@ package com.adobe.epubcheck.ocf; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; + +import com.adobe.epubcheck.api.EPUBLocation; import com.adobe.epubcheck.util.HandlerUtil; import com.adobe.epubcheck.xml.XMLElement; import com.adobe.epubcheck.xml.XMLHandler; import com.adobe.epubcheck.xml.XMLParser; -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; - public class EncryptionHandler implements XMLHandler { private final OCFPackage ocf; @@ -83,6 +84,7 @@ public void startElement() if (algorithm.equals("http://www.idpf.org/2008/embedding")) { ocf.setEncryption(entryName, new IDPFFontManglingFilter(null)); + ocf.setObfuscated(entryName, parser.getLocation()); } else if (algorithm.equals("http://ns.adobe.com/pdf/enc#RC")) { diff --git a/src/main/java/com/adobe/epubcheck/ocf/OCFChecker.java b/src/main/java/com/adobe/epubcheck/ocf/OCFChecker.java index b5639cec1..836d217fb 100755 --- a/src/main/java/com/adobe/epubcheck/ocf/OCFChecker.java +++ b/src/main/java/com/adobe/epubcheck/ocf/OCFChecker.java @@ -51,9 +51,11 @@ import com.adobe.epubcheck.api.Report; import com.adobe.epubcheck.messages.MessageId; import com.adobe.epubcheck.opf.OPFChecker; +import com.adobe.epubcheck.opf.OPFChecker30; import com.adobe.epubcheck.opf.OPFData; import com.adobe.epubcheck.opf.OPFHandler; import com.adobe.epubcheck.opf.OPFHandler30; +import com.adobe.epubcheck.opf.OPFItem; import com.adobe.epubcheck.opf.ValidationContext; import com.adobe.epubcheck.opf.ValidationContext.ValidationContextBuilder; import com.adobe.epubcheck.util.CheckUtil; @@ -64,6 +66,7 @@ import com.adobe.epubcheck.xml.XMLParser; import com.adobe.epubcheck.xml.XMLValidator; import com.adobe.epubcheck.xml.XMLValidators; +import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.base.Predicate; import com.google.common.base.Predicates; @@ -349,7 +352,7 @@ else if (!normalizedEntriesSet.add(Normalizer.normalize(entry, Form.NFC))) for (final String entry : ocf.getFileEntries()) { ocf.reportMetadata(entry, report); - + // if the entry is not in the whitelist (META-INF/* + mimetype) // and not declared in (one of) the OPF document(s) if (!entry.startsWith("META-INF/") && !entry.startsWith("META-INF\\") @@ -371,6 +374,27 @@ public boolean apply(OPFHandler opfHandler) report.message(MessageId.OPF_003, EPUBLocation.create(ocf.getName()), entry); } OCFFilenameChecker.checkCompatiblyEscaped(entry, report, validationVersion); + + // check obfuscated resource are Font Core Media Types + if (ocf.isObfuscatedFont(entry)) + { + for (OPFHandler opf : opfHandlers) + { + // try to find the first Package Document where the entry is + // declared + Optional item = opf.getItemByPath(entry); + if (item.isPresent()) + { + // report if it is not a font core media type + if (!OPFChecker30.isBlessedFontType(item.get().getMimeType())) + { + report.message(MessageId.PKG_026, ocf.getObfuscationDeclarationLocation(entry), + item.get().getMimeType(), opf.getPath()); + } + break; + } + } + } } // check all directory entries without duplicates diff --git a/src/main/java/com/adobe/epubcheck/ocf/OCFPackage.java b/src/main/java/com/adobe/epubcheck/ocf/OCFPackage.java index 6821d90af..5747ca203 100644 --- a/src/main/java/com/adobe/epubcheck/ocf/OCFPackage.java +++ b/src/main/java/com/adobe/epubcheck/ocf/OCFPackage.java @@ -4,7 +4,6 @@ import java.io.InputStream; import java.util.Collections; import java.util.HashMap; -import java.util.Hashtable; import java.util.List; import java.util.Map; import java.util.Set; @@ -26,7 +25,8 @@ public abstract class OCFPackage implements GenericResourceProvider { - final Hashtable enc; + final Map enc = new HashMap<>(); + final Map obfuscated = new HashMap<>(); String uniqueIdentifier; private Report reporter; private final Supplier ocfData = Suppliers.memoize(new Supplier() @@ -72,16 +72,17 @@ public Map get() } }); - public OCFPackage() - { - this.enc = new Hashtable(); - } public void setEncryption(String name, EncryptionFilter encryptionFilter) { enc.put(name, encryptionFilter); } + public void setObfuscated(String name, EPUBLocation location) + { + obfuscated.put(name, location); + } + /** * @param name * the name of a relative file that is possibly in the container @@ -133,6 +134,27 @@ public boolean canDecrypt(String fileName) return filter == null || filter.canDecrypt(); } + /** + * @param path path of the resource to test + * @return true if that resource is encrypted with the font obfuscation algorithm + */ + public boolean isObfuscatedFont(String path) + { + return obfuscated.containsKey(path); + } + + /** + * Returns the location in META-INF/encryption.xml where the given resource + * is declared as an obfuscated font, or null if it is not. + * + * @param path path of the resource to test + * @return the location in META-INF/encryption.xml, or null + */ + public EPUBLocation getObfuscationDeclarationLocation(String path) + { + return obfuscated.get(path); + } + /** * This method parses the container entry and stores important data, but does * /not/ validate the container against a schema definition. diff --git a/src/main/java/com/adobe/epubcheck/opf/OPFHandler.java b/src/main/java/com/adobe/epubcheck/opf/OPFHandler.java index a246236fb..8630119e7 100755 --- a/src/main/java/com/adobe/epubcheck/opf/OPFHandler.java +++ b/src/main/java/com/adobe/epubcheck/opf/OPFHandler.java @@ -127,6 +127,10 @@ public OPFHandler(ValidationContext context, XMLParser parser) this.report = context.report; this.parser = parser; } + + public String getPath() { + return path; + } public boolean getOpf12PackageFile() { diff --git a/src/main/resources/com/adobe/epubcheck/messages/MessageBundle.properties b/src/main/resources/com/adobe/epubcheck/messages/MessageBundle.properties index 6b7fd61db..623cf516b 100644 --- a/src/main/resources/com/adobe/epubcheck/messages/MessageBundle.properties +++ b/src/main/resources/com/adobe/epubcheck/messages/MessageBundle.properties @@ -313,7 +313,8 @@ PKG_022=Wrong file extension for image. The image is a "%1$s" file but has the f PKG_023=Validating the EPUB against version 2.0, default validation profile will be used. PKG_024=Uncommon EPUB file extension. PKG_024_SUG=For maximum compatibility, use ".epub". -PKG_025=Publication resource must not be located in the META-INF directory +PKG_025=Publication resource must not be located in the META-INF directory +PKG_026=Obfuscated resource must be a Font Core Media Type (was declared as "%1$s" in "%2$s"). #Resources RSC_001=File "%1$s" could not be found. diff --git a/src/test/resources/epub3/container-publication.feature b/src/test/resources/epub3/container-publication.feature index 1f27ad886..599ecf483 100644 --- a/src/test/resources/epub3/container-publication.feature +++ b/src/test/resources/epub3/container-publication.feature @@ -119,9 +119,10 @@ Feature: EPUB 3 ▸ Open Container Format ▸ Full Publication Checks And the message contains 'expected element "encryption"' And no other errors or warnings are reported - Scenario: Report an unknown encryption scheme + Scenario: Verify encryption can be used + (but file will not be parsed) Given the reporting level is set to INFO - When checking EPUB 'ocf-encryption-unknown-error' + When checking EPUB 'ocf-encryption-unknown-valid' Then info RSC-004 is reported And no other errors or warnings are reported @@ -180,16 +181,21 @@ Feature: EPUB 3 ▸ Open Container Format ▸ Full Publication Checks ## 5. Resource Obfuscation - - Scenario: Verify a publication with obfuscated resource (here a font file) + + Scenario: Verify a publication with obfuscated font When checking EPUB 'ocf-obfuscation-valid' Then no errors or warnings are reported - Scenario: Verify a publication with obfuscation of a usually-parsed resource (here an SVG image) - When checking EPUB 'ocf-obfuscation-svg-valid' - Then info RSC-004 is reported + Scenario: Report an obfuscated font that is not a Core Media Type + When checking EPUB 'ocf-obfuscation-not-cmt-error' + Then error PKG-026 is reported And no errors or warnings are reported - + + Scenario: Report an obfuscated font that is not a font + When checking EPUB 'ocf-obfuscation-not-font-error' + Then error PKG-026 is reported + And no errors or warnings are reported + ## C. the 'application/epub+zip' Media Type diff --git a/src/test/resources/epub3/files/epub/ocf-encryption-unknown-error/EPUB/content_001.xhtml b/src/test/resources/epub3/files/epub/ocf-encryption-unknown-valid/EPUB/content_001.xhtml similarity index 100% rename from src/test/resources/epub3/files/epub/ocf-encryption-unknown-error/EPUB/content_001.xhtml rename to src/test/resources/epub3/files/epub/ocf-encryption-unknown-valid/EPUB/content_001.xhtml diff --git a/src/test/resources/epub3/files/epub/ocf-encryption-unknown-error/EPUB/nav.xhtml b/src/test/resources/epub3/files/epub/ocf-encryption-unknown-valid/EPUB/nav.xhtml similarity index 100% rename from src/test/resources/epub3/files/epub/ocf-encryption-unknown-error/EPUB/nav.xhtml rename to src/test/resources/epub3/files/epub/ocf-encryption-unknown-valid/EPUB/nav.xhtml diff --git a/src/test/resources/epub3/files/epub/ocf-encryption-unknown-error/EPUB/package.opf b/src/test/resources/epub3/files/epub/ocf-encryption-unknown-valid/EPUB/package.opf similarity index 100% rename from src/test/resources/epub3/files/epub/ocf-encryption-unknown-error/EPUB/package.opf rename to src/test/resources/epub3/files/epub/ocf-encryption-unknown-valid/EPUB/package.opf diff --git a/src/test/resources/epub3/files/epub/ocf-encryption-unknown-error/META-INF/container.xml b/src/test/resources/epub3/files/epub/ocf-encryption-unknown-valid/META-INF/container.xml similarity index 100% rename from src/test/resources/epub3/files/epub/ocf-encryption-unknown-error/META-INF/container.xml rename to src/test/resources/epub3/files/epub/ocf-encryption-unknown-valid/META-INF/container.xml diff --git a/src/test/resources/epub3/files/epub/ocf-encryption-unknown-error/META-INF/encryption.xml b/src/test/resources/epub3/files/epub/ocf-encryption-unknown-valid/META-INF/encryption.xml similarity index 100% rename from src/test/resources/epub3/files/epub/ocf-encryption-unknown-error/META-INF/encryption.xml rename to src/test/resources/epub3/files/epub/ocf-encryption-unknown-valid/META-INF/encryption.xml diff --git a/src/test/resources/epub3/files/epub/ocf-encryption-unknown-error/mimetype b/src/test/resources/epub3/files/epub/ocf-encryption-unknown-valid/mimetype similarity index 100% rename from src/test/resources/epub3/files/epub/ocf-encryption-unknown-error/mimetype rename to src/test/resources/epub3/files/epub/ocf-encryption-unknown-valid/mimetype diff --git a/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/EPUB/content_001.xhtml b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/EPUB/content_001.xhtml new file mode 100644 index 000000000..ea29a1610 --- /dev/null +++ b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/EPUB/content_001.xhtml @@ -0,0 +1,11 @@ + + + + + Minimal EPUB + + +

Loomings

+

Call me Ishmael.

+ + diff --git a/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/EPUB/nav.xhtml b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/EPUB/nav.xhtml new file mode 100644 index 000000000..240745e63 --- /dev/null +++ b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/EPUB/nav.xhtml @@ -0,0 +1,14 @@ + + + + + Minimal Nav + + + + + diff --git a/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/EPUB/obfuscated-font.otf b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/EPUB/obfuscated-font.otf new file mode 100755 index 000000000..18ac1fcd7 Binary files /dev/null and b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/EPUB/obfuscated-font.otf differ diff --git a/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/EPUB/package.opf b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/EPUB/package.opf new file mode 100644 index 000000000..4d1038448 --- /dev/null +++ b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/EPUB/package.opf @@ -0,0 +1,17 @@ + + + + Minimal EPUB 3.0 + en + NOID + 2017-06-14T00:00:01Z + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/META-INF/container.xml b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/META-INF/container.xml new file mode 100644 index 000000000..318782179 --- /dev/null +++ b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/META-INF/container.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/META-INF/encryption.xml b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/META-INF/encryption.xml new file mode 100644 index 000000000..3764b5474 --- /dev/null +++ b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/META-INF/encryption.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/mimetype b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/mimetype similarity index 100% rename from src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/mimetype rename to src/test/resources/epub3/files/epub/ocf-obfuscation-not-cmt-error/mimetype diff --git a/src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/EPUB/content_001.xhtml b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/EPUB/content_001.xhtml similarity index 86% rename from src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/EPUB/content_001.xhtml rename to src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/EPUB/content_001.xhtml index 601da6e51..2f8492a6f 100644 --- a/src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/EPUB/content_001.xhtml +++ b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/EPUB/content_001.xhtml @@ -7,6 +7,5 @@

Loomings

Call me Ishmael.

- a smiling emoji diff --git a/src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/EPUB/emoji.svg b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/EPUB/doc.xml similarity index 100% rename from src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/EPUB/emoji.svg rename to src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/EPUB/doc.xml diff --git a/src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/EPUB/nav.xhtml b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/EPUB/nav.xhtml similarity index 100% rename from src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/EPUB/nav.xhtml rename to src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/EPUB/nav.xhtml diff --git a/src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/EPUB/package.opf b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/EPUB/package.opf similarity index 90% rename from src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/EPUB/package.opf rename to src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/EPUB/package.opf index 9f4ba40d7..fb40ce83f 100644 --- a/src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/EPUB/package.opf +++ b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/EPUB/package.opf @@ -9,7 +9,7 @@ - + diff --git a/src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/META-INF/container.xml b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/META-INF/container.xml similarity index 100% rename from src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/META-INF/container.xml rename to src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/META-INF/container.xml diff --git a/src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/META-INF/encryption.xml b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/META-INF/encryption.xml similarity index 86% rename from src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/META-INF/encryption.xml rename to src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/META-INF/encryption.xml index d172242b5..b25f63d02 100644 --- a/src/test/resources/epub3/files/epub/ocf-obfuscation-svg-valid/META-INF/encryption.xml +++ b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/META-INF/encryption.xml @@ -4,7 +4,7 @@ - + diff --git a/src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/mimetype b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/mimetype new file mode 100644 index 000000000..57ef03f24 --- /dev/null +++ b/src/test/resources/epub3/files/epub/ocf-obfuscation-not-font-error/mimetype @@ -0,0 +1 @@ +application/epub+zip \ No newline at end of file diff --git a/src/test/resources/epub3/files/epub/ocf-obfuscation-valid/EPUB/package.opf b/src/test/resources/epub3/files/epub/ocf-obfuscation-valid/EPUB/package.opf index 4d1038448..d0cdd1fbd 100644 --- a/src/test/resources/epub3/files/epub/ocf-obfuscation-valid/EPUB/package.opf +++ b/src/test/resources/epub3/files/epub/ocf-obfuscation-valid/EPUB/package.opf @@ -9,7 +9,7 @@ - +