Skip to content

Commit

Permalink
feat(ref-imp): #781 - make jcs long form do size check on delta
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacJChen committed Sep 23, 2020
1 parent 5808eaf commit f71108c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/core/versions/latest/Delta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,18 @@ export default class Delta {
throw new SidetreeError(ErrorCode.DeltaExceedsMaximumSize, errorMessage);
}
}

/**
* Validates size of the delta object
* TODO: SIP 2 #781 make observer check that the chunk file is cannonicalized, so we know that no filler characters are used
* Once we know there are no filler characters, it is valid to do size check on delta object because we know the object size === size in file
*/
public static validateDeltaSize (delta: object) {
const size = Buffer.byteLength(JSON.stringify(delta), 'utf8');
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
14 changes: 14 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,19 @@ 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 }));

try {
Did['constructCreateOperationFromEncodedJcs'](testInitialState);
fail('expect to throw sidetree error but did not');
} catch (e) {
expect(e.code).toEqual(ErrorCode.DeltaExceedsMaximumSize);
}
})
});

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

0 comments on commit f71108c

Please sign in to comment.