Skip to content

Commit

Permalink
Get strict relationship status before opening stream
Browse files Browse the repository at this point in the history
Fixes #1802
  • Loading branch information
twsouthwick committed Nov 5, 2024
1 parent f9e6ad7 commit a407b9f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/DocumentFormat.OpenXml.Framework/OpenXmlPartRootElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ internal void LoadFromPart(OpenXmlPart openXmlPart)
throw new ArgumentNullException(nameof(openXmlPart));
}

// Accessed before stream as it may cause the stream to reload
var strictRelationshipFound = openXmlPart.OpenXmlPackage.StrictRelationshipFound;

using (Stream partStream = openXmlPart.GetStream(FileMode.Open))
{
LoadFromPart(openXmlPart, partStream);
LoadFromPart(openXmlPart, partStream, strictRelationshipFound);
}
}

Expand All @@ -96,12 +99,13 @@ internal void LoadFromPart(OpenXmlPart openXmlPart)
/// </summary>
/// <param name="openXmlPart">The part this root element to be loaded from.</param>
/// <param name="partStream">The stream of the part.</param>
/// <param name="strictRelationshipFound">Whether a strict relationship was found.</param>
/// <returns>
/// Returns true when the part stream is loaded successfully into this root element.
/// Returns false when the part stream does not contain any xml element.
/// </returns>
/// <exception cref="InvalidDataException">Thrown when the part stream contains an incorrect root element.</exception>
internal bool LoadFromPart(OpenXmlPart openXmlPart, Stream partStream)
internal bool LoadFromPart(OpenXmlPart openXmlPart, Stream partStream, bool strictRelationshipFound)
{
if (partStream.Length < 4)
{
Expand All @@ -123,7 +127,7 @@ internal bool LoadFromPart(OpenXmlPart openXmlPart, Stream partStream)
context.XmlReaderSettings.DtdProcessing = DtdProcessing.Prohibit; // set to prohibit explicitly for security fix
#endif

using (var xmlReader = XmlConvertingReaderFactory.Create(partStream, Features.GetNamespaceResolver(), context.XmlReaderSettings, openXmlPart.OpenXmlPackage.StrictRelationshipFound))
using (var xmlReader = XmlConvertingReaderFactory.Create(partStream, Features.GetNamespaceResolver(), context.XmlReaderSettings, strictRelationshipFound))
{
context.MCSettings = openXmlPart.MCSettings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ internal void LoadDomTree<T>()
var events = Features.Get<IPartRootEventsFeature>();
events?.OnChange(EventType.Creating, this);

// Accessed before stream as it may cause the stream to reload
var strictRelationshipFound = OpenXmlPackage.StrictRelationshipFound;

using (Stream stream = GetStream(FileMode.OpenOrCreate, FileAccess.Read))
{
if (stream.Length < 4)
Expand All @@ -494,7 +497,7 @@ internal void LoadDomTree<T>()
// OpenXmlReaderWriterTest.bug247883() unit test fails.
var rootElement = new T { OpenXmlPart = this };

if (rootElement.LoadFromPart(this, stream))
if (rootElement.LoadFromPart(this, stream, strictRelationshipFound))
{
// associate the root element with this part.
InternalRootElement = rootElement;
Expand Down

0 comments on commit a407b9f

Please sign in to comment.