Skip to content
Snippets Groups Projects
Unverified Commit 8fb9197d authored by David Straub's avatar David Straub Committed by GitHub
Browse files

fix(host-rules): support matchHost with a dot prefix (#11523)


Co-authored-by: default avatarRhys Arkins <rhys@arkins.net>
parent f9f4d29a
No related branches found
No related tags found
No related merge requests found
...@@ -1024,6 +1024,8 @@ Example: ...@@ -1024,6 +1024,8 @@ Example:
This can be a base URL (e.g. `https://api.github.com`) or a hostname like `github.com` or `api.github.com`. This can be a base URL (e.g. `https://api.github.com`) or a hostname like `github.com` or `api.github.com`.
If the value starts with `http(s)` then it will only match against URLs which start with the full base URL. If the value starts with `http(s)` then it will only match against URLs which start with the full base URL.
Otherwise, it will be matched by checking if the URL's hostname matches the `matchHost` directly or ends with it. Otherwise, it will be matched by checking if the URL's hostname matches the `matchHost` directly or ends with it.
When checking the end of the hostname, a single dot is prefixed to the value of `matchHost`, if one is not
already present, to ensure it can only match against whole domain segments.
### timeout ### timeout
......
...@@ -208,6 +208,15 @@ describe('util/host-rules', () => { ...@@ -208,6 +208,15 @@ describe('util/host-rules', () => {
expect(find({ url: 'https://domain.com' }).token).toEqual('def'); expect(find({ url: 'https://domain.com' }).token).toEqual('def');
expect(find({ url: 'httpsdomain.com' }).token).toBeUndefined(); expect(find({ url: 'httpsdomain.com' }).token).toBeUndefined();
}); });
it('matches on matchHost with dot prefix', () => {
add({
matchHost: '.domain.com',
token: 'def',
});
expect(find({ url: 'https://api.domain.com' }).token).toEqual('def');
expect(find({ url: 'https://domain.com' }).token).toBeUndefined();
expect(find({ url: 'httpsdomain.com' }).token).toBeUndefined();
});
it('matches on hostType and endpoint', () => { it('matches on hostType and endpoint', () => {
add({ add({
hostType: datasourceNuget.id, hostType: datasourceNuget.id,
......
...@@ -85,7 +85,10 @@ function matchesHost(rule: HostRule, search: HostRuleSearch): boolean { ...@@ -85,7 +85,10 @@ function matchesHost(rule: HostRule, search: HostRuleSearch): boolean {
return false; return false;
} }
const { hostname } = parsedUrl; const { hostname } = parsedUrl;
return hostname === rule.matchHost || hostname.endsWith(`.${rule.matchHost}`); const dotPrefixedMatchHost = rule.matchHost.startsWith('.')
? rule.matchHost
: `.${rule.matchHost}`;
return hostname === rule.matchHost || hostname.endsWith(dotPrefixedMatchHost);
} }
export function find(search: HostRuleSearch): HostRule { export function find(search: HostRuleSearch): HostRule {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment