-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented strict mode to prevent insecure access to external variables #64
Conversation
Re-ran the tests after the latest updates:
|
@@ -169,8 +193,17 @@ mo() ( | |||
done | |||
fi | |||
|
|||
# Allow turning off Strict Mode |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment seems to be the opposite of what you want. Perhaps change this to
# Allow turning off Strict Mode | |
# Restrict Strict Mode to only operate with sourced data |
[[ $MO_STRICT_MODE != true ]] && return 0 | ||
[[ " ${MO_SOURCED_VARS[*]} " =~ " ${1%.*} " ]] && return 0 | ||
|
||
echo "Illegal variable access $1" >&2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a minor change
echo "Illegal variable access $1" >&2 | |
echo "Illegal access to variable: $1" >&2 |
|
||
echo "${STDOUT[@]}" | ||
|
||
expectedErr="Illegal variable access BASH_VERSINFO" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expectedErr="Illegal variable access BASH_VERSINFO" | |
expectedErr="Illegal access to variable: BASH_VERSINFO" |
# shellcheck disable=SC1090 | ||
. "$f2source" | ||
AFTER_VARS=(`cat <(set -o posix ; set) | cut -d'=' -f1`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like this type of approach, but would prefer to have a more clean environment. Also, mo
was made to avoid calling other tools like sort
, uniq
, cut
, and tr
. What do you think of something like this? The meat of this (the huge eval
) could be pulled out into a function making it easier for devs to understand. Then simply call it from here to list the variables that the script exports.
while read -r line; do
if [[ ! " ${MO_SOURCED_VARS[@]} " =~ " $line " ]]; then
MO_SOURCED_VARS+=("$line")
fi
done < <(
# Need to pass the name of the file into this but can't use a variable
eval "
# Remove all variables that are not read only
while read -r line; do
line=\${line#declare * }
line=\${line%%=*}
unset \"\$line\" &> /dev/null
done < <(declare -p)
unset line
# Load variables
. \"$f2source\"
# Write out all readonly variables
while read -r line; do
line=\${line#declare * }
line=\${line%%=*}
unset \"\$line\" &> /dev/null
[[ ! -v \"\$line\" ]] && [[ \"\$line\" != \"BASH_ARGV\" ]] && echo \"\$line\"
done < <(declare -p)
"
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this! Honestly, I meant to revisit this afterwards to remove those external tools. I'm glad you pointed this out. I actually avoided many solutions with grep
and others, but I guess I never got around to cleaning the proof-of-concept up before pushing this up haha.
I'll play with this and see what I can do!
Implements #52
Introduces
--strict
to prevent access to variables that weren't defined in sourced filesThis only works with
--source
. It's a no-op if it's set without--source
.Tested using
fidian/multishell-small
:Minor addition