From d44da54b36490146b1136b867f8f010be057f18c Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 2 Aug 2021 13:49:38 -0700 Subject: [PATCH] add some pretty lame security checks to stfu --- sh/stfu | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/sh/stfu b/sh/stfu index 03f1f58..bffa2dd 100644 --- a/sh/stfu +++ b/sh/stfu @@ -43,7 +43,30 @@ stfu() { ### @- ### ``` [ $# -gt 0 ] || { printf "%s\n" "$0: too few arguments" >&2; return 1; } - local temp="${TMP:-/tmp}/stfu" + local dirty=0 temp="$STFU_DIR" + if [ -z "$temp" ]; then + temp="$(mktemp -dt stfu.XXXXXXXXXX)" + [ $? -eq 0 ] || { printf "%s\n" "$0: failed create temporary directory" >&2; return 1; } + dirty=1 + fi + + # NOTE: this stat command will not work on BSD-likes, + # but it will work with GNU coreutils and busybox. + local perms="$(stat -c '%a' "$temp")" + local realtemp="$(readlink -f "$temp")" + [ -z "$MSYSTEM" ] || perms=700 # MSYS2 is insecure, oh well. + if [ -d "$temp" ] && [ "$realtemp" = "$temp" ] && [ "$perms" = 700 ]; then + if [ $dirty -ne 0 ]; then + export STFU_DIR="$temp" + fi + else + if [ -n "$STFU_DIR" ]; then + printf "%s\n" "$0: invalid temporary directory, please unset STFU_DIR" >&2; + else + printf "%s\n" "$0: something went horribly wrong, maybe you can tell?" "$perms" "$realtemp" + fi + return 1 + fi local time="$(date -u '+%s')" [ $? -eq 0 ] || { printf "%s\n" "$0: failed to get current time" >&2; return 1; } @@ -54,6 +77,8 @@ stfu() { ### @- local out="$temp/out_$time" local err="$temp/err_$time" + touch "$out" && touch "$err" || { printf "%s\n" "$0: failed to create temp files" >&2; return 1; } + #local out="$(mktemp -t -p "$temp" out_XXXXXX)" #[ $? -eq 0 ] || { printf "%s\n" "$0: failed to create temp file" >&2; return 1; } #local err="$(mktemp -t -p "$temp" err_XXXXXX)"