Skip to content

Commit

Permalink
fix(ref-imp): #898 - Used a URI library for URI validation
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai committed Oct 30, 2020
1 parent 175b5f7 commit bdeeb5f
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 79 deletions.
13 changes: 5 additions & 8 deletions lib/core/versions/latest/DocumentComposer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as URI from 'uri-js';
import ArrayMethods from './util/ArrayMethods';
import DidState from '../../models/DidState';
import DocumentModel from './models/DocumentModel';
Expand Down Expand Up @@ -304,16 +305,12 @@ export default class DocumentComposer {
throw new SidetreeError(ErrorCode.DocumentComposerPatchServiceTypeTooLong);
}

// `serviceEndpoint` validation.
// `serviceEndpoint` validations.
const serviceEndpoint = service.serviceEndpoint;
if (typeof serviceEndpoint === 'string') {
try {
// TODO: Issue #898 - `serviceEndpoint` string check should use a proper URI library.
// just want to validate url, no need to assign to variable, it will throw if not valid
// eslint-disable-next-line no-new
new URL(service.serviceEndpoint);
} catch {
throw new SidetreeError(ErrorCode.DocumentComposerPatchServiceEndpointNotValidUrl);
const uri = URI.parse(service.serviceEndpoint);
if (uri.error !== undefined) {
throw new SidetreeError(ErrorCode.DocumentComposerPatchServiceEndpointStringNotValidUri, `Service endpoint string '${serviceEndpoint}' is not a valid URI.`);
}
} else if (typeof serviceEndpoint === 'object') {
// Allow `object` type only if it is not an array.
Expand Down
2 changes: 1 addition & 1 deletion lib/core/versions/latest/ErrorCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default {
DocumentComposerPublicKeyInvalidPurpose: 'document_composer_public_key_invalid_purpose',
DocumentComposerPatchServiceEndpointCannotBeAnArray: 'document_composer_service_endpoint_cannot_be_an_array',
DocumentComposerPatchServiceEndpointMustBeStringOrNonArrayObject: 'document_composer_service_endpoint_must_be_string_or_non_array_object',
DocumentComposerPatchServiceEndpointNotValidUrl: 'document_composer_service_endpoint_not_valid_url',
DocumentComposerPatchServiceEndpointStringNotValidUri: 'document_composer_patch_service_endpoint_string_not_valid_uri',
DocumentComposerPatchServiceIdsNotArray: 'document_composer_service_ids_not_array',
DocumentComposerPatchServiceTypeNotString: 'document_composer_service_type_not_string',
DocumentComposerPatchServiceTypeTooLong: 'document_composer_service_type_too_long',
Expand Down
Loading

0 comments on commit bdeeb5f

Please sign in to comment.