forked from renovatebot/renovate
/
host-rules.ts
168 lines (152 loc) · 4.59 KB
/
host-rules.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import is from '@sindresorhus/is';
import merge from 'deepmerge';
import { logger } from '../logger';
import type { HostRule, HostRuleSearchResult } from '../types';
import { clone } from './clone';
import * as sanitize from './sanitize';
import { toBase64 } from './string';
import { parseUrl, validateUrl } from './url';
let hostRules: HostRule[] = [];
export function add(params: HostRule): void {
const rule = clone(params);
const confidentialFields: (keyof HostRule)[] = ['password', 'token'];
if (rule.matchHost) {
const parsedUrl = parseUrl(rule.matchHost);
rule.resolvedHost = parsedUrl?.hostname ?? rule.matchHost;
confidentialFields.forEach((field) => {
if (rule[field]) {
logger.debug(
// TODO: types (#7154)
`Adding ${field} authentication for ${rule.matchHost!} to hostRules`
);
}
});
}
confidentialFields.forEach((field) => {
const secret = rule[field];
if (is.string(secret) && secret.length > 3) {
sanitize.addSecretForSanitizing(secret);
}
});
if (rule.username && rule.password) {
sanitize.addSecretForSanitizing(
toBase64(`${rule.username}:${rule.password}`)
);
}
hostRules.push(rule);
}
export interface HostRuleSearch {
hostType?: string;
url?: string;
}
function isEmptyRule(rule: HostRule): boolean {
return !rule.hostType && !rule.resolvedHost;
}
function isHostTypeRule(rule: HostRule): boolean {
return !!rule.hostType && !rule.resolvedHost;
}
function isHostOnlyRule(rule: HostRule): boolean {
return !rule.hostType && !!rule.matchHost;
}
function isMultiRule(rule: HostRule): boolean {
return !!rule.hostType && !!rule.resolvedHost;
}
function matchesHostType(rule: HostRule, search: HostRuleSearch): boolean {
return rule.hostType === search.hostType;
}
function matchesHost(rule: HostRule, search: HostRuleSearch): boolean {
// istanbul ignore if
if (!rule.matchHost) {
return false;
}
if (search.url && validateUrl(rule.matchHost)) {
return search.url.startsWith(rule.matchHost);
}
const parsedUrl = search.url ? parseUrl(search.url) : null;
if (!parsedUrl?.hostname) {
return false;
}
const { hostname } = parsedUrl;
const dotPrefixedMatchHost = rule.matchHost.startsWith('.')
? rule.matchHost
: `.${rule.matchHost}`;
return hostname === rule.matchHost || hostname.endsWith(dotPrefixedMatchHost);
}
function prioritizeLongestMatchHost(rule1: HostRule, rule2: HostRule): number {
// istanbul ignore if: won't happen in practice
if (!rule1.matchHost || !rule2.matchHost) {
return 0;
}
return rule1.matchHost.length - rule2.matchHost.length;
}
export function find(search: HostRuleSearch): HostRuleSearchResult {
if (!(search.hostType || search.url)) {
logger.warn({ search }, 'Invalid hostRules search');
return {};
}
let res = {} as any as HostRule;
// First, apply empty rule matches
hostRules
.filter((rule) => isEmptyRule(rule))
.forEach((rule) => {
res = merge(res, rule);
});
// Next, find hostType-only matches
hostRules
.filter((rule) => isHostTypeRule(rule) && matchesHostType(rule, search))
.forEach((rule) => {
res = merge(res, rule);
});
hostRules
.filter((rule) => isHostOnlyRule(rule) && matchesHost(rule, search))
.sort(prioritizeLongestMatchHost)
.forEach((rule) => {
res = merge(res, rule);
});
// Finally, find combination matches
hostRules
.filter(
(rule) =>
isMultiRule(rule) &&
matchesHostType(rule, search) &&
matchesHost(rule, search)
)
.sort(prioritizeLongestMatchHost)
.forEach((rule) => {
res = merge(res, rule);
});
delete res.hostType;
delete res.resolvedHost;
delete res.matchHost;
return res;
}
export function hosts({ hostType }: { hostType: string }): string[] {
return hostRules
.filter((rule) => rule.hostType === hostType)
.map((rule) => rule.resolvedHost)
.filter(is.truthy);
}
export function hostType({ url }: { url: string }): string | null {
return (
hostRules
.filter((rule) => matchesHost(rule, { url }))
.sort(prioritizeLongestMatchHost)
.map((rule) => rule.hostType)
.filter(is.truthy)
.pop() ?? null
);
}
export function findAll({ hostType }: { hostType: string }): HostRule[] {
return hostRules.filter((rule) => rule.hostType === hostType);
}
/**
* @returns a deep copy of all known host rules without any filtering
*/
export function getAll(): HostRule[] {
return clone(hostRules);
}
export function clear(): void {
logger.debug('Clearing hostRules');
hostRules = [];
sanitize.clearSanitizedSecretsList();
}