Skip to content

Commit

Permalink
feat(ref-imp): #781 - make jcs long form do size check on delta (#866)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacJChen committed Sep 25, 2020
1 parent 5808eaf commit 20cfc61
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/core/versions/latest/Delta.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ErrorCode from './ErrorCode';
import JsonCanonicalizer from './util/JsonCanonicalizer';
import ProtocolParameters from './ProtocolParameters';
import SidetreeError from '../../../common/SidetreeError';

Expand All @@ -9,6 +10,7 @@ export default class Delta {

/**
* Validates size of the encoded delta string.
* TODO: SIP 2 #781 delete this and use validateDeltaSize only in chunk file
* @throws `SidetreeError` if fails validation.
*/
public static validateEncodedDeltaSize (encodedDelta: string) {
Expand All @@ -19,4 +21,16 @@ export default class Delta {
throw new SidetreeError(ErrorCode.DeltaExceedsMaximumSize, errorMessage);
}
}

/**
* Validates size of the delta object
*/
public static validateDeltaSize (delta: object) {
const size = Buffer.byteLength(JsonCanonicalizer.canonicalizeAsBuffer(delta));
if (size > ProtocolParameters.maxDeltaSizeInBytes) {
const errorMessage = `${size} bytes of 'delta' exceeded limit of ${ProtocolParameters.maxDeltaSizeInBytes} bytes.`;
console.info(errorMessage);
throw new SidetreeError(ErrorCode.DeltaExceedsMaximumSize, errorMessage);
}
}
}
1 change: 1 addition & 0 deletions lib/core/versions/latest/Did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export default class Did {
}

Did.validateInitialState(initialStateEncodedJcs, initialStateObject);
Delta.validateDeltaSize(initialStateObject.delta);

const createOperationRequest = {
type: OperationType.Create,
Expand Down
2 changes: 1 addition & 1 deletion lib/core/versions/latest/protocol-parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"hashAlgorithmInMultihashCode": 18,
"maxAnchorFileSizeInBytes": 1000000,
"maxMapFileSizeInBytes": 1000000,
"maxChunkFileSizeInBytes": 20000000,
"maxChunkFileSizeInBytes": 10000000,
"maxNumberOfOperationsPerTransactionTime": 600000,
"maxNumberOfTransactionsPerTransactionTime": 300,
"maxOperationsPerBatch": 10000,
Expand Down
9 changes: 9 additions & 0 deletions tests/core/Did.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Did from '../../lib/core/versions/latest/Did';
import Encoder from '../../lib/core/versions/latest/Encoder';
import ErrorCode from '../../lib/core/versions/latest/ErrorCode';
import JasmineSidetreeErrorValidator from '../JasmineSidetreeErrorValidator';
import JsonCanonicalizer from '../../lib/core/versions/latest/util/JsonCanonicalizer';
import OperationGenerator from '../generators/OperationGenerator';
import SidetreeError from '../../lib/common/SidetreeError';

Expand All @@ -27,6 +28,14 @@ describe('DID', async () => {
expect(e).toEqual(new SidetreeError(ErrorCode.DidInitialStateJcsIsNotJcs, 'Initial state object and JCS string mismatch.'));
}
});

it('should throw sidetree error if delta exceeds size limit', () => {
const largeData = crypto.randomBytes(2000).toString('hex');// Intentionally exceeding max size.
const largeDelta = { data: largeData };
const testInitialState = Encoder.encode(JsonCanonicalizer.canonicalizeAsBuffer({ suffix_data: 'some data', delta: largeDelta }));

JasmineSidetreeErrorValidator.expectSidetreeErrorToBeThrown(() => { Did['constructCreateOperationFromEncodedJcs'](testInitialState); }, ErrorCode.DeltaExceedsMaximumSize);
});
});

describe('create()', async () => {
Expand Down

0 comments on commit 20cfc61

Please sign in to comment.