2019-05-24 15:40:39 +00:00
|
|
|
import merge from 'deepmerge';
|
2019-07-15 09:04:05 +00:00
|
|
|
import { logger } from '../logger';
|
2020-03-02 11:06:16 +00:00
|
|
|
import { HostRule } from '../types';
|
2021-05-01 16:19:38 +00:00
|
|
|
import { clone } from './clone';
|
2020-05-01 16:03:48 +00:00
|
|
|
import * as sanitize from './sanitize';
|
2021-05-06 07:57:44 +00:00
|
|
|
import { parseUrl, validateUrl } from './url';
|
2019-05-24 15:40:39 +00:00
|
|
|
|
|
|
|
let hostRules: HostRule[] = [];
|
|
|
|
|
2021-05-13 20:53:18 +00:00
|
|
|
const legacyHostFields = ['hostName', 'domainName', 'baseUrl'];
|
2021-05-01 21:18:14 +00:00
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
export function add(params: HostRule): void {
|
2021-05-13 20:53:18 +00:00
|
|
|
const rule = clone(params);
|
|
|
|
const matchedFields = legacyHostFields.filter((field) => rule[field]);
|
|
|
|
if (matchedFields.length) {
|
|
|
|
logger.warn(
|
|
|
|
`Legacy hostRules fields ${matchedFields.join(
|
|
|
|
'+'
|
|
|
|
)} should be migrated to "matchHost"`
|
2021-05-01 21:18:14 +00:00
|
|
|
);
|
2021-05-13 20:53:18 +00:00
|
|
|
if (rule.matchHost || matchedFields.length > 1) {
|
|
|
|
matchedFields.push('matchHost');
|
|
|
|
throw new Error(
|
|
|
|
`hostRules cannot contain more than one host-matching field - use "matchHost" only. Found: [${matchedFields.join(
|
|
|
|
', '
|
|
|
|
)}]`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
rule.matchHost = rule[matchedFields[0]];
|
|
|
|
delete rule[matchedFields[0]];
|
2018-07-06 05:26:36 +00:00
|
|
|
}
|
2021-05-13 20:53:18 +00:00
|
|
|
|
2019-09-07 12:51:00 +00:00
|
|
|
const confidentialFields = ['password', 'token'];
|
2021-05-13 20:53:18 +00:00
|
|
|
if (rule.matchHost) {
|
|
|
|
const parsedUrl = parseUrl(rule.matchHost);
|
|
|
|
rule.resolvedHost = parsedUrl?.hostname || rule.matchHost;
|
2021-04-20 08:52:38 +00:00
|
|
|
confidentialFields.forEach((field) => {
|
2021-05-13 20:53:18 +00:00
|
|
|
if (rule[field]) {
|
2021-04-20 08:52:38 +00:00
|
|
|
logger.debug(
|
2021-05-13 20:53:18 +00:00
|
|
|
`Adding ${field} authentication for ${rule.matchHost} to hostRules`
|
2021-04-20 08:52:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-04-12 16:09:36 +00:00
|
|
|
confidentialFields.forEach((field) => {
|
2021-05-13 20:53:18 +00:00
|
|
|
const secret = rule[field];
|
2020-03-17 11:15:22 +00:00
|
|
|
if (secret && secret.length > 3) {
|
|
|
|
sanitize.add(secret);
|
|
|
|
}
|
2019-09-07 12:51:00 +00:00
|
|
|
});
|
2021-05-13 20:53:18 +00:00
|
|
|
if (rule.username && rule.password) {
|
|
|
|
const secret = Buffer.from(`${rule.username}:${rule.password}`).toString(
|
|
|
|
'base64'
|
|
|
|
);
|
2019-09-12 10:48:31 +00:00
|
|
|
sanitize.add(secret);
|
2019-09-07 12:51:00 +00:00
|
|
|
}
|
2021-05-13 20:53:18 +00:00
|
|
|
hostRules.push(rule);
|
2019-05-24 15:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface HostRuleSearch {
|
|
|
|
hostType?: string;
|
2019-07-17 08:14:56 +00:00
|
|
|
url?: string;
|
2019-05-24 15:40:39 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
function isEmptyRule(rule: HostRule): boolean {
|
2021-05-01 21:30:24 +00:00
|
|
|
return !rule.hostType && !rule.resolvedHost;
|
2019-05-24 15:40:39 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
function isHostTypeRule(rule: HostRule): boolean {
|
2021-05-01 21:30:24 +00:00
|
|
|
return rule.hostType && !rule.resolvedHost;
|
2019-05-24 15:40:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 07:57:44 +00:00
|
|
|
function isHostOnlyRule(rule: HostRule): boolean {
|
|
|
|
return !rule.hostType && !!rule.matchHost;
|
|
|
|
}
|
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
function isMultiRule(rule: HostRule): boolean {
|
2021-05-01 21:30:24 +00:00
|
|
|
return rule.hostType && !!rule.resolvedHost;
|
2019-05-24 15:40:39 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
function matchesHostType(rule: HostRule, search: HostRuleSearch): boolean {
|
2019-05-24 15:40:39 +00:00
|
|
|
return rule.hostType === search.hostType;
|
|
|
|
}
|
|
|
|
|
2021-05-06 07:57:44 +00:00
|
|
|
function matchesHost(rule: HostRule, search: HostRuleSearch): boolean {
|
|
|
|
if (validateUrl(rule.matchHost)) {
|
|
|
|
return search.url.startsWith(rule.matchHost);
|
|
|
|
}
|
|
|
|
const parsedUrl = parseUrl(search.url);
|
|
|
|
if (!parsedUrl?.hostname) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const { hostname } = parsedUrl;
|
|
|
|
return hostname === rule.matchHost || hostname.endsWith(`.${rule.matchHost}`);
|
|
|
|
}
|
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
export function find(search: HostRuleSearch): HostRule {
|
2019-05-24 15:40:39 +00:00
|
|
|
if (!(search.hostType || search.url)) {
|
|
|
|
logger.warn({ search }, 'Invalid hostRules search');
|
2019-05-25 05:49:26 +00:00
|
|
|
return {};
|
2018-09-13 04:48:08 +00:00
|
|
|
}
|
2019-05-24 15:40:39 +00:00
|
|
|
let res = ({} as any) as HostRule;
|
|
|
|
// First, apply empty rule matches
|
|
|
|
hostRules
|
2020-04-12 16:09:36 +00:00
|
|
|
.filter((rule) => isEmptyRule(rule))
|
|
|
|
.forEach((rule) => {
|
2019-05-24 15:40:39 +00:00
|
|
|
res = merge(res, rule);
|
|
|
|
});
|
|
|
|
// Next, find hostType-only matches
|
|
|
|
hostRules
|
2020-04-12 16:09:36 +00:00
|
|
|
.filter((rule) => isHostTypeRule(rule) && matchesHostType(rule, search))
|
|
|
|
.forEach((rule) => {
|
2019-05-24 15:40:39 +00:00
|
|
|
res = merge(res, rule);
|
|
|
|
});
|
2021-05-06 07:57:44 +00:00
|
|
|
hostRules
|
|
|
|
.filter((rule) => isHostOnlyRule(rule) && matchesHost(rule, search))
|
|
|
|
.forEach((rule) => {
|
|
|
|
res = merge(res, rule);
|
|
|
|
});
|
2019-05-24 15:40:39 +00:00
|
|
|
// Finally, find combination matches
|
|
|
|
hostRules
|
|
|
|
.filter(
|
2020-04-12 16:09:36 +00:00
|
|
|
(rule) =>
|
2019-05-24 15:40:39 +00:00
|
|
|
isMultiRule(rule) &&
|
|
|
|
matchesHostType(rule, search) &&
|
2021-05-13 20:53:18 +00:00
|
|
|
matchesHost(rule, search)
|
2019-05-24 15:40:39 +00:00
|
|
|
)
|
2020-04-12 16:09:36 +00:00
|
|
|
.forEach((rule) => {
|
2019-05-24 15:40:39 +00:00
|
|
|
res = merge(res, rule);
|
|
|
|
});
|
|
|
|
delete res.hostType;
|
2021-05-01 16:19:38 +00:00
|
|
|
delete res.resolvedHost;
|
2021-05-06 07:57:44 +00:00
|
|
|
delete res.matchHost;
|
2019-05-24 15:40:39 +00:00
|
|
|
return res;
|
2018-07-06 05:26:36 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
export function hosts({ hostType }: { hostType: string }): string[] {
|
2019-05-24 15:40:39 +00:00
|
|
|
return hostRules
|
2020-04-12 16:09:36 +00:00
|
|
|
.filter((rule) => rule.hostType === hostType)
|
2021-05-01 21:34:16 +00:00
|
|
|
.map((rule) => rule.resolvedHost)
|
2019-05-24 15:40:39 +00:00
|
|
|
.filter(Boolean);
|
2018-07-06 05:26:36 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 13:37:35 +00:00
|
|
|
export function findAll({ hostType }: { hostType: string }): HostRule[] {
|
2020-04-12 16:09:36 +00:00
|
|
|
return hostRules.filter((rule) => rule.hostType === hostType);
|
2020-02-11 13:37:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
export function clear(): void {
|
2021-04-20 08:52:38 +00:00
|
|
|
logger.debug('Clearing hostRules');
|
2019-05-24 15:40:39 +00:00
|
|
|
hostRules = [];
|
2019-09-12 10:48:31 +00:00
|
|
|
sanitize.clear();
|
2018-07-06 05:26:36 +00:00
|
|
|
}
|