From 01f43147cf34ff666815a58002f2548d28aa06a0 Mon Sep 17 00:00:00 2001 From: Praveen Adusumilli <47391951+adusumillipraveen@users.noreply.github.com> Date: Wed, 3 Jun 2020 16:36:19 +0100 Subject: [PATCH 1/3] Make Aliases freeChoice as it fails validation --- lib/config/definitions.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/config/definitions.ts b/lib/config/definitions.ts index 1bd317846885a1..db2fb0a748c640 100644 --- a/lib/config/definitions.ts +++ b/lib/config/definitions.ts @@ -634,6 +634,7 @@ const options: RenovateOptions[] = [ description: 'Aliases for registries, package manager specific', type: 'object', default: {}, + freeChoice: true, additionalProperties: { type: 'string', format: 'uri', From 71e31b3d02f4a72f00ff07c8e1aaa448996e5935 Mon Sep 17 00:00:00 2001 From: Praveen Adusumilli Date: Thu, 4 Jun 2020 12:42:57 +0100 Subject: [PATCH 2/3] Adding specific validations to aliases --- .../__snapshots__/validation.spec.ts.snap | 20 ++++++++ lib/config/definitions.ts | 1 - lib/config/validation.spec.ts | 46 +++++++++++++++++++ lib/config/validation.ts | 43 ++++++++++++----- package.json | 2 + yarn.lock | 10 ++++ 6 files changed, 110 insertions(+), 12 deletions(-) diff --git a/lib/config/__snapshots__/validation.spec.ts.snap b/lib/config/__snapshots__/validation.spec.ts.snap index fc5d501d847f2c..782e160bdcbf75 100644 --- a/lib/config/__snapshots__/validation.spec.ts.snap +++ b/lib/config/__snapshots__/validation.spec.ts.snap @@ -88,6 +88,24 @@ Array [ ] `; +exports[`config/validation validateConfig(config) errors if aliases depth is more than 1 1`] = ` +Array [ + Object { + "depName": "Configuration Error", + "message": "Invalid alias object configuration", + }, +] +`; + +exports[`config/validation validateConfig(config) errors if aliases have invalid url 1`] = ` +Array [ + Object { + "depName": "Configuration Error", + "message": "Invalid alias object configuration", + }, +] +`; + exports[`config/validation validateConfig(config) errors if regexManager fields are missing 1`] = ` Array [ Object { @@ -150,3 +168,5 @@ Array [ }, ] `; + +exports[`config/validation validateConfig(config) validates valid alias objects 1`] = `Array []`; diff --git a/lib/config/definitions.ts b/lib/config/definitions.ts index db2fb0a748c640..1bd317846885a1 100644 --- a/lib/config/definitions.ts +++ b/lib/config/definitions.ts @@ -634,7 +634,6 @@ const options: RenovateOptions[] = [ description: 'Aliases for registries, package manager specific', type: 'object', default: {}, - freeChoice: true, additionalProperties: { type: 'string', format: 'uri', diff --git a/lib/config/validation.spec.ts b/lib/config/validation.spec.ts index 5c0c219b9e51be..73a9088b2effe0 100644 --- a/lib/config/validation.spec.ts +++ b/lib/config/validation.spec.ts @@ -336,5 +336,51 @@ describe('config/validation', () => { expect(warnings).toHaveLength(0); expect(errors).toHaveLength(0); }); + + it('validates valid alias objects', async () => { + const config = { + aliases: { + example1: 'http://www.example.com', + example2: 'https://www.example2.com/example', + }, + }; + const { warnings, errors } = await configValidation.validateConfig( + config + ); + expect(warnings).toHaveLength(0); + expect(errors).toHaveLength(0); + expect(errors).toMatchSnapshot(); + }); + + it('errors if aliases depth is more than 1', async () => { + const config = { + aliases: { + sample: { + example1: 'http://www.example.com', + }, + }, + }; + const { warnings, errors } = await configValidation.validateConfig( + config + ); + expect(warnings).toHaveLength(0); + expect(errors).toHaveLength(1); + expect(errors).toMatchSnapshot(); + }); + + it('errors if aliases have invalid url', async () => { + const config = { + aliases: { + example1: 'noturl', + example2: 'http://www.example.com', + }, + }; + const { warnings, errors } = await configValidation.validateConfig( + config + ); + expect(warnings).toHaveLength(0); + expect(errors).toHaveLength(1); + expect(errors).toMatchSnapshot(); + }); }); }); diff --git a/lib/config/validation.ts b/lib/config/validation.ts index 773f908ccd7c42..0df445d231e94c 100644 --- a/lib/config/validation.ts +++ b/lib/config/validation.ts @@ -1,4 +1,5 @@ import is from '@sindresorhus/is'; +import isUrl from 'is-url'; import { regEx } from '../util/regex'; import * as template from '../util/template'; import { hasValidSchedule, hasValidTimezone } from '../workers/branch/schedule'; @@ -56,6 +57,17 @@ export async function validateConfig( return ignoredNodes.includes(key); } + function validateAliasObject(key: string, val: object): boolean { + if (key === 'aliases') { + for (const value of Object.values(val)) { + if (!isUrl(value)) { + return false; + } + } + } + return true; + } + for (const [key, val] of Object.entries(config)) { const currentPath = parentPath ? `${parentPath}.${key}` : key; if ( @@ -346,17 +358,26 @@ export async function validateConfig( } } else if (type === 'object' && currentPath !== 'compatibility') { if (is.object(val)) { - const ignoredObjects = options - .filter((option) => option.freeChoice) - .map((option) => option.name); - if (!ignoredObjects.includes(key)) { - const subValidation = await module.exports.validateConfig( - val, - isPreset, - currentPath - ); - warnings = warnings.concat(subValidation.warnings); - errors = errors.concat(subValidation.errors); + if (key === 'aliases') { + if (!validateAliasObject(key, val)) { + errors.push({ + depName: 'Configuration Error', + message: `Invalid alias object configuration`, + }); + } + } else { + const ignoredObjects = options + .filter((option) => option.freeChoice) + .map((option) => option.name); + if (!ignoredObjects.includes(key)) { + const subValidation = await module.exports.validateConfig( + val, + isPreset, + currentPath + ); + warnings = warnings.concat(subValidation.warnings); + errors = errors.concat(subValidation.errors); + } } } else { errors.push({ diff --git a/package.json b/package.json index 8df8b8150a325e..36c0a0b655f58a 100644 --- a/package.json +++ b/package.json @@ -135,6 +135,7 @@ "hasha": "5.2.0", "ignore": "5.1.8", "ini": "1.3.5", + "is-url": "^1.2.4", "js-yaml": "3.14.0", "json-dup-key-validator": "1.0.2", "json-stringify-pretty-compact": "2.0.0", @@ -199,6 +200,7 @@ "@types/got": "9.6.11", "@types/graceful-fs": "4.1.3", "@types/ini": "1.3.30", + "@types/is-url": "^1.2.28", "@types/jest": "25.2.3", "@types/js-yaml": "3.12.4", "@types/json5": "0.0.30", diff --git a/yarn.lock b/yarn.lock index 384d4d02020e28..b468b89304a970 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1542,6 +1542,11 @@ resolved "https://registry.yarnpkg.com/@types/ini/-/ini-1.3.30.tgz#d1485459c9fad84e937414b832a2adb649eab379" integrity sha512-2+iF8zPSbpU83UKE+PNd4r/MhwNAdyGpk3H+VMgEH3EhjFZq1kouLgRoZrmIcmoGX97xFvqdS44DkICR5Nz3tQ== +"@types/is-url@^1.2.28": + version "1.2.28" + resolved "https://registry.yarnpkg.com/@types/is-url/-/is-url-1.2.28.tgz#914dabd50546d9b0142806e42c72bc7c2b7e0787" + integrity sha1-kU2r1QVG2bAUKAbkLHK8fCt+B4c= + "@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1": version "2.0.2" resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.2.tgz#79d7a78bad4219f4c03d6557a1c72d9ca6ba62d5" @@ -5210,6 +5215,11 @@ is-typedarray@^1.0.0, is-typedarray@~1.0.0: resolved "https://registry.yarnpkg.com/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a" integrity sha1-5HnICFjfDBsR3dppQPlgEfzaSpo= +is-url@^1.2.4: + version "1.2.4" + resolved "https://registry.yarnpkg.com/is-url/-/is-url-1.2.4.tgz#04a4df46d28c4cff3d73d01ff06abeb318a1aa52" + integrity sha512-ITvGim8FhRiYe4IQ5uHSkj7pVaPDrCTkNd3yq3cV7iZAcJdHTUMPMEHcqSOy9xZ9qFenQCvi+2wjH9a1nXqHww== + is-whitespace-character@^1.0.0: version "1.0.4" resolved "https://registry.yarnpkg.com/is-whitespace-character/-/is-whitespace-character-1.0.4.tgz#0858edd94a95594c7c9dd0b5c174ec6e45ee4aa7" From 133ef922fb4f589aec9f636eff45f95e9ddff14d Mon Sep 17 00:00:00 2001 From: Praveen Adusumilli Date: Thu, 4 Jun 2020 13:58:50 +0100 Subject: [PATCH 3/3] use is.urlString --- lib/config/validation.ts | 3 +-- package.json | 2 -- yarn.lock | 10 ---------- 3 files changed, 1 insertion(+), 14 deletions(-) diff --git a/lib/config/validation.ts b/lib/config/validation.ts index 0df445d231e94c..db3eb7baec4517 100644 --- a/lib/config/validation.ts +++ b/lib/config/validation.ts @@ -1,5 +1,4 @@ import is from '@sindresorhus/is'; -import isUrl from 'is-url'; import { regEx } from '../util/regex'; import * as template from '../util/template'; import { hasValidSchedule, hasValidTimezone } from '../workers/branch/schedule'; @@ -60,7 +59,7 @@ export async function validateConfig( function validateAliasObject(key: string, val: object): boolean { if (key === 'aliases') { for (const value of Object.values(val)) { - if (!isUrl(value)) { + if (!is.urlString(value)) { return false; } } diff --git a/package.json b/package.json index 36c0a0b655f58a..8df8b8150a325e 100644 --- a/package.json +++ b/package.json @@ -135,7 +135,6 @@ "hasha": "5.2.0", "ignore": "5.1.8", "ini": "1.3.5", - "is-url": "^1.2.4", "js-yaml": "3.14.0", "json-dup-key-validator": "1.0.2", "json-stringify-pretty-compact": "2.0.0", @@ -200,7 +199,6 @@ "@types/got": "9.6.11", "@types/graceful-fs": "4.1.3", "@types/ini": "1.3.30", - "@types/is-url": "^1.2.28", "@types/jest": "25.2.3", "@types/js-yaml": "3.12.4", "@types/json5": "0.0.30", diff --git a/yarn.lock b/yarn.lock index b468b89304a970..384d4d02020e28 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1542,11 +1542,6 @@ resolved "https://registry.yarnpkg.com/@types/ini/-/ini-1.3.30.tgz#d1485459c9fad84e937414b832a2adb649eab379" integrity sha512-2+iF8zPSbpU83UKE+PNd4r/MhwNAdyGpk3H+VMgEH3EhjFZq1kouLgRoZrmIcmoGX97xFvqdS44DkICR5Nz3tQ== -"@types/is-url@^1.2.28": - version "1.2.28" - resolved "https://registry.yarnpkg.com/@types/is-url/-/is-url-1.2.28.tgz#914dabd50546d9b0142806e42c72bc7c2b7e0787" - integrity sha1-kU2r1QVG2bAUKAbkLHK8fCt+B4c= - "@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1": version "2.0.2" resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.2.tgz#79d7a78bad4219f4c03d6557a1c72d9ca6ba62d5" @@ -5215,11 +5210,6 @@ is-typedarray@^1.0.0, is-typedarray@~1.0.0: resolved "https://registry.yarnpkg.com/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a" integrity sha1-5HnICFjfDBsR3dppQPlgEfzaSpo= -is-url@^1.2.4: - version "1.2.4" - resolved "https://registry.yarnpkg.com/is-url/-/is-url-1.2.4.tgz#04a4df46d28c4cff3d73d01ff06abeb318a1aa52" - integrity sha512-ITvGim8FhRiYe4IQ5uHSkj7pVaPDrCTkNd3yq3cV7iZAcJdHTUMPMEHcqSOy9xZ9qFenQCvi+2wjH9a1nXqHww== - is-whitespace-character@^1.0.0: version "1.0.4" resolved "https://registry.yarnpkg.com/is-whitespace-character/-/is-whitespace-character-1.0.4.tgz#0858edd94a95594c7c9dd0b5c174ec6e45ee4aa7"