Navigation Menu

Skip to content

Commit

Permalink
fix: Ban ls command (#1141)
Browse files Browse the repository at this point in the history
* Ban `ls` command
* Update banned commands test so it only checks bash and sh files
* Only allow `ls` for resolving symlinks
* Replace `ls` commands with `find`
  • Loading branch information
Stratus3D committed Dec 29, 2021
1 parent e61e3d9 commit 87137e4
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/commands/command-install.bash
Expand Up @@ -78,7 +78,7 @@ install_local_tool_versions() {

# Locate all the plugins installed in the system
local plugins_installed
if ls "$plugins_path" &>/dev/null; then
if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then
for plugin_path in "$plugins_path"/*; do
local plugin_name
plugin_name=$(basename "$plugin_path")
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-latest.bash
Expand Up @@ -43,7 +43,7 @@ latest_all() {
local plugins_path
plugins_path=$(get_plugin_path)

if ls "$plugins_path" &>/dev/null; then
if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then
for plugin_path in "$plugins_path"/*; do
plugin_name=$(basename "$plugin_path")

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-list.bash
Expand Up @@ -8,7 +8,7 @@ list_command() {
local plugins_path
plugins_path=$(get_plugin_path)

if ls "$plugins_path" &>/dev/null; then
if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then
for plugin_path in "$plugins_path"/*; do
plugin_name=$(basename "$plugin_path")
printf "%s\\n" "$plugin_name"
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-plugin-list-all.bash
Expand Up @@ -9,7 +9,7 @@ plugin_list_all_command() {
local plugins_local_path
plugins_local_path="$(get_plugin_path)"

if ls "$plugins_index_path" &>/dev/null; then
if find "$plugins_index_path" -mindepth 1 -type d &>/dev/null; then
(
for index_plugin in "$plugins_index_path"/*; do
index_plugin_name=$(basename "$index_plugin")
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-plugin-list.bash
Expand Up @@ -23,7 +23,7 @@ plugin_list_command() {
esac
done

if ls "$plugins_path" &>/dev/null; then
if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then
(
for plugin_path in "$plugins_path"/*; do
plugin_name=$(basename "$plugin_path")
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-reshim.bash
Expand Up @@ -11,7 +11,7 @@ reshim_command() {
local plugins_path
plugins_path=$(get_plugin_path)

if ls "$plugins_path" &>/dev/null; then
if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then
for plugin_path in "$plugins_path"/*; do
plugin_name=$(basename "$plugin_path")
reshim_command "$plugin_name"
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.bash
Expand Up @@ -454,7 +454,7 @@ resolve_symlink() {
# This seems to be the only cross-platform way to resolve symlink paths to
# the real file path.
# shellcheck disable=SC2012
resolved_path=$(ls -l "$symlink" | sed -e 's|.*-> \(.*\)|\1|')
resolved_path=$(ls -l "$symlink" | sed -e 's|.*-> \(.*\)|\1|') # asdf_allow: ls '

# Check if resolved path is relative or not by looking at the first character.
# If it is a slash we can assume it's root and absolute. Otherwise we treat it
Expand Down
12 changes: 10 additions & 2 deletions test/banned_commands.bats
Expand Up @@ -33,6 +33,14 @@ banned_commands_regex=(
# sort --sort-version isn't supported everywhere
"sort.*-V"
"sort.*--sort-versions"

# ls often gets used when we want to glob for files that match a pattern
# or when we want to find all files/directories that match a pattern or are
# found in a certain location. Using shell globs is preferred over ls, and
# find is better at locating files that are in a certain location or that
# match certain filename patterns.
# https://github-wiki-see.page/m/koalaman/shellcheck/wiki/SC2012
'\bls '
)

setup() {
Expand All @@ -50,7 +58,7 @@ teardown() {
# followed by an underscore (indicating it's a variable and not a
# command).
for cmd in "${banned_commands[@]}"; do
run bash -c "grep -nHR '$cmd' asdf.* lib bin\
run bash -c "grep -nHR --include \*.bash --include \*.sh '$cmd' asdf.* lib bin\
| grep -v '#.*$cmd'\
| grep -v '\".*$cmd.*\"' \
| grep -v '${cmd}_'\
Expand All @@ -67,7 +75,7 @@ teardown() {
done

for cmd in "${banned_commands_regex[@]}"; do
run bash -c "grep -nHRE '$cmd' asdf.* lib bin\
run bash -c "grep -nHRE --include \*.bash --include \*.sh '$cmd' asdf.* lib bin\
| grep -v '#.*$cmd'\
| grep -v '\".*$cmd.*\"' \
| grep -v '${cmd}_'\
Expand Down

0 comments on commit 87137e4

Please sign in to comment.