Nuget 私人来源 - 可以在 PackageReference 中设置条件吗?
Nuget private source - possible to set condition in PackageReference?
我正在通过 github 设置私有 nuget 源。一切正常,使用这样的 nuget.config
(已编辑凭证部分):
<configuration>
<packageSources>
<add key="github" value="https://nuget.pkg.github.com/<my-company>/index.json" />
</packageSources>
<packageSourceCredentials>...</packageSourceCredentials>
</configuration>
注意,我没有清除其他 packageSources:我仍然需要使用 public nuget 源。我只是另外添加一个私人来源。
正如我所说,一切正常,我可以从任一来源添加包。但是当我检查 csproj
中的 PackageReference
条目时,我开始担心了。它不指示包使用哪个来源。
例如,在这种情况下,MyCompany.Common
来自我的私人来源,Newtonsoft.Json
来自 public nuget 来源。
<ItemGroup>
<PackageReference Include="MyCompany.Common" Version="1.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
但是如果有同名的 public 包怎么办?我试过了,结果很可怕。
所以换句话说,如果有人猜到了我的私有包名称,下次有人克隆 repo 并构建时,很可能 Visual Studio 会从错误的来源获取包。
有什么办法可以避免这种情况吗?我一直在阅读 Nuget PackageReference docs,唯一看起来甚至很接近的可能是 Condition
:
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" Condition="'$(TargetFramework)' == 'net452'" />
但看起来更多的是根据solution/project/system配置来设置条件,而不是包源。
有条件可以指定包源吗?或者是否有其他东西可以为特定包指定 nuget 源?
我认为这可能与 this article 有关。
这里有一个有趣的post
https://azure.microsoft.com/en-us/resources/3-ways-to-mitigate-risk-using-private-package-feeds/
我发现最实用的方法是使用锁定模式,这样你就会有一个 package.lock.json 这样的文件
"dependencies": {
".NETCoreApp,Version=v5.0": {
"Newtonsoft.Json": {
"type": "Direct",
"requested": "[12.0.1, )",
"resolved": "12.0.1",
"contentHash": "pBR3wCgYWZGiaZDYP+HHYnalVnPJlpP1q55qvVb+adrDHmFMDc1NAKio61xTwftK3Pw5h7TZJPJEEVMd6ty8rg=="
}
}
}
contentHash
应该保护包裹。
锁定模式可以通过
启用
<PropertyGroup>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
不,NuGet 中没有内置功能来执行这种“源过滤”,因为那超出了它的范围。
然而,有许多第三方(付费)NuGet 服务器产品公开了此类功能。或者,您可以编写自己的 NuGet 服务器来为您执行此操作。
看了@lastr2d2提供的link他的回答,我发现一段话也很贴切:
For NuGet Gallery: An ID prefix
can be registered by publishers to restrict uploads to the public gallery. Packages under a registered prefix can only be uploaded by approved accounts, which also protects against public substitution attacks. This reservation can be done whether you intend to publish your packages to NuGet.org or not. Using a registered ID prefix for private packages helps ensure that an attacker cannot claim any of your names."
(强调)
我考虑过是否在他的回答下添加这个作为评论,但最终决定这本身就是一个成熟的解决方案,因此应该是一个单独的答案。
我正在通过 github 设置私有 nuget 源。一切正常,使用这样的 nuget.config
(已编辑凭证部分):
<configuration>
<packageSources>
<add key="github" value="https://nuget.pkg.github.com/<my-company>/index.json" />
</packageSources>
<packageSourceCredentials>...</packageSourceCredentials>
</configuration>
注意,我没有清除其他 packageSources:我仍然需要使用 public nuget 源。我只是另外添加一个私人来源。
正如我所说,一切正常,我可以从任一来源添加包。但是当我检查 csproj
中的 PackageReference
条目时,我开始担心了。它不指示包使用哪个来源。
例如,在这种情况下,MyCompany.Common
来自我的私人来源,Newtonsoft.Json
来自 public nuget 来源。
<ItemGroup>
<PackageReference Include="MyCompany.Common" Version="1.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
但是如果有同名的 public 包怎么办?我试过了,结果很可怕。 所以换句话说,如果有人猜到了我的私有包名称,下次有人克隆 repo 并构建时,很可能 Visual Studio 会从错误的来源获取包。
有什么办法可以避免这种情况吗?我一直在阅读 Nuget PackageReference docs,唯一看起来甚至很接近的可能是 Condition
:
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" Condition="'$(TargetFramework)' == 'net452'" />
但看起来更多的是根据solution/project/system配置来设置条件,而不是包源。
有条件可以指定包源吗?或者是否有其他东西可以为特定包指定 nuget 源?
我认为这可能与 this article 有关。
这里有一个有趣的post
https://azure.microsoft.com/en-us/resources/3-ways-to-mitigate-risk-using-private-package-feeds/
我发现最实用的方法是使用锁定模式,这样你就会有一个 package.lock.json 这样的文件
"dependencies": {
".NETCoreApp,Version=v5.0": {
"Newtonsoft.Json": {
"type": "Direct",
"requested": "[12.0.1, )",
"resolved": "12.0.1",
"contentHash": "pBR3wCgYWZGiaZDYP+HHYnalVnPJlpP1q55qvVb+adrDHmFMDc1NAKio61xTwftK3Pw5h7TZJPJEEVMd6ty8rg=="
}
}
}
contentHash
应该保护包裹。
锁定模式可以通过
启用<PropertyGroup>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
不,NuGet 中没有内置功能来执行这种“源过滤”,因为那超出了它的范围。
然而,有许多第三方(付费)NuGet 服务器产品公开了此类功能。或者,您可以编写自己的 NuGet 服务器来为您执行此操作。
看了@lastr2d2提供的link他的回答,我发现一段话也很贴切:
For NuGet Gallery: An
ID prefix
can be registered by publishers to restrict uploads to the public gallery. Packages under a registered prefix can only be uploaded by approved accounts, which also protects against public substitution attacks. This reservation can be done whether you intend to publish your packages to NuGet.org or not. Using a registered ID prefix for private packages helps ensure that an attacker cannot claim any of your names."
(强调)
我考虑过是否在他的回答下添加这个作为评论,但最终决定这本身就是一个成熟的解决方案,因此应该是一个单独的答案。