moorara
moorara

Reputation: 4226

How to sanitize an input command in shell script by blacklisting/whitelisting?

I'm working on a very expanded source code. I added a number of new source files to different paths, and wrote a shell script to open all of my files in a text editor.

I get an input from user to open files in his favourite code editor (gedit, geany, sublime, ...). For the sake of defensive programming, I would like to filter some commands like rm which can delete all my files instead of opening them!

My question is what is the best way to achieve this? If I want to blacklist or whitelist a set of commands, do I need to compare the input command against all of the commands in the blacklist/whitelist? Is there any shorter way?

Upvotes: 1

Views: 804

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295308

If you want a user to be able to edit files but not run other, arbitrary commands on them, do all of the following:

  • Store the files under a user account the user doesn't have direct access to.
  • Configure /etc/sudoers to allow that user to run only specifically whitelisted commands as an account able to read the file.
  • Support only editors which have a restricted mode. By default, for instance, vim can be used to run arbitrary commands, but it has a (restricted) rvim alternative which doesn't allow privilege escalation.

Or do the sane thing, and use version control. :)

Upvotes: 2

Related Questions