.NET Core 应用程序中的 Sonarqube 规则 S4823 "Using command line arguments is security-sensitive"
Sonarqube rule S4823 "Using command line arguments is security-sensitive" in .NET Core application
我使用 Sonarqube 检查项目中的代码质量。
我收到此处描述的错误 S4823 Using command line arguments is security-sensitive
:https://rules.sonarsource.com/csharp/RSPEC-4823?search=using%20command
错误出现在我没有更改的代码中 - 它是在项目创建期间自动生成的:
namespace WebApplication
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
我的应用程序 运行 已投入生产一段时间。它作为 OutOfProcess 在 IIS 后面托管。我想知道我是否可以安全地从 ConfigureWebHostDefaults 中删除 args?
我不记得在任何地方设置了额外的参数,但也许在这个过程中,某些东西(IIS、Kestrel、系统……)设置了一些参数?在那种情况下,我应该接受来自 Sonarqube 的警告还是采取其他措施?
如您在代码中所见,args
被 Host.CreateDefaultBuilder()
使用
此方法使用默认配置创建一个 HostBuilder。您可以查看 Microsoft Github repository.
上的代码
不为空时,作为配置数据的提供者。其他默认提供程序是环境变量以及 appsettings.{Environment}.json
和 appsettings.json。检查 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1
其中一些参数可用于 Kestrel(端口、超时等),因为您在 Web 应用程序中。来自 official documentation :
For out-of-process, CreateDefaultBuilder
calls UseIISIntegration
to:
- Configure the port and base path the server should listen on when
running behind the ASP.NET Core Module.
- Configure the host to capture
startup errors.
如果你查看 official repository 上的源代码,你可以看到参数(主机、端口等)是这样检索的:
var port = hostBuilder.GetSetting(ServerPort) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{ServerPort}");
文档指出 GetSetting
从配置中读取值,因此它可能来自 args
或环境变量作为后备。
由于我不确定IIS是否使用命令行参数或环境变量,您可以打印args和环境变量的值来检查哪些是IIS提供的。
我使用 Sonarqube 检查项目中的代码质量。
我收到此处描述的错误 S4823 Using command line arguments is security-sensitive
:https://rules.sonarsource.com/csharp/RSPEC-4823?search=using%20command
错误出现在我没有更改的代码中 - 它是在项目创建期间自动生成的:
namespace WebApplication
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
我的应用程序 运行 已投入生产一段时间。它作为 OutOfProcess 在 IIS 后面托管。我想知道我是否可以安全地从 ConfigureWebHostDefaults 中删除 args?
我不记得在任何地方设置了额外的参数,但也许在这个过程中,某些东西(IIS、Kestrel、系统……)设置了一些参数?在那种情况下,我应该接受来自 Sonarqube 的警告还是采取其他措施?
如您在代码中所见,args
被 Host.CreateDefaultBuilder()
此方法使用默认配置创建一个 HostBuilder。您可以查看 Microsoft Github repository.
上的代码不为空时,作为配置数据的提供者。其他默认提供程序是环境变量以及 appsettings.{Environment}.json
和 appsettings.json。检查 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1
其中一些参数可用于 Kestrel(端口、超时等),因为您在 Web 应用程序中。来自 official documentation :
For out-of-process,
CreateDefaultBuilder
callsUseIISIntegration
to:
- Configure the port and base path the server should listen on when running behind the ASP.NET Core Module.
- Configure the host to capture startup errors.
如果你查看 official repository 上的源代码,你可以看到参数(主机、端口等)是这样检索的:
var port = hostBuilder.GetSetting(ServerPort) ?? Environment.GetEnvironmentVariable($"ASPNETCORE_{ServerPort}");
文档指出 GetSetting
从配置中读取值,因此它可能来自 args
或环境变量作为后备。
由于我不确定IIS是否使用命令行参数或环境变量,您可以打印args和环境变量的值来检查哪些是IIS提供的。