Forming sanitary shell commands or system calls in Ruby

后端 未结 5 1780
长发绾君心
长发绾君心 2020-12-08 11:20

I\'m building a daemon that will help me manage my server(s). Webmin works fine, as does just opening a shell to the server, but I\'d prefer to be able to control server ope

5条回答
  •  猫巷女王i
    2020-12-08 12:00

    It doesn't look like you need a shell for what you're doing. See the documentation for system here: http://ruby-doc.org/core/classes/Kernel.html#M001441

    You should use the second form of system. Your example above would become:

    system 'usermod', '-p', @options['shadow'], @options['username']
    

    A nicer (IMO) way to write this is:

    system *%W(usermod -p #{@options['shadow']} #{@options['username']})
    

    The arguments this way are passed directly into the execve call, so you don't have to worry about sneaky shell tricks.

提交回复
热议问题