From 3a5ad66d5e255d7741a3b4b7dd74722ff6afa2de Mon Sep 17 00:00:00 2001
From: Oleg Krivtsov <olegkrivtsov@gmail.com>
Date: Wed, 8 Dec 2021 17:38:15 +0700
Subject: [PATCH] fix(manager/nuget): support package sources with whitespaces
 in keys (#12882)

---
 .../with-whitespaces/NuGet.config             |  8 ++++++++
 .../with-whitespaces/with-whitespaces.csproj  | 12 +++++++++++
 lib/manager/nuget/artifacts.ts                |  3 ++-
 lib/manager/nuget/extract.spec.ts             | 20 +++++++++++++++++++
 4 files changed, 42 insertions(+), 1 deletion(-)
 create mode 100644 lib/manager/nuget/__fixtures__/with-whitespaces/NuGet.config
 create mode 100644 lib/manager/nuget/__fixtures__/with-whitespaces/with-whitespaces.csproj

diff --git a/lib/manager/nuget/__fixtures__/with-whitespaces/NuGet.config b/lib/manager/nuget/__fixtures__/with-whitespaces/NuGet.config
new file mode 100644
index 0000000000..cc8abe128e
--- /dev/null
+++ b/lib/manager/nuget/__fixtures__/with-whitespaces/NuGet.config
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<configuration>
+  <packageSources>
+    <clear />
+    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> 
+    <add key="My Package Source" value="https://my.myget.org/F/my/auth/guid/api/v3/index.json" />
+  </packageSources>
+</configuration>
diff --git a/lib/manager/nuget/__fixtures__/with-whitespaces/with-whitespaces.csproj b/lib/manager/nuget/__fixtures__/with-whitespaces/with-whitespaces.csproj
new file mode 100644
index 0000000000..872e201179
--- /dev/null
+++ b/lib/manager/nuget/__fixtures__/with-whitespaces/with-whitespaces.csproj
@@ -0,0 +1,12 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+    <PropertyGroup>
+        <TargetFramework>netcoreapp3.1</TargetFramework>
+        <RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
+    </PropertyGroup>
+
+    <ItemGroup>
+      <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
+    </ItemGroup>
+
+</Project>
diff --git a/lib/manager/nuget/artifacts.ts b/lib/manager/nuget/artifacts.ts
index a3b6fdeaab..3ff382af53 100644
--- a/lib/manager/nuget/artifacts.ts
+++ b/lib/manager/nuget/artifacts.ts
@@ -1,4 +1,5 @@
 import { join } from 'path';
+import { quote } from 'shlex';
 import { GlobalConfig } from '../../config/global';
 import { TEMPORARY_ERROR } from '../../constants/error-messages';
 import { id, parseRegistryUrl } from '../../datasource/nuget';
@@ -45,7 +46,7 @@ async function addSourceCmds(
     let addSourceCmd = `dotnet nuget add source ${registryInfo.feedUrl} --configfile ${nugetConfigFile}`;
     if (registry.name) {
       // Add name for registry, if known.
-      addSourceCmd += ` --name ${registry.name}`;
+      addSourceCmd += ` --name ${quote(registry.name)}`;
     }
     if (username && password) {
       // Add registry credentials from host rules, if configured.
diff --git a/lib/manager/nuget/extract.spec.ts b/lib/manager/nuget/extract.spec.ts
index e12e01825c..d12fc9b4e6 100644
--- a/lib/manager/nuget/extract.spec.ts
+++ b/lib/manager/nuget/extract.spec.ts
@@ -132,6 +132,26 @@ describe('manager/nuget/extract', () => {
         ],
       });
     });
+
+    it('handles NuGet.config with whitespaces in package source keys', async () => {
+      const packageFile = 'with-whitespaces/with-whitespaces.csproj';
+      const contents = loadFixture(packageFile);
+      expect(await extractPackageFile(contents, packageFile, config)).toEqual({
+        deps: [
+          {
+            currentValue: '12.0.3',
+            datasource: 'nuget',
+            depName: 'Newtonsoft.Json',
+            depType: 'nuget',
+            registryUrls: [
+              'https://api.nuget.org/v3/index.json#protocolVersion=3',
+              'https://my.myget.org/F/my/auth/guid/api/v3/index.json',
+            ],
+          },
+        ],
+      });
+    });
+
     it('ignores local feed in NuGet.config', async () => {
       const packageFile =
         'with-local-feed-in-config-file/with-local-feed-in-config-file.csproj';
-- 
GitLab