Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to pass additional arguments to an command ? Like some meta data ? #1709

Open
genaray opened this issue Apr 15, 2022 · 2 comments

Comments

@genaray
Copy link

genaray commented Apr 15, 2022

Im using the command line API for my game server... i have several mods and admins sending commands from their client to the game server.

However, i need to check some commands for privileges to prevent abusing those commands.
Currently it looks a bit like this...

/GiveItem -caller AdminName -to PlayerName -... other stuff and the command acesses the caller option to check if the admin has the right to use this type of command... this is a bit ugly. The caller of the command shouldnt be passed as an option, this is also insecure because other admins without those rights could simply parse another admins name in that option to acess it.

Im looking for some type of security validation root.Invoke(command, adminNameOrAccountOrWhatever, other stuff... ) this way the option could do the security validation without having the name passed as an option or argument.

I hope you all understand what i mean, im terrible at describing. Is there something like this build in ?

@elgonzo
Copy link
Contributor

elgonzo commented Apr 15, 2022

I don't really know what and precisely how you want to program, but an idea that comes to my mind is using a ThreadLocal or a AsyncLocal (dependig on whether your control flow is synchronous or asynchronous) to pass necessary data other than commandline arguments into the commandline handler, kinda like this (pseudo code!):

private static ThreadLocal<Whatever> myThreadLocal = new();

void TheApiRequestHandler(...)
{
    var stuffThatMatters = get user token/id and whatever else is needed...;
    myThreadLocal.Value = stuffThatMatters;

   rootCommand.Invoke(normal commandline args);
}

void CommandHandler(normal commandline option and argument parameters)
{
    var hereIsTheStuffThatMatters = myThreadLocal.Value;
}

@KalleOlaviNiemitalo
Copy link

KalleOlaviNiemitalo commented Jul 1, 2022

It might also be possible to pass the additional information in properties of a custom object that implements IConsole, because you can pass an IConsole to CommandExtensions.Invoke and read it back from InvocationContext.Console. However:

  • It would look somewhat strange, although perhaps it makes sense in a game server / chatbot where the console corresponds to a user agent.
  • Would break if System.CommandLine creates a delegating IConsole instead of passing the original instance.
  • May be broken by Change IConsole to ConsoleStreams or equivalent #1786.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants