Skip to content

Commit

Permalink
Make permission check more lenient.
Browse files Browse the repository at this point in the history
Instead of enforcing an arbitrary permission policy, check permissions against  current umask.

If permissions are changed from the default, this might be a hint that something shady is going on.

Resolves MichaelAquilina#152.
  • Loading branch information
derula authored Oct 7, 2021
1 parent ecc53e3 commit 1590ac6
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions autoswitch_virtualenv.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,21 @@ function check_venv()
file_owner="$(/usr/bin/stat -f %u "$venv_path")"
file_permissions="$(/usr/bin/stat -f %OLp "$venv_path")"
fi
if [[ -d "$venv_path" ]]; then
default_permissions=777
else
default_permissions=666
fi
default_permissions="$(printf %o "$((~8#$(umask) & 8#$default_permissions))")"

if [[ "$file_owner" != "$(id -u)" ]]; then
printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
printf "Reason: Found a $AUTOSWITCH_FILE file but it is not owned by the current user\n"
printf "Change ownership of ${PURPLE}$venv_path${NORMAL} to ${PURPLE}'$USER'${NORMAL} to fix this\n"
elif ! [[ "$file_permissions" =~ ^[64][04][04]$ ]]; then
elif [[ "$file_permissions" != "$default_permissions" ]]; then
printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
printf "Reason: Found a $AUTOSWITCH_FILE file with weak permission settings ($file_permissions).\n"
printf "Run the following command to fix this: ${PURPLE}\"chmod 600 $venv_path\"${NORMAL}\n"
printf "Reason: Found a $AUTOSWITCH_FILE file with non-default permission settings ($file_permissions).\n"
printf "Run the following command to fix this: ${PURPLE}\"chmod $default_permissions $venv_path\"${NORMAL}\n"
else
if [[ "$venv_path" == *"/Pipfile" ]]; then
if type "pipenv" > /dev/null && _activate_pipenv; then
Expand Down

0 comments on commit 1590ac6

Please sign in to comment.