{"record":{"id":"6c0b4c969dac0f7d","repo":"hyperledger/fabric","slug":"failed-to-unmarshal-applicationpolicy-bytes","errorCode":null,"errorMessage":"failed to unmarshal ApplicationPolicy bytes","messagePattern":"failed to unmarshal ApplicationPolicy bytes","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/chaincode/lifecycle/metadata_provider.go","lineNumber":79,"sourceCode":"\t\tChaincodeInfoProvider:  cip,\n\t\tLegacyMetadataProvider: lmp,\n\t\tChannelPolicyReferenceProvider: &channelPolicyReferenceProviderImpl{\n\t\t\tChannelConfigSource: ccs,\n\t\t},\n\t}\n}\n\ntype MetadataProvider struct {\n\tChaincodeInfoProvider          ChaincodeInfoProvider\n\tLegacyMetadataProvider         LegacyMetadataProvider\n\tChannelPolicyReferenceProvider ChannelPolicyReferenceProvider\n}\n\nfunc (mp *MetadataProvider) toSignaturePolicyEnvelope(channelID string, policyBytes []byte) ([]byte, error) {\n\tp := &peer.ApplicationPolicy{}\n\terr := proto.Unmarshal(policyBytes, p)\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"failed to unmarshal ApplicationPolicy bytes\")\n\t}\n\n\tswitch policy := p.Type.(type) {\n\tcase *peer.ApplicationPolicy_SignaturePolicy:\n\t\treturn protoutil.MarshalOrPanic(policy.SignaturePolicy), nil\n\tcase *peer.ApplicationPolicy_ChannelConfigPolicyReference:\n\t\tp, err := mp.ChannelPolicyReferenceProvider.NewPolicy(channelID, policy.ChannelConfigPolicyReference)\n\t\tif err != nil {\n\t\t\treturn nil, errors.WithMessagef(err, \"could not retrieve policy for reference '%s' on channel '%s'\", policy.ChannelConfigPolicyReference, channelID)\n\t\t}\n\n\t\tcp, ok := p.(policies.Converter)\n\t\tif !ok {\n\t\t\treturn nil, errors.Errorf(\"policy with reference '%s' on channel '%s' is not convertible to SignaturePolicyEnvelope\", policy.ChannelConfigPolicyReference, channelID)\n\t\t}\n\n\t\tspe, err := cp.Convert()\n\t\tif err != nil {","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/chaincode/lifecycle/metadata_provider.go#L61-L97","documentation":"toSignaturePolicyEnvelope unmarshals raw bytes into a peer.ApplicationPolicy protobuf. If proto.Unmarshal fails, the bytes are not a valid ApplicationPolicy message and the error wraps the underlying cause. This happens when the stored policy bytes are corrupt or are not an ApplicationPolicy at all.","triggerScenarios":"Metadata (discovery) calls toSignaturePolicyEnvelope with policyBytes that fail protobuf decoding — e.g. bytes referencing a legacy policy type stored in the wrong format or truncated/corrupt state.","commonSituations":"Peer state corruption; Fabric version mismatch where an older policy serialization is read by newer code; manual state manipulation; a policy captured as a different protobuf type.","solutions":["Inspect and repair the peer state; restore from a clean backup if policy bytes are corrupt","Re-set the policy via a channel config update or re-approve the chaincode with a valid endorsement policy","Align peer versions across the network so serialization expectations match","Enable debug logging and check the wrapped proto error to identify the malformed source"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"envBytes, err := mp.toSignaturePolicyEnvelope(channelID, policyBytes)\nif err != nil {\n  if strings.Contains(err.Error(), \"failed to unmarshal ApplicationPolicy bytes\") {\n    // skip this policy source; log and fall back to chaincode-level policy\n  }\n}","preventionTips":["Keep peer versions homogeneous to avoid serialization mismatches","Restore peer state only from compatible backups","Avoid manual state DB edits","Investigate the wrapped proto error immediately when it appears"],"tags":["fabric","policy","protobuf","unmarshal"],"backgroundTag":"protobuf-unmarshal-failed","analyzedSha":"2736b63f8fd5932511d56fe68b7039d15977f7f6","analyzedAt":"2026-09-04T08:52:36.465Z","contentChangedAt":"2026-09-04T08:52:36.465Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}