Skip to content

Commit

Permalink
fix(ref-imp): added size limit for writer_lock_id field in anchor file
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai authored Aug 12, 2020
1 parent a84f079 commit be481c0
Show file tree
Hide file tree
Showing 12 changed files with 924 additions and 7,330 deletions.
25 changes: 22 additions & 3 deletions lib/core/versions/0.9.0/AnchorFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ export default class AnchorFile {
throw new SidetreeError(ErrorCode.AnchorFileMissingOperationsProperty);
}

if (anchorFileModel.hasOwnProperty('writer_lock_id') &&
typeof anchorFileModel.writer_lock_id !== 'string') {
throw new SidetreeError(ErrorCode.AnchorFileWriterLockIPropertyNotString);
// `writer_lock_id` validations.
if (anchorFileModel.hasOwnProperty('writer_lock_id')) {
if (typeof anchorFileModel.writer_lock_id !== 'string') {
throw new SidetreeError(ErrorCode.AnchorFileWriterLockIdPropertyNotString);
}

AnchorFile.validateWriterLockId(anchorFileModel.writer_lock_id);
}

// Map file hash validations.
Expand Down Expand Up @@ -157,6 +161,10 @@ export default class AnchorFile {
deactivateOperationArray: DeactivateOperation[]
): Promise<AnchorFileModel> {

if (writerLockId !== undefined) {
AnchorFile.validateWriterLockId(writerLockId);
}

const createOperations = createOperationArray.map(operation => {
return {
suffix_data: operation.encodedSuffixData
Expand Down Expand Up @@ -206,4 +214,15 @@ export default class AnchorFile {

return Compressor.compress(anchorFileBuffer);
}

private static validateWriterLockId (writerLockId: string) {
// Max size check.
const writerLockIdSizeInBytes = Buffer.from(writerLockId).length;
if (writerLockIdSizeInBytes > ProtocolParameters.maxWriterLockIdInBytes) {
throw new SidetreeError(
ErrorCode.AnchorFileWriterLockIdExceededMaxSize,
`Writer lock ID of ${writerLockIdSizeInBytes} bytes exceeded the maximum size of ${ProtocolParameters.maxWriterLockIdInBytes} bytes.`
);
}
}
}
3 changes: 2 additions & 1 deletion lib/core/versions/0.9.0/ErrorCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export default {
AnchorFileRecoverPropertyNotArray: 'anchor_file_recover_property_not_array',
AnchorFileDeactivatePropertyNotArray: 'anchor_file_deactivate_property_not_array',
AnchorFileUnexpectedPropertyInOperations: 'anchor_file_unexpected_property_in_operations',
AnchorFileWriterLockIPropertyNotString: 'anchor_file_writer_lock_id_property_not_string',
AnchorFileWriterLockIdExceededMaxSize: 'anchor_file_writer_lock_id_exceeded_max_size',
AnchorFileWriterLockIdPropertyNotString: 'anchor_file_writer_lock_id_property_not_string',
BatchWriterAlreadyHasOperationForDid: 'batch_writer_already_has_operation_for_did',
CasFileHashNotValid: 'cas_file_hash_not_valid',
CasFileNotAFile: 'cas_file_not_a file',
Expand Down
2 changes: 2 additions & 0 deletions lib/core/versions/0.9.0/models/ProtocolParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export default interface ProtocolParameters {
maxNumberOfTransactionsPerTransactionTime: number;
/** Maximum operations per batch. */
maxOperationsPerBatch: number;
/** Maximum writer lock ID in bytes. */
maxWriterLockIdInBytes: number;
/** The multiplier that converts the normalized fee from blockchain into a 'per operation' fee. */
normalizedFeeToPerOperationFeeMultiplier: number;
/** The multiplier that converts the normalized 'per operation' fee into 'per operation lock amount' */
Expand Down
1 change: 1 addition & 0 deletions lib/core/versions/0.9.0/protocol-parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"maxOperationsPerBatch": 10000,
"maxDeltaSizeInBytes": 1000,
"maxNumberOfOperationsForNoValueTimeLock": 100,
"maxWriterLockIdInBytes": 200,
"normalizedFeeToPerOperationFeeMultiplier": 0.01,
"valueTimeLockAmountMultiplier": 600
}
25 changes: 22 additions & 3 deletions lib/core/versions/latest/AnchorFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ export default class AnchorFile {
throw new SidetreeError(ErrorCode.AnchorFileMissingOperationsProperty);
}

if (anchorFileModel.hasOwnProperty('writer_lock_id') &&
typeof anchorFileModel.writer_lock_id !== 'string') {
throw new SidetreeError(ErrorCode.AnchorFileWriterLockIPropertyNotString);
// `writer_lock_id` validations.
if (anchorFileModel.hasOwnProperty('writer_lock_id')) {
if (typeof anchorFileModel.writer_lock_id !== 'string') {
throw new SidetreeError(ErrorCode.AnchorFileWriterLockIdPropertyNotString);
}

AnchorFile.validateWriterLockId(anchorFileModel.writer_lock_id);
}

// Map file hash validations.
Expand Down Expand Up @@ -157,6 +161,10 @@ export default class AnchorFile {
deactivateOperationArray: DeactivateOperation[]
): Promise<AnchorFileModel> {

if (writerLockId !== undefined) {
AnchorFile.validateWriterLockId(writerLockId);
}

const createOperations = createOperationArray.map(operation => {
return {
suffix_data: operation.encodedSuffixData
Expand Down Expand Up @@ -206,4 +214,15 @@ export default class AnchorFile {

return Compressor.compress(anchorFileBuffer);
}

private static validateWriterLockId (writerLockId: string) {
// Max size check.
const writerLockIdSizeInBytes = Buffer.from(writerLockId).length;
if (writerLockIdSizeInBytes > ProtocolParameters.maxWriterLockIdInBytes) {
throw new SidetreeError(
ErrorCode.AnchorFileWriterLockIdExceededMaxSize,
`Writer lock ID of ${writerLockIdSizeInBytes} bytes exceeded the maximum size of ${ProtocolParameters.maxWriterLockIdInBytes} bytes.`
);
}
}
}
3 changes: 2 additions & 1 deletion lib/core/versions/latest/ErrorCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export default {
AnchorFileRecoverPropertyNotArray: 'anchor_file_recover_property_not_array',
AnchorFileDeactivatePropertyNotArray: 'anchor_file_deactivate_property_not_array',
AnchorFileUnexpectedPropertyInOperations: 'anchor_file_unexpected_property_in_operations',
AnchorFileWriterLockIPropertyNotString: 'anchor_file_writer_lock_id_property_not_string',
AnchorFileWriterLockIdExceededMaxSize: 'anchor_file_writer_lock_id_exceeded_max_size',
AnchorFileWriterLockIdPropertyNotString: 'anchor_file_writer_lock_id_property_not_string',
BatchWriterAlreadyHasOperationForDid: 'batch_writer_already_has_operation_for_did',
CasFileHashNotValid: 'cas_file_hash_not_valid',
CasFileNotAFile: 'cas_file_not_a file',
Expand Down
2 changes: 2 additions & 0 deletions lib/core/versions/latest/models/ProtocolParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export default interface ProtocolParameters {
maxNumberOfTransactionsPerTransactionTime: number;
/** Maximum operations per batch. */
maxOperationsPerBatch: number;
/** Maximum writer lock ID in bytes. */
maxWriterLockIdInBytes: number;
/** The multiplier that converts the normalized fee from blockchain into a 'per operation' fee. */
normalizedFeeToPerOperationFeeMultiplier: number;
/** The multiplier that converts the normalized 'per operation' fee into 'per operation lock amount' */
Expand Down
1 change: 1 addition & 0 deletions lib/core/versions/latest/protocol-parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"maxOperationsPerBatch": 10000,
"maxDeltaSizeInBytes": 1000,
"maxNumberOfOperationsForNoValueTimeLock": 100,
"maxWriterLockIdInBytes": 200,
"normalizedFeeToPerOperationFeeMultiplier": 0.01,
"valueTimeLockAmountMultiplier": 600
}
Loading

0 comments on commit be481c0

Please sign in to comment.