Skip to content

RelationshipErrorHandlerFactory fails with "Data at the root level is invalid" #883

Closed
@Muppets

Description

@Muppets

Using RelationshipErrorHandlerFactory.Rewrite with an output that is shorter than the original URI causes the output XML to be malformed in this Word document.

        static void Main(string[] args)
        {
            var doc = WordprocessingDocument.Open("BrokenURI.docx", isEditable: true, new OpenSettings
            {
                RelationshipErrorHandlerFactory = _ => new RemoveMalformedHyperlinksRelationshipErrorHandler(),
            });
        }

        private sealed class RemoveMalformedHyperlinksRelationshipErrorHandler : RelationshipErrorHandler
        {
            // Works
            //public override string Rewrite(Uri partUri, string id, string uri) => $"https://error{new string('r', uri.Length)}";

            // Fails with "Data at the root level is invalid"
            public override string Rewrite(Uri partUri, string id, string uri) => $"https://error";
        }

When the above is run on a Word document with the following document.xml.rels:

<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
	<Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/>
	<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>
	<Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="mailto:[email protected];%[email protected];%[email protected];%[email protected];%[email protected];%[email protected]?subject=Unsubscribe%20Request&amp;body=Please%20unsubscribe%20me%20from%20all%20future%20communications" TargetMode="External"/>
	<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
	<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" Target="numbering.xml"/>
	<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes" Target="endnotes.xml"/>
	<Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes" Target="footnotes.xml"/>
	<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml"/>
	<Relationship Id="rId9" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
</Relationships>

It fails with "Data at the root level is invalid. Line 12, position 17." and has produced the following in the output document:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml" />
  <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml" />
  <Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="https://error" TargetMode="External" />
  <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml" />
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" Target="numbering.xml" />
  <Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes" Target="endnotes.xml" />
  <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes" Target="footnotes.xml" />
  <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml" />
  <Relationship Id="rId9" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml" />
</Relationships>l"/><Relationship Id="rId9" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/></Relationships>

As you can see, the end part of the XML has now been malformed. This is probably a buffer issue with the shorter string being returned from the Rewrite method. I've seen this in a few places when working with the Package classes.

Extending the Rewrite output to match the length of the original URI resolves the issue:

public override string Rewrite(Uri partUri, string id, string uri) => $"https://error{new string('r', uri.Length)}";

Information

  • .NET Target: .NET Core
  • DocumentFormat.OpenXml Version: 2.12.1

Please see example console app with sample Word document repo'ing the issue:

https://github.com/Muppets/RelationshipErrorHandlerFactoryTest

Observed

Exception thrown: System.Xml.XmlException: 'Data at the root level is invalid. Line 12, position 17.'

Expected

Invalid URI is replaced with https://error

Just as a footnote, I'm loving the new RelationshipErrorHandlerFactory support added recently, great work! 🎉

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions