{"record":{"id":"6bee493d2f310872","repo":"dotnet/wpf","slug":"signature-structures-are-corrupted-in-this-package-6bee49","errorCode":null,"errorMessage":"Signature structures are corrupted in this package.","messagePattern":"Signature structures are corrupted in this package\\.","errorType":"exception","errorClass":"FileFormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/PackageDigitalSignatureManager.cs","lineNumber":1088,"sourceCode":"\n            // now invoke the delegate for each member\n            for (int i = 0; i < relationshipsToVisit.Count; i++)\n            {\n                // exit if visitor wants us to\n                if (!visit(relationshipsToVisit[i], context))\n                    break;\n            }\n        }\n\n        /// <summary>\n        /// Removes the certificate associated with the given signature if removing the signature would leave the\n        /// certificate part orphaned.\n        /// </summary>\n        private bool DeleteCertificateIfReferenceCountBecomesZeroVisitor(PackageRelationship r, Object context)\n        {\n            // don't resolve if external\n            if (r.TargetMode != TargetMode.Internal)\n                throw new FileFormatException(SR.PackageSignatureCorruption);\n            \n            Uri certificatePartName = PackUriHelper.ResolvePartUri(r.SourceUri, r.TargetUri);\n            if (CertificatePartReferenceCount(certificatePartName) == 1)    // we are part of the calculation so one is the magic number\n                _container.DeletePart(certificatePartName);                 // will not throw if part not found\n\n            return true;\n        }\n\n        /// <summary>\n        /// Deletes any relationship that is of the type that relates a Package to the Digital Signature Origin\n        /// </summary>\n        /// <param name=\"r\"></param>\n        /// <param name=\"context\"></param>\n        /// <returns></returns>\n        private bool DeleteRelationshipOfTypePackageToOriginVisitor(PackageRelationship r, Object context)\n        {\n            Debug.Assert(Uri.Compare(r.SourceUri, \n                                     MS.Internal.IO.Packaging.PackUriHelper.PackageRootUri, ","sourceCodeStart":1070,"sourceCodeEnd":1106,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/PackageDigitalSignatureManager.cs#L1070-L1106","documentation":"DeleteCertificateIfReferenceCountBecomesZeroVisitor is a relationship visitor used while removing signatures; if a relationship it walks is external (TargetMode != Internal) where an internal certificate part reference is required, it throws FileFormatException (SR.PackageSignatureCorruption), meaning the package's digital-signature structures are inconsistent or tampered with.","triggerScenarios":"Removing signatures (RemoveSignature/RemoveAllSignatures traversal) from a package whose signature-origin/certificate relationships point to external targets instead of internal parts — a corrupted or hand-edited OPC signature infrastructure.","commonSituations":"Packages modified by third-party tools that rewrote relationships, files damaged by partial downloads or manual zip editing, or non-conformant producers of signed OPC packages.","solutions":["Catch FileFormatException around RemoveSignature/RemoveAllSignatures and treat the package as corrupt — do not attempt further signature edits.","Rebuild or re-export the package from its source application to regenerate conformant signature structures.","If you control package production, ensure certificate relationships created for signatures always use TargetMode.Internal."],"exampleFix":"// before\nmgr.RemoveAllSignatures(); // FileFormatException on corrupt package\n// after\ntry { mgr.RemoveAllSignatures(); pkg.Flush(); }\ncatch (FileFormatException ex)\n{\n    log.Error(\"Package signature structures are corrupted\", ex);\n}","handlingStrategy":"try-catch","validationCode":"bool HasExternalCertRelationships(Package pkg) =>\n    pkg.GetRelationships().Any(r => r.TargetMode == TargetMode.External);","typeGuard":null,"tryCatchPattern":"try { mgr.RemoveSignature(uri); }\ncatch (FileFormatException ex) { /* signature structures corrupted — quarantine package */ }","preventionTips":["Treat FileFormatException during signature ops as package corruption, not a transient error.","Validate packages from third-party producers before editing signatures.","Keep certificate relationships TargetMode.Internal when authoring packages."],"tags":["wpf","packaging","corruption","file-format","digital-signature"],"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-21T21:30:21.729Z"}