User input into Bash scripts and checking validity of user input??
Hello,
I write quite a few little bash scripts to do all sorts of funky stuff, the problem is they would be in an un-secure state ie. when reading or making use of user input into a script, i haven't checked for what the user has inputted into the script. Therefore obviously leaving open security whole that the script could end up doing other things i dont want it to do. Whats the best way in bash to be validating user input to reduce the chances of this happening. Read in a user input or when starting a script with an argument ./script argument this argument isn't validated so it could be :- yes my name is me;rm -Rf /* So its basically reducing what could happen, therefore i gues one thing to do would be to check for occurrences of ';' in any user input, whatelse and how should i do this?? Regards and Thanks |
I doubt that a user's input can be executed as a command. If you use the read statement all the input line is stored in a variable and most likely your script does not simply eval that variable. If input is passed as arguments, you can do all the checks you need, but again you will not simply eval the arguments, won't you?
|
Hey,
What do you mean eval? do you mean run or execute the argument or input the user has given?? Cheers |
Hi. I mean literally the eval built-in which let you evaluate a string as a command and execute it.
|
ooo, sorry i thought it was some term i've not heard of yet :-) yeah now that makes sense. But there must still be some method i could use to check some possible incorrect input that could cause a problem like the one i've sugested?
Cheers |
IMHO you should ensure user input remains inert, not executed. There's probably a Gogolplex of docs around about input sanitation I haven't tried to find but AFAIK input sanitation in the sense of "blacklisting" requires you to safeguard against every known iteration and every loophole which is inefficient and unsafe because you don't cover everything, you can be sure of *that*. Input sanitation in the "whitelisting" sense (only allowing a few options) is relatively safer because it is more restrictive (sudo-like). So you could 'declare -r goodChars="1234567890-_.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"' then 'scrubbedString="${userInput//[^$goodChars]/}"'. Doing something like 'declare -r badChars=";:,.[]{}<>" leads to frustration. Frustration leads to anger, and we all know what anger leads to :-]
|
Quote:
You should know a priori what a user's input should be and check it accordingly. If you expect a random sequence of characters, never execute it. Actually I cannot figure out a script that accepts commands as input: if the user can execute the script, most likely he can execute any command (unless he runs in a restricted shell). On the other hand if the user is meant to drive how the script works, you may use options (see getopts). Returning to your original question (how to check if a string contains a semi-colon) you can use the method suggested by Unspawn (I would add also a blank space in the list of good characters) and check if a string contains any unwanted character. Or simply do a literal Code:
echo $userinput | grep -o \; |
Thanks guy's this should be perfect
Cheers Some great stuff i can work with there |
Quote:
Code:
#!/bin/bash Hope this is useful for others. All improvements to the code welcome! Gilbert Mendoza |
All times are GMT -5. The time now is 09:11 AM. |