Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hostRules/matchHost): massage and validate #29487

Merged
2 changes: 2 additions & 0 deletions lib/config/migrations/custom/host-rules-migration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('config/migrations/custom/host-rules-migration', () => {
{ hostName: 'some.domain.com', token: '123test' },
{ endpoint: 'domain.com/', token: '123test' },
{ host: 'some.domain.com', token: '123test' },
{ matchHost: 'some.domain.com:8080', token: '123test' },
],
} as any,
{
Expand Down Expand Up @@ -58,6 +59,7 @@ describe('config/migrations/custom/host-rules-migration', () => {
{ matchHost: 'some.domain.com', token: '123test' },
{ matchHost: 'https://domain.com/', token: '123test' },
{ matchHost: 'some.domain.com', token: '123test' },
{ matchHost: 'https://some.domain.com:8080', token: '123test' },
],
},
);
Expand Down
2 changes: 2 additions & 0 deletions lib/config/migrations/custom/host-rules-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ function validateHostRule(rule: LegacyHostRule & HostRule): void {
function massageUrl(url: string): string {
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
if (!url.includes('://') && url.includes('/')) {
return 'https://' + url;
} else if (!url.includes('://') && url.includes(':')) {
return 'https://' + url;
} else {
return url;
}
Expand Down
42 changes: 42 additions & 0 deletions lib/config/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,48 @@ describe('config/validation', () => {
]);
});

it('errors if invalid matchHost values in hostRules', async () => {
GlobalConfig.set({ allowedHeaders: ['X-*'] });

const config = {
hostRules: [
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
{
matchHost: '://',
token: 'token',
},
{
matchHost: '',
token: 'token',
},
{
matchHost: undefined,
token: 'token',
},
{
hostType: 'github',
token: 'token',
},
],
};
const { errors } = await configValidation.validateConfig('repo', config);
expect(errors).toMatchObject([
{
topic: 'Configuration Error',
message:
'Configuration option `hostRules[2].matchHost` should be a string',
},
{
topic: 'Configuration Error',
message:
'Invalid value for hostRules matchHost. It cannot be an empty string.',
},
{
topic: 'Configuration Error',
message: 'hostRules matchHost `://` is not a valid URL.',
},
]);
});

it('errors if forbidden header in hostRules', async () => {
GlobalConfig.set({ allowedHeaders: ['X-*'] });

Expand Down
18 changes: 18 additions & 0 deletions lib/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
matchRegexOrGlobList,
} from '../util/string-match';
import * as template from '../util/template';
import { parseUrl } from '../util/url';
import {
hasValidSchedule,
hasValidTimezone,
Expand Down Expand Up @@ -814,6 +815,23 @@ export async function validateConfig(
? (config.allowedHeaders as string[]) ?? []
: GlobalConfig.get('allowedHeaders', []);
for (const rule of val as HostRule[]) {
if (is.nonEmptyString(rule.matchHost)) {
if (rule.matchHost.includes('://')) {
if (is.null_(parseUrl(rule.matchHost))) {
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
errors.push({
topic: 'Configuration Error',
message: `hostRules matchHost \`${rule.matchHost}\` is not a valid URL.`,
});
}
}
} else if (is.emptyString(rule.matchHost)) {
errors.push({
topic: 'Configuration Error',
message:
'Invalid value for hostRules matchHost. It cannot be an empty string.',
});
}

if (!rule.headers) {
continue;
}
Expand Down