From 0099ba17623577689a85b7fd1290b0fa6053e43a Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Mon, 22 Nov 2021 21:08:10 +0100
Subject: [PATCH] fix(config): check if matchStrings is iterable (#12759)

---
 lib/config/validation.spec.ts | 31 +++++++++++++++++++
 lib/config/validation.ts      | 58 ++++++++++++++++++-----------------
 2 files changed, 61 insertions(+), 28 deletions(-)

diff --git a/lib/config/validation.spec.ts b/lib/config/validation.spec.ts
index f850f7f291..0bf7c5a906 100644
--- a/lib/config/validation.spec.ts
+++ b/lib/config/validation.spec.ts
@@ -292,6 +292,29 @@ describe('config/validation', () => {
       expect(errors).toMatchSnapshot();
     });
     it('errors if no regexManager matchStrings', async () => {
+      const config = {
+        regexManagers: [
+          {
+            fileMatch: [],
+          },
+        ],
+      };
+      const { warnings, errors } = await configValidation.validateConfig(
+        config as any,
+        true
+      );
+      expect(warnings).toHaveLength(0);
+      expect(errors).toHaveLength(1);
+      expect(errors).toMatchInlineSnapshot(`
+        Array [
+          Object {
+            "message": "Each Regex Manager must contain a fileMatch array",
+            "topic": "Configuration Error",
+          },
+        ]
+      `);
+    });
+    it('errors if empty regexManager matchStrings', async () => {
       const config = {
         regexManagers: [
           {
@@ -306,6 +329,14 @@ describe('config/validation', () => {
       );
       expect(warnings).toHaveLength(0);
       expect(errors).toHaveLength(1);
+      expect(errors).toMatchInlineSnapshot(`
+        Array [
+          Object {
+            "message": "Each Regex Manager must contain a fileMatch array",
+            "topic": "Configuration Error",
+          },
+        ]
+      `);
     });
     it('errors if no regexManager fileMatch', async () => {
       const config = {
diff --git a/lib/config/validation.ts b/lib/config/validation.ts
index fb99cc6f3d..69aaca7ac9 100644
--- a/lib/config/validation.ts
+++ b/lib/config/validation.ts
@@ -408,39 +408,41 @@ export async function validateConfig(
                     )}`,
                   });
                 } else if (is.nonEmptyArray(regexManager.fileMatch)) {
-                  let validRegex = false;
-                  for (const matchString of regexManager.matchStrings) {
-                    try {
-                      regEx(matchString);
-                      validRegex = true;
-                    } catch (e) {
-                      errors.push({
-                        topic: 'Configuration Error',
-                        message: `Invalid regExp for ${currentPath}: \`${String(
-                          matchString
-                        )}\``,
-                      });
-                    }
-                  }
-                  if (validRegex) {
-                    const mandatoryFields = [
-                      'depName',
-                      'currentValue',
-                      'datasource',
-                    ];
-                    for (const field of mandatoryFields) {
-                      if (
-                        !regexManager[`${field}Template`] &&
-                        !regexManager.matchStrings.some((matchString) =>
-                          matchString.includes(`(?<${field}>`)
-                        )
-                      ) {
+                  if (is.nonEmptyArray(regexManager.matchStrings)) {
+                    let validRegex = false;
+                    for (const matchString of regexManager.matchStrings) {
+                      try {
+                        regEx(matchString);
+                        validRegex = true;
+                      } catch (e) {
                         errors.push({
                           topic: 'Configuration Error',
-                          message: `Regex Managers must contain ${field}Template configuration or regex group named ${field}`,
+                          message: `Invalid regExp for ${currentPath}: \`${String(
+                            matchString
+                          )}\``,
                         });
                       }
                     }
+                    if (validRegex) {
+                      const mandatoryFields = [
+                        'depName',
+                        'currentValue',
+                        'datasource',
+                      ];
+                      for (const field of mandatoryFields) {
+                        if (
+                          !regexManager[`${field}Template`] &&
+                          !regexManager.matchStrings.some((matchString) =>
+                            matchString.includes(`(?<${field}>`)
+                          )
+                        ) {
+                          errors.push({
+                            topic: 'Configuration Error',
+                            message: `Regex Managers must contain ${field}Template configuration or regex group named ${field}`,
+                          });
+                        }
+                      }
+                    }
                   }
                 } else {
                   errors.push({
-- 
GitLab