From a407b9fbdf1acfc807de27cd168ea65eb808949f Mon Sep 17 00:00:00 2001 From: Taylor Southwick Date: Tue, 5 Nov 2024 10:58:47 -0800 Subject: [PATCH] Get strict relationship status before opening stream Fixes #1802 --- .../OpenXmlPartRootElement.cs | 10 +++++++--- .../Packaging/OpenXmlPart.cs | 5 ++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/DocumentFormat.OpenXml.Framework/OpenXmlPartRootElement.cs b/src/DocumentFormat.OpenXml.Framework/OpenXmlPartRootElement.cs index 5ad344887..276d455ad 100644 --- a/src/DocumentFormat.OpenXml.Framework/OpenXmlPartRootElement.cs +++ b/src/DocumentFormat.OpenXml.Framework/OpenXmlPartRootElement.cs @@ -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); } } @@ -96,12 +99,13 @@ internal void LoadFromPart(OpenXmlPart openXmlPart) /// /// The part this root element to be loaded from. /// The stream of the part. + /// Whether a strict relationship was found. /// /// 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. /// /// Thrown when the part stream contains an incorrect root element. - internal bool LoadFromPart(OpenXmlPart openXmlPart, Stream partStream) + internal bool LoadFromPart(OpenXmlPart openXmlPart, Stream partStream, bool strictRelationshipFound) { if (partStream.Length < 4) { @@ -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; diff --git a/src/DocumentFormat.OpenXml.Framework/Packaging/OpenXmlPart.cs b/src/DocumentFormat.OpenXml.Framework/Packaging/OpenXmlPart.cs index 749e483eb..62913117b 100644 --- a/src/DocumentFormat.OpenXml.Framework/Packaging/OpenXmlPart.cs +++ b/src/DocumentFormat.OpenXml.Framework/Packaging/OpenXmlPart.cs @@ -476,6 +476,9 @@ internal void LoadDomTree() var events = Features.Get(); 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) @@ -494,7 +497,7 @@ internal void LoadDomTree() // 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;