From 8fb9197d0cbb35aece07597f694d3bf851261fe0 Mon Sep 17 00:00:00 2001 From: David Straub <Scinvention@gmail.com> Date: Thu, 2 Sep 2021 13:03:51 -0400 Subject: [PATCH] fix(host-rules): support matchHost with a dot prefix (#11523) Co-authored-by: Rhys Arkins <rhys@arkins.net> --- docs/usage/configuration-options.md | 2 ++ lib/util/host-rules.spec.ts | 9 +++++++++ lib/util/host-rules.ts | 5 ++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md index 9b8baa781c..dd672c19b9 100644 --- a/docs/usage/configuration-options.md +++ b/docs/usage/configuration-options.md @@ -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`. 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. +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 diff --git a/lib/util/host-rules.spec.ts b/lib/util/host-rules.spec.ts index 1e01f83c89..7bc63e902d 100644 --- a/lib/util/host-rules.spec.ts +++ b/lib/util/host-rules.spec.ts @@ -208,6 +208,15 @@ describe('util/host-rules', () => { expect(find({ url: 'https://domain.com' }).token).toEqual('def'); 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', () => { add({ hostType: datasourceNuget.id, diff --git a/lib/util/host-rules.ts b/lib/util/host-rules.ts index d9c18f536b..c6887c5a0e 100644 --- a/lib/util/host-rules.ts +++ b/lib/util/host-rules.ts @@ -85,7 +85,10 @@ function matchesHost(rule: HostRule, search: HostRuleSearch): boolean { return false; } 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 { -- GitLab