Skip to content

Commit fd5ebce

Browse files
authored
[msp] Fix cached nil identities (#78)
#### Type of change - Bug fix #### Description The commit fixes a nil pointer dereference panic that occurred when deserializing an identity using a CertificateId. Root cause: When DeserializeIdentity was called with an Identity_CertificateId, it called GetKnownDeserializedIdentity() to look up the identity from an internal map. If the identity wasn't found, the method returned nil. This nil identity then reached the cache layer (cachedMSP.DeserializeIdentity), which attempted id.(msp.Identity) on the nil interface value, causing a panic. #### Related issues - resolves #77 Signed-off-by: Senthilnathan <cendhu@gmail.com>
1 parent cf16c9c commit fd5ebce

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

msp/cache/cache.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,19 @@ func (c *cachedMSP) DeserializeIdentity(identity *msppb.Identity) (msp.Identity,
7474
}
7575

7676
id, err := c.MSP.DeserializeIdentity(identity)
77-
if err == nil {
78-
c.deserializeIdentityCache.add(identity.String(), id)
79-
return &cachedIdentity{
80-
cache: c,
81-
Identity: id.(msp.Identity),
82-
}, nil
77+
if err != nil {
78+
return nil, err
79+
}
80+
81+
c.deserializeIdentityCache.add(identity.String(), id)
82+
mspIdentity, ok := id.(msp.Identity)
83+
if !ok {
84+
return nil, errors.New("internal error: DeserializeIdentity returned unexpected type")
8385
}
84-
return nil, err
86+
return &cachedIdentity{
87+
cache: c,
88+
Identity: mspIdentity,
89+
}, nil
8590
}
8691

8792
func (c *cachedMSP) Setup(config *pmsp.MSPConfig) error {

msp/msp_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,35 @@ func TestDeserializeIdentityFails(t *testing.T) {
236236
require.Error(t, err)
237237
}
238238

239+
func TestDeserializeIdentityWithUnknownCertificateId(t *testing.T) {
240+
t.Parallel()
241+
242+
// Deserializing with a CertificateId that is not in the known identities
243+
// map should return an error instead of a nil identity.
244+
tests := []struct {
245+
name string
246+
deserialize func(*msppb.Identity) (Identity, error)
247+
}{
248+
{
249+
name: "via MSP",
250+
deserialize: localMsp.DeserializeIdentity,
251+
},
252+
{
253+
name: "via MSP manager",
254+
deserialize: mspMgr.DeserializeIdentity,
255+
},
256+
}
257+
258+
for _, tt := range tests {
259+
t.Run(tt.name, func(t *testing.T) {
260+
t.Parallel()
261+
id, err := tt.deserialize(msppb.NewIdentityWithIDOfCert("SampleOrg", "nonexistent-cert-id"))
262+
require.Nil(t, id)
263+
require.EqualError(t, err, "identity is unknown")
264+
})
265+
}
266+
}
267+
239268
func TestGetSigningIdentityFromVerifyingMSP(t *testing.T) {
240269
cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore())
241270
require.NoError(t, err)

msp/mspimpl.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,12 @@ func (msp *bccspmsp) DeserializeIdentity(identity *msppb.Identity) (Identity, er
406406
case *msppb.Identity_Certificate:
407407
return msp.deserializeIdentityInternal(identity.GetCertificate())
408408
case *msppb.Identity_CertificateId:
409-
return msp.GetKnownDeserializedIdentity(
410-
IdentityIdentifier{Mspid: identity.MspId, Id: identity.GetCertificateId()}), nil
409+
id := msp.GetKnownDeserializedIdentity(
410+
IdentityIdentifier{Mspid: identity.MspId, Id: identity.GetCertificateId()})
411+
if id == nil {
412+
return nil, errors.New("identity is unknown")
413+
}
414+
return id, nil
411415
default:
412416
return nil, errors.New("unknown creator type in the identity")
413417
}

msp/mspmgrimpl.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,12 @@ func (mgr *mspManagerImpl) DeserializeIdentity(sID *msppb.Identity) (Identity, e
8989
case *msppb.Identity_Certificate:
9090
return t.deserializeIdentityInternal(sID.GetCertificate())
9191
case *msppb.Identity_CertificateId:
92-
return msp.GetKnownDeserializedIdentity(
93-
IdentityIdentifier{Mspid: sID.MspId, Id: sID.GetCertificateId()}), nil
92+
id := msp.GetKnownDeserializedIdentity(
93+
IdentityIdentifier{Mspid: sID.MspId, Id: sID.GetCertificateId()})
94+
if id == nil {
95+
return nil, errors.New("identity is unknown")
96+
}
97+
return id, nil
9498
default:
9599
return nil, errors.New("unknown creator type")
96100
}

0 commit comments

Comments
 (0)