From bf38439ec2983a1c72e80a513ab81b680cd5dd8a Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Mon, 9 Jan 2023 15:23:53 +0000
Subject: [PATCH] fix(versioning/hashicorp): check valid before matches
 (#19742)

---
 lib/modules/versioning/hashicorp/index.ts | 4 +++-
 lib/util/package-rules/current-version.ts | 1 +
 lib/util/package-rules/index.spec.ts      | 1 +
 3 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/lib/modules/versioning/hashicorp/index.ts b/lib/modules/versioning/hashicorp/index.ts
index c736d22b42..d89564391d 100644
--- a/lib/modules/versioning/hashicorp/index.ts
+++ b/lib/modules/versioning/hashicorp/index.ts
@@ -1,3 +1,4 @@
+import { logger } from '../../../logger';
 import type { RangeStrategy } from '../../../types/versioning';
 import { api as npm } from '../npm';
 import type { NewValueConfig, VersioningApi } from '../types';
@@ -25,6 +26,7 @@ export function isValid(input: string): boolean {
     try {
       return npm.isValid(hashicorp2npm(input));
     } catch (err) {
+      logger.debug({ value: input }, 'Unsupported hashicorp versioning value');
       return false;
     }
   }
@@ -32,7 +34,7 @@ export function isValid(input: string): boolean {
 }
 
 function matches(version: string, range: string): boolean {
-  return npm.matches(version, hashicorp2npm(range));
+  return isValid(range) && npm.matches(version, hashicorp2npm(range));
 }
 
 function getSatisfyingVersion(
diff --git a/lib/util/package-rules/current-version.ts b/lib/util/package-rules/current-version.ts
index d93cb11e66..e01cb1a37e 100644
--- a/lib/util/package-rules/current-version.ts
+++ b/lib/util/package-rules/current-version.ts
@@ -39,6 +39,7 @@ export class CurrentVersionMatcher extends Matcher {
           isUnconstrainedValue ||
           !!(
             currentValue &&
+            version.isValid(currentValue) &&
             version.matches(matchCurrentVersionStr, currentValue)
           )
         );
diff --git a/lib/util/package-rules/index.spec.ts b/lib/util/package-rules/index.spec.ts
index f788a327a1..b521b99189 100644
--- a/lib/util/package-rules/index.spec.ts
+++ b/lib/util/package-rules/index.spec.ts
@@ -727,6 +727,7 @@ describe('util/package-rules/index', () => {
           x: 1,
         },
       ],
+      versioning: 'npm',
     };
     const res1 = applyPackageRules({
       ...config,
-- 
GitLab