Robustness towards invalid URIs

This commit is contained in:
Christof Marti 2017-06-28 16:41:00 -07:00
parent bca6c99bd0
commit 5afaf1dd4e

View file

@ -236,7 +236,7 @@ export class ExtensionLinter {
const repo = tree && findNodeAtLocation(tree, ['repository', 'url']);
const info: PackageJsonInfo = {
isExtension: !!(engine && engine.type === 'string'),
hasHttpsRepository: !!(repo && repo.type === 'string' && repo.value && Uri.parse(repo.value).scheme.toLowerCase() === 'https')
hasHttpsRepository: !!(repo && repo.type === 'string' && repo.value && parseUri(repo.value).scheme.toLowerCase() === 'https')
};
const str = folder.toString();
const oldInfo = this.folderToPackageJsonInfo[str];
@ -269,7 +269,7 @@ export class ExtensionLinter {
}
private addDiagnostics(diagnostics: Diagnostic[], document: TextDocument, begin: number, end: number, src: string, context: Context, info: PackageJsonInfo) {
const uri = Uri.parse(src);
const uri = parseUri(src);
const scheme = uri.scheme.toLowerCase();
if (scheme && scheme !== 'https' && scheme !== 'data') {
@ -328,3 +328,15 @@ function fileExists(path: string): Promise<boolean> {
});
});
}
function parseUri(src: string) {
try {
return Uri.parse(src);
} catch (err) {
try {
return Uri.parse(encodeURI(src));
} catch (err) {
return Uri.parse('');
}
}
}