diff --git a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfMetadata.cs b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfMetadata.cs
index 88d65d08b..6742f3f52 100644
--- a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfMetadata.cs
+++ b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfMetadata.cs
@@ -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.
@@ -145,17 +155,17 @@ internal string CreateDefaultMetadata(MetadataEncodingType encodingType)
// xmlns:pdf: Producer, Keywords
$"""
- {metadataInfo.Producer}
- {metadataInfo.Keywords}
+ {producer}
+ {keywords}
""" + "\r\n" +
// xmlns:xap: CreatorTool, CreateDate, ModifyDate
$"""
- {metadataInfo.Creator}
- {metadataInfo.CreationDate}
- {metadataInfo.ModificationDate}
- {(!String.IsNullOrWhiteSpace(metadataInfo.ModificationDate) ? metadataInfo.ModificationDate : metadataInfo.CreationDate)}
+ {creator}
+ {creationDate}
+ {modificationDate}
+ {metadataDate}
""" + "\r\n" +
// xmlns:dc: Title, Creator (author), Description
@@ -163,17 +173,17 @@ internal string CreateDefaultMetadata(MetadataEncodingType encodingType)
- {metadataInfo.Title}
+ {title}
- {metadataInfo.Author}
+ {author}
- {metadataInfo.Subject}
+ {subject}
@@ -183,8 +193,8 @@ internal string CreateDefaultMetadata(MetadataEncodingType encodingType)
// xmlns:xapMM: DocumentID, InstanceID
$"""
- uuid:{metadataInfo.DocumentID}
- uuid:{metadataInfo.InstanceID}
+ uuid:{EscapeXmlText(metadataInfo.DocumentID)}
+ uuid:{EscapeXmlText(metadataInfo.InstanceID)}
""" + "\r\n" +
#endif
@@ -198,6 +208,43 @@ internal string CreateDefaultMetadata(MetadataEncodingType encodingType)
return xml;
}
+ ///
+ /// Escapes XML special characters and removes characters disallowed in XML 1.0.
+ ///
+ 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
+ {
+ '&' => "&",
+ '<' => "<",
+ '>' => ">",
+ '"' => """,
+ '\'' => "'",
+ _ 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;
+ }
+
///
/// Converts the stream content to a string.
/// The BOM in “<?xpacket begin="{BOM}"” is set to “UTF-8” because a UTF-8 BOM cannot be a
diff --git a/src/foundation/src/PDFsharp/tests/PdfSharp.Tests/Pdf.Metadata/MetadataTests.cs b/src/foundation/src/PDFsharp/tests/PdfSharp.Tests/Pdf.Metadata/MetadataTests.cs
index 13e7092f3..4867d1e9f 100644
--- a/src/foundation/src/PDFsharp/tests/PdfSharp.Tests/Pdf.Metadata/MetadataTests.cs
+++ b/src/foundation/src/PDFsharp/tests/PdfSharp.Tests/Pdf.Metadata/MetadataTests.cs
@@ -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;
@@ -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 ";
+ doc.Info.Subject = "A & B > C";
+ doc.Info.Keywords = "tag1 & tag2, ";
+ 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 ");
+ 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, ");
+ xmlDoc.SelectSingleNode("//xap:CreatorTool", ns)!.InnerText
+ .Should().Be("Tool <1.0> & Co");
+ xmlDoc.SelectSingleNode("//pdf:Producer", ns)!.InnerText
+ .Should().Be("Producer & Engine");
+ }
}
}