Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set FEATURE_SECURE_PROCESSING for DocumentBuilderFactory #1489

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand All @@ -36,6 +37,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import static org.verapdf.metadata.fixer.utils.MetadataFixerConstants.*;

Expand All @@ -44,6 +47,7 @@
*/
public class XMLProcessedObjectsParser implements ProcessedObjectsParser {

private static final Logger LOGGER = Logger.getLogger(XMLProcessedObjectsParser.class.getCanonicalName());
private static final String XML_PROCESSED_OBJECTS_PATH_PROPERTY_PDFA_1 = "processed.objects.path.pdfa_1";
private static final String XML_PROCESSED_OBJECTS_PATH_PROPERTY_PDFA_2_3 = "processed.objects.path.pdfa_2_3";
private static final String XML_PROCESSED_OBJECTS_PATH_PROPERTY_PDFA_4 = "processed.objects.path.pdfa_4";
Expand Down Expand Up @@ -83,7 +87,11 @@ public ProcessedObjects getProcessedObjects(String path)
public ProcessedObjects getProcessedObjects(InputStream xml)
throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

try {
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Unable to secure xml processing");
}
DocumentBuilder builder = factory.newDocumentBuilder();

factory.setIgnoringElementContentWhitespace(true);
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/java/org/verapdf/report/XmpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
import javax.xml.bind.DatatypeConverter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Class that's initially a placeholder for XMP specific functionality.
Expand All @@ -51,6 +54,7 @@
* @author <a href="mailto:carl@openpreservation.org">Carl Wilson</a>
*/
public class XmpHandler {
private static final Logger LOGGER = Logger.getLogger(XmpHandler.class.getCanonicalName());
private static final byte[] UTF8_METADATA_PREFIX_SQ = {0x3C, 0x3F, 0x78,
0x70, 0x61, 0x63, 0x6B, 0x65, 0x74, 0x20, 0x62, 0x65, 0x67, 0x69,
0x6E, 0x3D, 0x27, -0x11, -0x45, -0x41, 0x27};
Expand Down Expand Up @@ -134,6 +138,11 @@ public static Node parseMetadataRootElement(FeatureTreeNode metadataNode)
return null;
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Unable to secure metadata processing");
}
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document metadataDocument = builder.parse(is);
Expand Down
Loading