kibana/x-pack/legacy/plugins/code/common/git_url_utils.ts
2019-07-30 18:16:26 +08:00

41 lines
1.2 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { i18n } from '@kbn/i18n';
import GitUrlParse from 'git-url-parse';
// return true if the git url is valid, otherwise throw Error with
// exact reasons.
export function validateGitUrl(
url: string,
hostWhitelist?: string[],
protocolWhitelist?: string[]
): boolean {
const repo = GitUrlParse(url);
if (hostWhitelist && hostWhitelist.length > 0) {
const hostSet = new Set(hostWhitelist);
if (!hostSet.has(repo.source)) {
throw new Error(
i18n.translate('xpack.code.gitUrlUtil.urlNotWhitelistedMessage', {
defaultMessage: 'Git url host is not whitelisted.',
})
);
}
}
if (protocolWhitelist && protocolWhitelist.length > 0) {
const protocolSet = new Set(protocolWhitelist);
if (!protocolSet.has(repo.protocol)) {
throw new Error(
i18n.translate('xpack.code.gitUrlUtil.protocolNotWhitelistedMessage', {
defaultMessage: 'Git url protocol is not whitelisted.',
})
);
}
}
return true;
}