diff --git a/lib/config/validation.spec.ts b/lib/config/validation.spec.ts index 7798ceaebba2fdf323b8c335ba117a2b71c536ff..b3d006ce6b1b619b461681d9963fbc6c659c7688 100644 --- a/lib/config/validation.spec.ts +++ b/lib/config/validation.spec.ts @@ -1075,5 +1075,75 @@ describe('config/validation', () => { }, ]); }); + + it('errors if allowedHeaders is empty or not defined', async () => { + GlobalConfig.set({}); + + const config = { + hostRules: [ + { + matchHost: 'https://domain.com/all-versions', + headers: { + 'X-Auth-Token': 'token', + }, + }, + ], + }; + const { warnings, errors } = await configValidation.validateConfig( + false, + config, + ); + expect(warnings).toHaveLength(0); + expect(errors).toMatchObject([ + { + message: + "hostRules header `X-Auth-Token` is not allowed by this bot's `allowedHeaders`.", + topic: 'Configuration Error', + }, + ]); + }); + }); + + describe('validateConfig() -> globaOnly options', () => { + it('validates hostRules.headers', async () => { + const config = { + hostRules: [ + { + matchHost: 'https://domain.com/all-versions', + headers: { + 'X-Auth-Token': 'token', + }, + }, + ], + allowedHeaders: ['X-Auth-Token'], + }; + const { warnings, errors } = await configValidation.validateConfig( + true, + config, + ); + expect(warnings).toHaveLength(0); + expect(errors).toHaveLength(0); + }); + + it('errors if hostRules.headers is defined but allowedHeaders is not', async () => { + const config = { + hostRules: [ + { + matchHost: 'https://domain.com/all-versions', + headers: { + 'X-Auth-Token': 'token', + }, + }, + ], + }; + const { errors } = await configValidation.validateConfig(true, config); + expect(errors).toMatchObject([ + { + message: + "hostRules header `X-Auth-Token` is not allowed by this bot's `allowedHeaders`.", + topic: 'Configuration Error', + }, + ]); + }); }); }); diff --git a/lib/config/validation.ts b/lib/config/validation.ts index f1d0a36ca80292632dc21185b7f901a59d24e587..240f9a0403bc602da04003b3fafacfd5c61c83c4 100644 --- a/lib/config/validation.ts +++ b/lib/config/validation.ts @@ -710,7 +710,9 @@ export async function validateConfig( } if (key === 'hostRules' && is.array(val)) { - const allowedHeaders = GlobalConfig.get('allowedHeaders', []); + const allowedHeaders = isGlobalConfig + ? (config.allowedHeaders as string[]) ?? [] + : GlobalConfig.get('allowedHeaders', []); for (const rule of val as HostRule[]) { if (!rule.headers) { continue;