From 243c25ff4d78a92ba9b8d40161e001fa218eb001 Mon Sep 17 00:00:00 2001
From: Craig Andrews <candrews@integralblue.com>
Date: Tue, 9 Mar 2021 15:04:10 -0500
Subject: [PATCH] fix: registry returned by getRegistryRepository must be a URL
 (#8951)

The registry returned by getRegistryRepository must be a URL (including the scheme portion).

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
---
 .../docker/__snapshots__/index.spec.ts.snap      | 16 +++++++++++++++-
 lib/datasource/docker/index.spec.ts              | 14 ++++++++++++++
 lib/datasource/docker/index.ts                   | 10 ++++++++--
 3 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/lib/datasource/docker/__snapshots__/index.spec.ts.snap b/lib/datasource/docker/__snapshots__/index.spec.ts.snap
index 40b5985e56..e734330d07 100644
--- a/lib/datasource/docker/__snapshots__/index.spec.ts.snap
+++ b/lib/datasource/docker/__snapshots__/index.spec.ts.snap
@@ -392,7 +392,21 @@ Object {
 
 exports[`datasource/docker/index getRegistryRepository supports registryUrls 1`] = `
 Object {
-  "registry": "my.local.registry/prefix",
+  "registry": "https://my.local.registry/prefix",
+  "repository": "image",
+}
+`;
+
+exports[`datasource/docker/index getRegistryRepository supports http registryUrls 1`] = `
+Object {
+  "registry": "http://my.local.registry/prefix",
+  "repository": "image",
+}
+`;
+
+exports[`datasource/docker/index getRegistryRepository supports schemeless registryUrls 1`] = `
+Object {
+  "registry": "https://my.local.registry/prefix",
   "repository": "image",
 }
 `;
diff --git a/lib/datasource/docker/index.spec.ts b/lib/datasource/docker/index.spec.ts
index cdcb2dd7ad..f317cdb58e 100644
--- a/lib/datasource/docker/index.spec.ts
+++ b/lib/datasource/docker/index.spec.ts
@@ -71,6 +71,20 @@ describe(getName(__filename), () => {
       );
       expect(res).toMatchSnapshot();
     });
+    it('supports http registryUrls', () => {
+      const res = docker.getRegistryRepository(
+        'my.local.registry/prefix/image',
+        'http://my.local.registry/prefix'
+      );
+      expect(res).toMatchSnapshot();
+    });
+    it('supports schemeless registryUrls', () => {
+      const res = docker.getRegistryRepository(
+        'my.local.registry/prefix/image',
+        'my.local.registry/prefix'
+      );
+      expect(res).toMatchSnapshot();
+    });
   });
   describe('getDigest', () => {
     it('returns null if no token', async () => {
diff --git a/lib/datasource/docker/index.ts b/lib/datasource/docker/index.ts
index 2939ed344e..ff9e4f5336 100644
--- a/lib/datasource/docker/index.ts
+++ b/lib/datasource/docker/index.ts
@@ -11,6 +11,7 @@ import { ExternalHostError } from '../../types/errors/external-host-error';
 import * as packageCache from '../../util/cache/package';
 import * as hostRules from '../../util/host-rules';
 import { Http, HttpResponse } from '../../util/http';
+import { ensureTrailingSlash, trimTrailingSlash } from '../../util/url';
 import * as dockerVersioning from '../../versioning/docker';
 import type { GetReleasesConfig, ReleaseResult } from '../types';
 import { Image, ImageList, MediaType } from './types';
@@ -66,9 +67,14 @@ export function getRegistryRepository(
   registryUrl: string
 ): RegistryRepository {
   if (registryUrl !== defaultRegistryUrls[0]) {
-    const registry = registryUrl.replace('https://', '');
-    const registryEndingWithSlash = registry.replace(/\/?$/, '/');
+    const registryEndingWithSlash = ensureTrailingSlash(
+      registryUrl.replace(/^https?:\/\//, '')
+    );
     if (lookupName.startsWith(registryEndingWithSlash)) {
+      let registry = trimTrailingSlash(registryUrl);
+      if (!/^https?:\/\//.test(registry)) {
+        registry = `https://${registry}`;
+      }
       return {
         registry,
         repository: lookupName.replace(registryEndingWithSlash, ''),
-- 
GitLab