{"record":{"id":"1b4984a356ac2fd2","repo":"hyperledger/fabric","slug":"failed-to-unmarshal-envelope-from-bytes","errorCode":null,"errorMessage":"failed to unmarshal envelope from bytes","messagePattern":"failed to unmarshal envelope from bytes","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/common/ccprovider/sigcdspackage.go","lineNumber":227,"sourceCode":"\totherdata := &SignedCDSData{}\n\terr := proto.Unmarshal(ccdata.Data, otherdata)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif !proto.Equal(ccpack.data, otherdata) {\n\t\treturn errors.New(\"data mismatch\")\n\t}\n\n\treturn nil\n}\n\n// InitFromBuffer sets the buffer if valid and returns ChaincodeData\nfunc (ccpack *SignedCDSPackage) InitFromBuffer(buf []byte) (*ChaincodeData, error) {\n\tenv := &common.Envelope{}\n\terr := proto.Unmarshal(buf, env)\n\tif err != nil {\n\t\treturn nil, errors.New(\"failed to unmarshal envelope from bytes\")\n\t}\n\tcHdr, sDepSpec, err := ccpackage.ExtractSignedCCDepSpec(env)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif cHdr.Type != int32(common.HeaderType_CHAINCODE_PACKAGE) {\n\t\treturn nil, errors.New(\"invalid type of envelope for chaincode package\")\n\t}\n\n\tdepSpec := &pb.ChaincodeDeploymentSpec{}\n\terr = proto.Unmarshal(sDepSpec.ChaincodeDeploymentSpec, depSpec)\n\tif err != nil {\n\t\treturn nil, errors.New(\"error getting deployment spec\")\n\t}\n\n\tdatabytes, id, data, err := ccpack.getCDSData(sDepSpec)\n\tif err != nil {","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/common/ccprovider/sigcdspackage.go#L209-L245","documentation":"InitFromBuffer attempts to proto.Unmarshal the package bytes into a common.Envelope and fails if the bytes are not a valid protobuf envelope. The error deliberately discards the underlying parse error. It means the input is not the expected signed chaincode package format at all.","triggerScenarios":"InitFromBuffer called (directly or via GetCCPackage / InitFromPath / processSignedCDS) with bytes that are not a valid common.Envelope: empty file, plain CDS bytes, tar package file, or random data.","commonSituations":"Pointing the installer at a .tar.gz CDS package instead of a signed CDS envelope; empty or truncated file; passing raw ChaincodeDeploymentSpec bytes.","solutions":["Confirm the input is the output of `peer chaincode signpackage` (a signed CDS envelope), not a CDS .tar.gz","Check the file is non-empty and fully transferred (compare sizes/checksums)","Regenerate the signed package from the original CDS"],"exampleFix":"// before\nbuf, _ := ioutil.ReadFile(\"mycc.cds\") // tar CDS package, not an envelope\nccpack.InitFromBuffer(buf)\n// after\nbuf, _ := ioutil.ReadFile(\"mycc-signed.pack\") // signed CDS envelope from signpackage\nccpack.InitFromBuffer(buf)","handlingStrategy":"validation","validationCode":"func validateEnvelopeBytes(buf []byte) error {\n    if len(buf) == 0 { return errors.New(\"empty package buffer\") }\n    env := &common.Envelope{}\n    if err := proto.Unmarshal(buf, env); err != nil {\n        return fmt.Errorf(\"not a valid signed CDS envelope: %w\", err)\n    }\n    if env.Payload == nil { return errors.New(\"envelope has no payload\") }\n    return nil\n}","typeGuard":"func isEnvelope(buf []byte) bool {\n    env := &common.Envelope{}\n    return len(buf) > 0 && proto.Unmarshal(buf, env) == nil && env.Payload != nil\n}","tryCatchPattern":"cd, err := GetCCPackage(buf, idFunc)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to unmarshal envelope\") {\n        return fmt.Errorf(\"%s is not a signed chaincode package (did you pass a .cds tar?)\", path)\n    }\n    return err\n}","preventionTips":["Only feed `peer chaincode signpackage` output into package loaders","Check file size and checksum before install","Do not confuse CDS tar packages with signed CDS envelopes"],"tags":["chaincode","fabric","protobuf","deserialization"],"backgroundTag":"invalid-protobuf-payload","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"}