ServiceStack / Issues

Issue Tracker for the commercial versions of ServiceStack
11 stars 8 forks source link

Service Stack crashes on parsing command line arguments when duplicate key is present #791

Closed exones closed 1 year ago

exones commented 1 year ago

Reproduce: Have a .NET 6.0 executable project with ServiceStack running inside ASP.NET (with using .UseKestrel()) and one service of any kind, smth similar to:

var host = new WebHostBuilder()
    .UseConfiguration(config)
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseKestrel(options =>
    {
        options.Listen(IPAddress.Parse(advertiseAddress),
            listenPort,
            listenOptions =>
            {
                listenOptions.UseConnectionLogging("HTTP");
                listenOptions.NoDelay = true;
            });
    })
    .UseStartup<Startup>()
    .Build();

And in Startup:

public class IriskaAspStartup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        var publishDirectory = GetPublishDirectory(env);
        RegisterServiceStackLicense();
        var appHost = CreateServiceStackAppHost(ioc);

        app.UseServiceStack(appHost);
        app.UseStaticFiles();
        ...
    }

    ...
}

Run the executable with duplicated keys in the call, for example:

dotnet app.dll --configDir=/firstConfigDir --configDir=/secondConfigDir

Expected ServiceStack doesn't try to intercept arguments, so nothing crashes when launching the application or accessing a service.

Observed Hard crash with call to Environment.Exit() from AppTasks class because ServiceStack assumes two things: the format of command line arges starting with -- or / and separated with =, and that there are no duplicate keys (call to .ToDictionary).

Offending line: https://github.com/ServiceStack/ServiceStack/blob/7e51d4a8080cd4818be00f9cfb5a44b83c44e6a3/ServiceStack/src/ServiceStack.Common/AppTasks.cs#L64

And much more suprisingly the fact that https://github.com/ServiceStack/ServiceStack/blob/7e51d4a8080cd4818be00f9cfb5a44b83c44e6a3/ServiceStack/src/ServiceStack.Common/AppTasks.cs#L124 kills the app although ServiceStack is an applicative library.

Proposition Allow duplicate arguments OR event better don't assume anything about the application's command line arguments format OR at least swallow exception and assume no --AppTasks arg is passed (https://github.com/ServiceStack/ServiceStack/blob/7e51d4a8080cd4818be00f9cfb5a44b83c44e6a3/ServiceStack/src/ServiceStack.Common/AppTasks.cs#L65). In my opinion, the way the application manages command line must never be responsibility of an applicative lib.

Many application allow duplicate cmd args keys (example: docker -v /vol1:/vol1 -v /vol2:vol2).

mythz commented 1 year ago

Thx for reporting, it should definitely be ignoring any non matching commands which should now be resolved from this commit: https://github.com/ServiceStack/ServiceStack/commit/1e3472662f4c473085b59aa27dfe6c31f7cc26a0

This change is available from v6.8.1+ that's now available on MyGet.

Note: purpose of --AppTasks is to run AppTask commands which are designed to run the specified command in the context of an App then immediately exit before the Server starts.