New function: SafeInput

This commit is contained in:
Michael Boelen 2019-06-29 19:34:12 +02:00
parent 81c8f1f2a6
commit 08e8e59197
No known key found for this signature in database
GPG Key ID: 26141F77A09D7F04
1 changed files with 32 additions and 0 deletions

View File

@ -86,6 +86,7 @@
# ReportSuggestion Add a suggestion to report file
# ReportWarning Add a warning and priority to report file
# SafePerms Check if a file has safe permissions
# SafeInput Test provided string to see if it contains unwanted characters
# SearchItem Search a string in a file
# ShowComplianceFinding Display a particular finding regarding compliance or a security standard
# ShowSymlinkPath Show a path behind a symlink
@ -2510,6 +2511,37 @@
}
################################################################################
# Name : SafeInput()
# Description : Test provided string to see if it contains unwanted characters
#
# Input : string + optional class (parameter 2)
# Returns : 0 (input considered to be safe) or 1 (validation failed)
################################################################################
SafeInput() {
exitcode=1
# By default remove only control characters
if [ $# -eq 1 ]; then
input="$1"
cleaned=$(echo ${input} | tr -d '[:cntrl:]')
# If know what to test against, then see if input matches the specified class
elif [ $# -eq 2 ]; then
input="$1"
testchars="$2"
cleaned=$(echo $1 | tr -cd "${testchars}")
else
ExitFatal "No argument or too many arguments provided to SafeInput()"
fi
if [ "${cleaned}" = "${input}" ]; then
exitcode=0
fi
return ${exitcode}
}
################################################################################
# Name : SafePerms()
# Return : 0 (file OK) or break