Skip to content
Open
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
69 changes: 58 additions & 11 deletions src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ internal string CreateDefaultMetadata(MetadataEncodingType encodingType)
// ReSharper disable StringLiteralTypo because we deal with XML elements here.
bool isPdfA = metadataInfo.PdfAFormat.HasValue;

var producer = EscapeXmlText(metadataInfo.Producer);
var keywords = EscapeXmlText(metadataInfo.Keywords);
var creator = EscapeXmlText(metadataInfo.Creator);
var creationDate = EscapeXmlText(metadataInfo.CreationDate);
var modificationDate = EscapeXmlText(metadataInfo.ModificationDate);
var metadataDate = !String.IsNullOrWhiteSpace(modificationDate) ? modificationDate : creationDate;
var title = EscapeXmlText(metadataInfo.Title);
var author = EscapeXmlText(metadataInfo.Author);
var subject = EscapeXmlText(metadataInfo.Subject);

var xml =
// XMP header.
// Note that in a .NET string the BOM is represented as a human-readable string.
Expand All @@ -145,35 +155,35 @@ internal string CreateDefaultMetadata(MetadataEncodingType encodingType)
// xmlns:pdf: Producer, Keywords
$"""
<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">
<pdf:Producer>{metadataInfo.Producer}</pdf:Producer>
<pdf:Keywords>{metadataInfo.Keywords}</pdf:Keywords>
<pdf:Producer>{producer}</pdf:Producer>
<pdf:Keywords>{keywords}</pdf:Keywords>
</rdf:Description>
""" + "\r\n" +
// xmlns:xap: CreatorTool, CreateDate, ModifyDate
$"""
<rdf:Description xmlns:xap="http://ns.adobe.com/xap/1.0/" rdf:about="">
<xap:CreatorTool>{metadataInfo.Creator}</xap:CreatorTool>
<xap:CreateDate>{metadataInfo.CreationDate}</xap:CreateDate>
<xap:ModifyDate>{metadataInfo.ModificationDate}</xap:ModifyDate>
<xap:MetadataDate>{(!String.IsNullOrWhiteSpace(metadataInfo.ModificationDate) ? metadataInfo.ModificationDate : metadataInfo.CreationDate)}</xap:MetadataDate>
<xap:CreatorTool>{creator}</xap:CreatorTool>
<xap:CreateDate>{creationDate}</xap:CreateDate>
<xap:ModifyDate>{modificationDate}</xap:ModifyDate>
<xap:MetadataDate>{metadataDate}</xap:MetadataDate>
</rdf:Description>
""" + "\r\n" +
// xmlns:dc: Title, Creator (author), Description
$"""
<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>
<rdf:Alt>
<rdf:li xml:lang="x-default">{metadataInfo.Title}</rdf:li>
<rdf:li xml:lang="x-default">{title}</rdf:li>
</rdf:Alt>
</dc:title>
<dc:creator>
<rdf:Seq>
<rdf:li>{metadataInfo.Author}</rdf:li>
<rdf:li>{author}</rdf:li>
</rdf:Seq>
</dc:creator>
<dc:description>
<rdf:Alt>
<rdf:li xml:lang="x-default">{metadataInfo.Subject}</rdf:li>
<rdf:li xml:lang="x-default">{subject}</rdf:li>
</rdf:Alt>
</dc:description>
</rdf:Description>
Expand All @@ -183,8 +193,8 @@ internal string CreateDefaultMetadata(MetadataEncodingType encodingType)
// xmlns:xapMM: DocumentID, InstanceID
$"""
<rdf:Description rdf:about="" xmlns:xapMM="http://ns.adobe.com/xap/1.0/mm/">
<xapMM:DocumentID>uuid:{metadataInfo.DocumentID}</xapMM:DocumentID>
<xapMM:InstanceID>uuid:{metadataInfo.InstanceID}</xapMM:InstanceID>
<xapMM:DocumentID>uuid:{EscapeXmlText(metadataInfo.DocumentID)}</xapMM:DocumentID>
<xapMM:InstanceID>uuid:{EscapeXmlText(metadataInfo.InstanceID)}</xapMM:InstanceID>
</rdf:Description>
""" + "\r\n" +
#endif
Expand All @@ -198,6 +208,43 @@ internal string CreateDefaultMetadata(MetadataEncodingType encodingType)
return xml;
}

/// <summary>
/// Escapes XML special characters and removes characters disallowed in XML 1.0.
/// </summary>
static string EscapeXmlText(string? value)
{
if (String.IsNullOrEmpty(value))
return "";

StringBuilder? sb = null;
for (int i = 0; i < value.Length; i++)
{
char ch = value[i];
string? replacement = ch switch
{
'&' => "&amp;",
'<' => "&lt;",
'>' => "&gt;",
'"' => "&quot;",
'\'' => "&apos;",
_ when (ch < 0x20 && ch is not ('\t' or '\n' or '\r')) || ch is '\uFFFE' or '\uFFFF' => "",
_ => null
};

if (replacement != null)
{
sb ??= new StringBuilder(value.Length + 16).Append(value, 0, i);
sb.Append(replacement);
}
else
{
sb?.Append(ch);
}
}

return sb?.ToString() ?? value;
}

/// <summary>
/// Converts the stream content to a string.<br/>
/// The BOM in “&lt;?xpacket begin="{BOM}"” is set to “UTF-8” because a UTF-8 BOM cannot be a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// PDFsharp - A .NET library for processing PDF
// See the LICENSE file in the solution root for more information.

using System.Xml;
using FluentAssertions;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
Expand Down Expand Up @@ -101,5 +102,42 @@ void SetSomeMetadata(PdfDocument doc)
info.Creator = "CCCC";
#endif
}

[Fact]
public void CreateDefaultMetadata_escapes_xml_and_strips_disallowed_control_chars()
{
var doc = new PdfDocument();
doc.Info.Title = "Profit & Loss <2026> \"Q1\" 'Draft'\u0000\u000B\u001F";
doc.Info.Author = "Smith & Sons <legal@example.com>";
doc.Info.Subject = "A & B > C";
doc.Info.Keywords = "tag1 & tag2, <xml>";
doc.Info.Creator = "Tool <1.0> & Co";
doc.Info.Elements.SetString(PdfDocumentInformation.Keys.Producer, "Producer & Engine");

var metadata = doc.Catalog.GetOrCreateMetadata();
var xml = metadata.CreateDefaultMetadata();

var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);

var ns = new XmlNamespaceManager(xmlDoc.NameTable);
ns.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
ns.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
ns.AddNamespace("pdf", "http://ns.adobe.com/pdf/1.3/");
ns.AddNamespace("xap", "http://ns.adobe.com/xap/1.0/");

xmlDoc.SelectSingleNode("//dc:title/rdf:Alt/rdf:li", ns)!.InnerText
.Should().Be("Profit & Loss <2026> \"Q1\" 'Draft'");
xmlDoc.SelectSingleNode("//dc:creator/rdf:Seq/rdf:li", ns)!.InnerText
.Should().Be("Smith & Sons <legal@example.com>");
xmlDoc.SelectSingleNode("//dc:description/rdf:Alt/rdf:li", ns)!.InnerText
.Should().Be("A & B > C");
xmlDoc.SelectSingleNode("//pdf:Keywords", ns)!.InnerText
.Should().Be("tag1 & tag2, <xml>");
xmlDoc.SelectSingleNode("//xap:CreatorTool", ns)!.InnerText
.Should().Be("Tool <1.0> & Co");
xmlDoc.SelectSingleNode("//pdf:Producer", ns)!.InnerText
.Should().Be("Producer & Engine");
}
}
}