Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-03-2008, 10:23 AM
|
#1
|
|
Member
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517
Rep:
|
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
|
|
|
|
01-03-2008, 10:57 AM
|
#2
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
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?
|
|
|
|
01-06-2008, 05:54 AM
|
#3
|
|
Member
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517
Original Poster
Rep:
|
Hey,
What do you mean eval?
do you mean run or execute the argument or input the user has given??
Cheers
|
|
|
|
01-06-2008, 05:59 AM
|
#4
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
Hi. I mean literally the eval built-in which let you evaluate a string as a command and execute it.
|
|
|
|
01-06-2008, 06:07 AM
|
#5
|
|
Member
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517
Original Poster
Rep:
|
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
|
|
|
|
01-06-2008, 07:25 AM
|
#6
|
|
Moderator
Registered: May 2001
Posts: 24,824
|
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 :-]
|
|
|
|
01-06-2008, 08:18 AM
|
#7
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
Quote:
Originally Posted by unSpawn
IMHO you should ensure user input remains inert, not executed.
|
I subscribe to this. It is what I meant in my first post (thanks Unspawn for summarizing).
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 \;
|
|
|
|
01-06-2008, 08:55 AM
|
#8
|
|
Member
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517
Original Poster
Rep:
|
Thanks guy's this should be perfect
Cheers
Some great stuff i can work with there
|
|
|
|
07-07-2008, 06:40 PM
|
#9
|
|
LQ Newbie
Registered: Nov 2007
Distribution: Ubuntu, Debian, Slackware
Posts: 6
Rep:
|
Quote:
Originally Posted by helptonewbie
Thanks guy's this should be perfect
Cheers
Some great stuff i can work with there
|
I was working on something similar, and used "tr" to strip all non-alphanumeric characters from the input. The following example also allows for spaces and tries to avoid shell expansion in handling of the variables.
Code:
#!/bin/bash
echo "Enter variable: "
read VAR_INPUT
# Sanitize input and assign to new variable
export VAR_CLEAN="`echo "${VAR_INPUT}" | tr -cd '[:alnum:] [:space:]'`"
echo "New Variable: ${VAR_CLEAN}"
You can also limit the number of characters to ten with "cut -c -10", etc, etc. The sky's the limit.
Hope this is useful for others. All improvements to the code welcome!
Gilbert Mendoza
Last edited by gmendoza; 07-09-2008 at 01:50 PM.
Reason: minor syntax issue
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:26 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|