Skip to content

Commit 1039740

Browse files
committed
PDFBOX-6125: avoid ClassCastException if structured type is used as an attribute
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1930623 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4f9c82f commit 1039740

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

xmpbox/src/main/java/org/apache/xmpbox/xml/DomXmpParser.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,20 @@ private void parseDescriptionRootAttr(XMPMetadata xmp, Element description, Attr
324324
type = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
325325
}
326326
}
327+
else if (!type.type().isSimple())
328+
{
329+
if (strictParsing)
330+
{
331+
throw new XmpParsingException(ErrorType.InvalidType, "The type '" +
332+
type.type().name() + "' in '" + attr.getPrefix() + ":" + attr.getLocalName() + "=" + attr.getValue()
333+
+ "' is a structured type, but attributes are simple types");
334+
}
335+
else
336+
{
337+
// PDFBOX-6125: Default to text
338+
type = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
339+
}
340+
}
327341

328342
try
329343
{
@@ -1091,6 +1105,20 @@ else if (XmpConstants.DEFAULT_RDF_PREFIX.equals(attr.getPrefix()))
10911105
type = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
10921106
}
10931107
}
1108+
else if (!type.type().isSimple())
1109+
{
1110+
if (strictParsing)
1111+
{
1112+
throw new XmpParsingException(ErrorType.InvalidType, "The type '" +
1113+
type.type().name() + "' in '" + attr.getPrefix() + ":" + attr.getLocalName() + "=" + attr.getValue()
1114+
+ "' is a structured type, but attributes are simple types");
1115+
}
1116+
else
1117+
{
1118+
// PDFBOX-6125: Default to text
1119+
type = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
1120+
}
1121+
}
10941122
AbstractSimpleProperty asp = tm.instanciateSimpleProperty(
10951123
attr.getNamespaceURI(), attr.getPrefix(), attr.getLocalName(),
10961124
attr.getValue(), type.type());

xmpbox/src/test/java/org/apache/xmpbox/xml/DomXmpParserTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.xmpbox.XMPMetadata;
3131
import org.apache.xmpbox.schema.AdobePDFSchema;
3232
import org.apache.xmpbox.schema.DublinCoreSchema;
33+
import org.apache.xmpbox.schema.ExifSchema;
3334
import org.apache.xmpbox.schema.PDFAIdentificationSchema;
3435
import org.apache.xmpbox.schema.PhotoshopSchema;
3536
import org.apache.xmpbox.schema.XMPMediaManagementSchema;
@@ -727,4 +728,37 @@ void testPropertyNotDefined() throws XmpParsingException
727728
() -> xmpParser.parse(s.getBytes(StandardCharsets.UTF_8)));
728729
assertEquals("Property 'Fired' not defined in http://ns.adobe.com/exif/1.0/", ex.getMessage());
729730
}
731+
732+
@Test
733+
void testBadAttr2() throws XmpParsingException
734+
{
735+
// File from image on page 14 from file 006054.pdf
736+
// exif:Flash is a structured type.
737+
// However the PDFLib XMP validator approves the file.
738+
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
739+
"<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>\n" +
740+
"<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"\n" +
741+
" x:xmptk=\"XMP toolkit 2.9.1-13, framework 1.6\">\n" +
742+
" <rdf:RDF xmlns:iX=\"http://ns.adobe.com/iX/1.0/\"\n" +
743+
" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" +
744+
" <rdf:Description xmlns:exif=\"http://ns.adobe.com/exif/1.0/\"\n" +
745+
" exif:FNumber=\"36/10\"\n" +
746+
" exif:FileSource=\"3\"\n" +
747+
" exif:Flash=\"1\"\n" +
748+
" rdf:about=\"\">\n" +
749+
" </rdf:Description>\n" +
750+
" </rdf:RDF>\n" +
751+
"</x:xmpmeta><?xpacket end='r'?>";
752+
753+
final DomXmpParser xmpParser1 = new DomXmpParser();
754+
XmpParsingException ex = assertThrows(
755+
XmpParsingException.class,
756+
() -> xmpParser1.parse(s.getBytes(StandardCharsets.UTF_8)));
757+
assertEquals("The type 'Flash' in 'exif:Flash=1' is a structured type, but attributes are simple types", ex.getMessage());
758+
final DomXmpParser xmpParser2 = new DomXmpParser();
759+
xmpParser2.setStrictParsing(false);
760+
XMPMetadata xmp = xmpParser2.parse(s.getBytes(StandardCharsets.UTF_8));
761+
ExifSchema exifSchema = (ExifSchema) xmp.getSchema(ExifSchema.class);
762+
assertEquals("[Flash=TextType:1]", exifSchema.getProperty(ExifSchema.FLASH).toString());
763+
}
730764
}

0 commit comments

Comments
 (0)