Skip to content

Commit

Permalink
fix(ref-imp): fix null delta throwing unexpected error (#957)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacJChen authored Dec 3, 2020
1 parent bc8e8d4 commit 26bc857
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/core/versions/latest/ChunkFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default class ChunkFile {
}

// Verify size of each delta does not exceed the maximum allowed limit.
Delta.validateDeltaSize(delta);
Delta.validateDelta(delta);
}
}

Expand Down
13 changes: 12 additions & 1 deletion lib/core/versions/latest/Delta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,21 @@ export default class Delta {
}
}

/**
* Validates that delta is not null or undefined
*/
private static validateDeltaIsDefined (delta: any) {
if (delta === undefined || delta === null) {
throw new SidetreeError(ErrorCode.DeltaIsNullOrUndefined, `Delta is ${delta}`);
}
}

/**
* Validates size of the delta object
*/
public static validateDeltaSize (delta: object) {
public static validateDelta (delta: any) {
// null and undefined cannot be turned into buffer
Delta.validateDeltaIsDefined(delta);
const size = Buffer.byteLength(JsonCanonicalizer.canonicalizeAsBuffer(delta));
if (size > ProtocolParameters.maxDeltaSizeInBytes) {
const errorMessage = `${size} bytes of 'delta' exceeded limit of ${ProtocolParameters.maxDeltaSizeInBytes} bytes.`;
Expand Down
6 changes: 3 additions & 3 deletions lib/core/versions/latest/Did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ export default class Did {
throw new SidetreeError(ErrorCode.DidInitialStateJcsIsNotJson, 'Long form initial state should be encoded jcs.');
}

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

const createOperationRequest = {
type: OperationType.Create,
Expand All @@ -203,7 +203,7 @@ export default class Did {
/**
* Make sure initial state is JCS
*/
private static validateInitialState (initialStateEncodedJcs: string, initialStateObject: any): void {
private static validateInitialStateJcs (initialStateEncodedJcs: string, initialStateObject: any): void {
const expectedInitialState = Encoder.encode(JsonCanonicalizer.canonicalizeAsBuffer(initialStateObject));
if (expectedInitialState !== initialStateEncodedJcs) {
throw new SidetreeError(ErrorCode.DidInitialStateJcsIsNotJcs, 'Initial state object and JCS string mismatch.');
Expand Down
1 change: 1 addition & 0 deletions lib/core/versions/latest/ErrorCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default {
DeactivateOperationTypeIncorrect: 'deactivate_operation_type_incorrect',
DeltaExceedsMaximumSize: 'delta_exceeds_maximum_size',
DeltaIsNotObject: 'delta_is_not_object',
DeltaIsNullOrUndefined: 'delta_is_null_or_undefined',
DeltaMissingOrNotString: 'delta_missing_or_not_string',
DeltaMissingOrUnknownProperty: 'delta_missing_or_unknown_property',
DidIncorrectPrefix: 'did_incorrect_prefix',
Expand Down
2 changes: 1 addition & 1 deletion lib/core/versions/latest/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class RequestHandler implements IRequestHandler {
if (operationRequest.type === OperationType.Create ||
operationRequest.type === OperationType.Recover ||
operationRequest.type === OperationType.Update) {
Delta.validateDeltaSize(operationRequest.delta);
Delta.validateDelta(operationRequest.delta);
}

operationModel = await Operation.parse(request);
Expand Down
72 changes: 72 additions & 0 deletions tests/core/Delta.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import Delta from '../../lib/core/versions/latest/Delta';
import ErrorCode from '../../lib/core/versions/latest/ErrorCode';
import JasmineSidetreeErrorValidator from '../JasmineSidetreeErrorValidator';

function generateLongString (length: number): string {
let result = '';
for (let i = 0; i < length; i++) {
result = result + 'a';
}
return result;
}

describe('Delta', () => {
describe('validateEncodedDeltaSize', () => {
it('should throw sidetree if encoded size exceeds max limit', () => {
const mockEncodedDelta = generateLongString(2000);
JasmineSidetreeErrorValidator.expectSidetreeErrorToBeThrown(
() => { Delta.validateEncodedDeltaSize(mockEncodedDelta); },
ErrorCode.DeltaExceedsMaximumSize
);
});

it('should not throw if encoded size does not exceed max limit', () => {
const mockEncodedDelta = generateLongString(1);
try {
Delta.validateEncodedDeltaSize(mockEncodedDelta);
} catch (e) {
fail(`Expected no error but got ${e}`);
}
});
});

describe('validateDelta', () => {
it('should throw sidetree error if delta size exceeds max limit', () => {
const mockDelta = {
someKey: generateLongString(2000)
};
JasmineSidetreeErrorValidator.expectSidetreeErrorToBeThrown(
() => { Delta.validateDelta(mockDelta); },
ErrorCode.DeltaExceedsMaximumSize
);
});

it('should throw sidetree error if delta is null', () => {
const mockDelta = null;
JasmineSidetreeErrorValidator.expectSidetreeErrorToBeThrown(
() => { Delta.validateDelta(mockDelta); },
ErrorCode.DeltaIsNullOrUndefined
);
});

it('should throw sidetree error if delta is undefined', () => {
const mockDelta = undefined;
JasmineSidetreeErrorValidator.expectSidetreeErrorToBeThrown(
() => { Delta.validateDelta(mockDelta); },
ErrorCode.DeltaIsNullOrUndefined
);
});

it('should not throw if delta size is valid', () => {
const mockDelta = {
someKey: 'some value'
};

try {
Delta.validateDelta(mockDelta);
} catch (e) {
fail(`Expected no error but got ${e}`);
}
});
});
});

0 comments on commit 26bc857

Please sign in to comment.