{"record":{"id":"a3a6c56460efa0f0","repo":"dotnet/wpf","slug":"sr-packagesignaturecorruption","errorCode":null,"errorMessage":"SR.PackageSignatureCorruption","messagePattern":"SR\\.PackageSignatureCorruption","errorType":"exception","errorClass":"XmlException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/XmlDigitalSignatureProcessor.cs","lineNumber":625,"sourceCode":"                        PackagingUtilities.PerformInitailReadAndVerifyEncoding(xmlReader);\n\n                        //If the reader.ReadState is ReadState.Initial, then XmlDocument with perform the\n                        //first xmlReader.Read() and start loading from that node/tag. \n                        //If the reader.ReadState is ReadState.Intermediate, then XmlDocument, will start\n                        //loading from that location itself.\n                        //Note: Since in the above method we perform only the first read and will have not \n                        //moved the reader state further down in the markup, we should be okay, and \n                        //xmlDocument.Load will load from the very begining as intended.\n                        xmlDocument.Load(xmlReader);\n\n                        // W3C spec allows for Signature tag to appear as an island and inherently allows\n                        // for multiple Signature tags within the same XML document.\n                        // OPC restricts this to a single, root-level Signature tag.  However, Signature\n                        // tags are allowed to exist within the non-OPC Object tags within an OPC signature.\n                        // This is common for XAdES signatures and must be explicitly allowed.\n                        XmlNodeList nodeList = xmlDocument.ChildNodes;\n                        if (nodeList == null || nodeList.Count == 0 || nodeList.Count > 2)\n                            throw new XmlException(SR.PackageSignatureCorruption);\n\n                        XmlNode node = nodeList[0];\n                        if (nodeList.Count == 2)\n                        {\n                            // First node must be the XmlDeclaration <?xml...>\n                            if (nodeList[0].NodeType != XmlNodeType.XmlDeclaration)\n                                throw new XmlException(SR.PackageSignatureCorruption);\n\n                            // Second node must be in the w3c namespace, and must be the <Signature> tag\n                            node = nodeList[1];\n                        }\n\n                        if ((node.NodeType != XmlNodeType.Element) ||\n                           (!string.Equals(node.NamespaceURI, SignedXml.XmlDsigNamespaceUrl, StringComparison.Ordinal)) ||\n                           (!string.Equals(node.LocalName, XTable.Get(XTable.ID.SignatureTagName), StringComparison.Ordinal)))\n                        {\n                            throw new XmlException(SR.PackageSignatureCorruption);\n                        }","sourceCodeStart":607,"sourceCodeEnd":643,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/XmlDigitalSignatureProcessor.cs#L607-L643","documentation":"EnsureXmlSignatureParsed validates that the signature XML document has a legal OPC structure: exactly one root-level Signature tag (optionally preceded by an XML declaration). If the child node list is empty or contains more than two nodes, the signature is considered corrupted and an XmlException is thrown.","triggerScenarios":"Verifying or reading a PackageDigitalSignature whose XML contains multiple root-level Signature elements, no Signature element, or extra top-level nodes; loading hand-crafted or externally modified signature parts (.xml Signature parts inside the package).","commonSituations":"Packages edited by scripts or third-party tools that appended extra signature elements; merged documents combining signatures; corrupted signature parts after partial package writes or truncation.","solutions":["Open the signature part XML and ensure it has exactly one root-level <Signature> element (plus optional XML declaration).","Delete the corrupted signature part (Package.DeleteRelationship / remove signature) if the package content itself is fine, then re-sign.","Re-sign the package with PackageDigitalSignatureManager.Sign to regenerate a valid Signature part.","Verify the package file integrity — if the package was truncated or corrupted in transit, obtain a fresh copy."],"exampleFix":"// before: multiple root Signature tags in the signature part\n<Signature>...</Signature>\n<Signature>...</Signature>\n// after: single root-level Signature\n<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\">...</Signature>","handlingStrategy":"validation","validationCode":"var doc = new XmlDocument(); doc.Load(signatureXml);\nbool structureOk = doc.DocumentElement != null\n    && doc.DocumentElement.LocalName == \"Signature\"\n    && doc.DocumentElement.NamespaceURI == \"http://www.w3.org/2000/09/xmldsig#\";","typeGuard":null,"tryCatchPattern":"try { status = dsm.VerifySignatures(); }\ncatch (XmlException ex) when (ex.Message.Contains(\"corrupt\")) { /* remove/re-sign the bad signature part */ }","preventionTips":["Never hand-edit signature parts","Ensure exactly one root-level Signature element","Re-sign rather than merge signature XML"],"tags":["wpf","xml-dsig","signatures","xml-validation"],"backgroundTag":"schema-validation-failed","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}