transmute-industries / sidetree.js

Sidetree Core Protocol and DID Method Drivers
https://identity.foundation/sidetree/spec/
Apache License 2.0
51 stars 26 forks source link

Possible endless loop in resolving DID document #164

Open S3bb1 opened 3 years ago

S3bb1 commented 3 years ago

Hi all,

i found a possible endless loop when resolving a DID document.

Test scenario:

  const testDid = 'did:elem:EiC6QAiru_uIUJf9FF18mxUFyLSqhsE-ZvCUj0N5--cmHg';
  const testData = [{
    type: 'create',
    suffix_data: 'eyJkZWx0YV9oYXNoIjoiRWlBOVNGU2hacVQzVWQ3eWtTdFZpTmxZZ2VpRjViblNuZUd6Vl9MdjVCbXdsUSIsInJlY292ZXJ5X2NvbW1pdG1lbnQiOiJFaUMzaDVoanJJdzRfRHBlUzB6RjRaanVRXzdzSFFjRXVPWVRHOXZOcDlMdUxRIn0',
    delta: 'eyJwYXRjaGVzIjpbeyJhY3Rpb24iOiJyZXBsYWNlIiwiZG9jdW1lbnQiOnsicHVibGljX2tleXMiOlt7ImlkIjoiejZNa20zQ3ZhUDdjRG9RRTRTd0V2ajNMMTJzZHBQYnl5WjE2ZlhvR1NvWUFzS0N4IiwiandrIjp7ImNydiI6IkVkMjU1MTkiLCJrdHkiOiJPS1AiLCJ4IjoiWWRoWEYyc1BNVW85OWdTNE1WSWNSeW0xQTVaMWhsRGg2REt0MFByWXRfMCJ9LCJwdXJwb3NlIjpbImdlbmVyYWwiLCJhdXRoIiwiYXNzZXJ0aW9uIiwiaW52b2NhdGlvbiIsImRlbGVnYXRpb24iXSwidHlwZSI6Ikpzb25XZWJLZXkyMDIwIn0seyJpZCI6Ino2TFNoYkZua0NmM0cyaDVHeXl1RHhLMmJCUHZzcjZEbml0UERCazNXQmhkQ0tTTiIsImp3ayI6eyJjcnYiOiJYMjU1MTkiLCJrdHkiOiJPS1AiLCJ4IjoiVi1OeVRGSnByVW1JVUN5YzFGQS1qRlpUWHVQZkVpRHMzMjZ5YkY3VVFEOCJ9LCJwdXJwb3NlIjpbImdlbmVyYWwiLCJhZ3J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UxOSIsImt0eSI6Ik9LUCIsIngiOiJZZGhYRjJzUE1Vbzk5Z1M0TVZJY1J5bTFBNVoxaGxEaDZES3QwUHJZdF8wIn19.pPi69Jbw8C6Pw8jfyu8hmNjt3XJgTzn6rjza1hrxBKaVbhlNS8V8ZyvXR-ZeGqT37kNJvw6sBOgBuCznPZtNDw',
    delta: 'eyJwYXRjaGVzIjpbeyJhY3Rpb24iOiJhZGQtc2VydmljZS1lbmRwb2ludHMiLCJzZXJ2aWNlX2VuZHBvaW50cyI6W3siZW5kcG9pbnQiOiJodHRwOi8vdGVzdDIuY29tIiwiaWQiOiJ0ZXN0MiIsInR5cGUiOiJTZWN1cmVEYXRhU3RvcmUifV19XSwidXBkYXRlX2NvbW1pdG1lbnQiOiJFaUMzaDVoanJJdzRfRHBlUzB6RjRaanVRXzdzSFFjRXVPWVRHOXZOcDlMdUxRIn0'
  }]

When executing these three operations in order with the following order

   await element.handleOperationRequest(
    Buffer.from(JSON.stringify(testData[0]))
  );
  await element.triggerBatchAndObserve();
   await element.handleOperationRequest(
    Buffer.from(JSON.stringify(testData[1]))
  );
  await element.triggerBatchAndObserve();
   await element.handleOperationRequest(
    Buffer.from(JSON.stringify(testData[2]))
  );
  await element.triggerBatchAndObserve();
  const resolveRequest = await element.handleResolveRequest(testDid);

sidetree.js runs into an endless loop during calculation of the didState. This is because i updated the DID document with 2 times the same "updateCommitment" hash.

In the resolver where the commitValueToOperationMap is processed i have for the same entry two values:

image

now the while loop in https://github.com/transmute-industries/sidetree.js/blob/main/packages/core/src/Resolver.ts#L151 checks if the map has the nextUpdateCommitmentHash and continues the loop. My nextUpdateCommitmentHash is always the same like in the picture above EiC3h5hjrIw4_DpeS0zF4ZjuQ_7sHQcEuOYTG9vNp9LuLQ and the while loop never finishes.

Solution would be that the already processed keys are deleted from the map.

I have also looked into the original Sidetree repo and it seems that the same issue exists there too.

OR13 commented 2 years ago

@BenjaminMoe I think I had you look at this issue on the upstream, please make sure to cross link so we get their fixes implemented.

OR13 commented 2 years ago

I think this is related: https://github.com/decentralized-identity/sidetree/pull/1172#pullrequestreview-857505779

@BenjaminMoe can you confirm.