From 62b1ca3a8661a8f2a1f968629686bc71295fa47c Mon Sep 17 00:00:00 2001 From: Rihad Roshan <152946208+rihadroshan@users.noreply.github.com> Date: Sun, 30 Aug 2026 16:31:50 +0530 Subject: [PATCH 1/2] Harden tui launcher install and removal Port webapp-install's name and desktop-value guards to tui-install: a name can no longer nest the launcher (slash), climb out of the applications directory (../), or inject extra desktop-entry keys via a newline in Name/Comment/Exec/Icon. tui-remove now deletes the indexed launcher like webapp-remove, so entries left nested by older installs stay removable. Test: shell (tui-install-hardening-test.sh) --- bin/omarchy-tui-install | 48 ++++++++- bin/omarchy-tui-remove | 32 ++++-- test/shell.d/tui-install-hardening-test.sh | 118 +++++++++++++++++++++ 3 files changed, 187 insertions(+), 11 deletions(-) create mode 100755 test/shell.d/tui-install-hardening-test.sh diff --git a/bin/omarchy-tui-install b/bin/omarchy-tui-install index 2ddb0778301c..ec94f29d4654 100755 --- a/bin/omarchy-tui-install +++ b/bin/omarchy-tui-install @@ -13,6 +13,18 @@ safe_icon_name() { | sed 's/[^[:alnum:]]\+/-/g; s/^-//; s/-$//' } +require_plain_name() { + # The name becomes a filename. A slash would turn it into directory levels, so + # the launcher lands somewhere omarchy-tui-remove cannot address and the app + # is stuck in the launcher; a leading ../ leaves the applications directory + # altogether. Refuse rather than silently renaming what the user typed -- most + # often it is a URL entered in the name field. + if [[ $1 == */* ]]; then + echo "App name cannot contain '/': $1" + exit 1 + fi +} + icon_name_from_ref() { local ref="$1" local name @@ -37,14 +49,37 @@ install_user_icon() { printf '%s\n' "$name" } +desktop_string_escape() { + # Desktop Entry "string" value (freedesktop Desktop Entry Spec, "Value types"): + # a raw newline would start a new key line and let a value inject a second + # Exec=. Escape backslash first, then tab/CR/LF and a leading space. Every value + # written into the .desktop file passes through here. + # + # Parameter expansion rather than sed: GNU sed's N auto-prints the pattern space + # and exits at end of input, so a `:a;N;$!ba` slurp skips every following s/// + # for a value with no newline in it - which is every value except the injection + # attempt this exists to stop. + local value="$1" + + value=${value//\\/\\\\} + value=${value//$'\t'/\\t} + value=${value//$'\r'/\\r} + value=${value//$'\n'/\\n} + [[ $value == " "* ]] && value="\\s${value# }" + + printf '%s' "$value" +} + if (( $# != 4 )); then echo -e "\e[32mLet's create a TUI shortcut you can start with the app launcher.\n\e[0m" APP_NAME=$(gum input --prompt "Name> " --placeholder "My TUI") + require_plain_name "$APP_NAME" APP_EXEC=$(gum input --prompt "Launch Command> " --placeholder "lazydocker or bash -c 'dust; read -n 1 -s'") WINDOW_STYLE=$(gum choose --header "Window style" float tile) ICON_REF=$(gum input --prompt "Icon URL/name> " --placeholder "See https://dashboardicons.com or enter an installed icon name") else APP_NAME="$1" + require_plain_name "$APP_NAME" APP_EXEC="$2" WINDOW_STYLE="$3" ICON_REF="$4" @@ -79,15 +114,20 @@ else APP_CLASS="TUI.tile" fi +name_field=$(desktop_string_escape "$APP_NAME") +comment_field=$(desktop_string_escape "$APP_NAME") +exec_field=$(desktop_string_escape "xdg-terminal-exec --app-id=$APP_CLASS -e $APP_EXEC") +icon_field=$(desktop_string_escape "$ICON_VALUE") + cat >"$DESKTOP_FILE" </dev/null) + +# The launcher matching a chosen name, or empty when nothing was indexed under +# it (a TUI removed between the scan and the pick, say). +path_for_tui() { + local wanted="$1" i + for i in "${!TUIS[@]}"; do + if [[ ${TUIS[$i]} == "$wanted" ]]; then + printf '%s\n' "${TUI_PATHS[$i]}" + return 0 fi - done < <(find "$DESKTOP_DIR" -name '*.desktop' -print0) + done +} +if (( $# == 0 )); then if ((${#TUIS[@]})); then mapfile -t SORTED_TUIS < <(printf '%s\n' "${TUIS[@]}" | sort) APP_NAME=$(omarchy-menu-select "Select TUI to remove" "${SORTED_TUIS[@]}" -- --width 520 --maxheight 520) @@ -34,7 +51,8 @@ if [[ -z $APP_NAME ]]; then fi icon_name=$(printf '%s\n' "$APP_NAME" | tr '[:upper:]' '[:lower:]' | sed 's/[^[:alnum:]]\+/-/g; s/^-//; s/-$//') -rm -f "$DESKTOP_DIR/$APP_NAME.desktop" +desktop_file=$(path_for_tui "$APP_NAME") +rm -f "${desktop_file:-$DESKTOP_DIR/$APP_NAME.desktop}" rm -f "$ICON_DIR/$icon_name.png" "$ICON_DIR/$APP_NAME.png" "$OLD_ICON_DIR/$APP_NAME.png" if [[ ${OMARCHY_REMOVE_NOTIFY:-true} != "false" ]]; then omarchy-notification-send -g  "TUI removed" "$APP_NAME" diff --git a/test/shell.d/tui-install-hardening-test.sh b/test/shell.d/tui-install-hardening-test.sh new file mode 100755 index 000000000000..570263c8e693 --- /dev/null +++ b/test/shell.d/tui-install-hardening-test.sh @@ -0,0 +1,118 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +tmp_dir=$(mktemp -d) +trap 'rm -rf "$tmp_dir"' EXIT +mkdir -p "$tmp_dir/bin" "$tmp_dir/home" + +for stub in gtk-update-icon-cache update-desktop-database omarchy-notification-send; do + printf '#!/bin/bash\n:\n' >"$tmp_dir/bin/$stub" + chmod +x "$tmp_dir/bin/$stub" +done + +run_install() { + HOME="$tmp_dir/home" PATH="$tmp_dir/bin:$PATH" \ + "$ROOT/bin/omarchy-tui-install" "$@" +} + +run_remove() { + HOME="$tmp_dir/home" PATH="$tmp_dir/bin:$PATH" OMARCHY_REMOVE_NOTIFY=false \ + "$ROOT/bin/omarchy-tui-remove" "$@" +} + +apps_dir="$tmp_dir/home/.local/share/applications" + +# A URL typed into the name field is the reported way in. Every slash used to +# become a directory level, leaving a launcher nothing could address. Assert on +# the message: creating the launcher directly in the applications directory +# already makes the path failure indistinguishable from no validation at all. +output=$(run_install "http://example.test/oops" "lazydocker" tile hey 2>&1) && + fail "tui install rejects a name containing a slash" +[[ $output == *"App name cannot contain '/'"* ]] || + fail "tui install says why it refused a slashed name" "$output" +[[ -e "$apps_dir/http:" ]] && + fail "tui install does not create a directory from a slashed name" +pass "tui install rejects a name that would nest the launcher" + +# The name was a path fragment until something said otherwise, so ../ climbed +# out of the applications directory entirely and wrote wherever it landed. +if run_install "../../../../escaped" "lazydocker" tile hey >/dev/null 2>&1; then + fail "tui install rejects a name that climbs out of the applications directory" +fi +[[ -e "$tmp_dir/escaped.desktop" ]] && + fail "tui install writes no launcher outside the applications directory" +pass "tui install refuses a name that would escape the applications directory" + +# The interactive prompt reads the name first, so refusal has to happen right +# then rather than after the window-style pick and icon questions. +mkdir -p "$tmp_dir/ibin" +cp "$tmp_dir/bin"/* "$tmp_dir/ibin/" +cat >"$tmp_dir/ibin/gum" <<'STUB' +#!/bin/bash +count=$(cat "$GUM_STUB_COUNT" 2>/dev/null || echo 0) +count=$((count + 1)) +echo "$count" >"$GUM_STUB_COUNT" +sed -n "${count}p" "$GUM_ANSWERS" +STUB +chmod +x "$tmp_dir/ibin/gum" + +printf 'http://example.test/oops\n' >"$tmp_dir/answers" +: >"$tmp_dir/gum-count" +if HOME="$tmp_dir/home" PATH="$tmp_dir/ibin:$PATH" GUM_STUB_COUNT="$tmp_dir/gum-count" \ + GUM_ANSWERS="$tmp_dir/answers" "$ROOT/bin/omarchy-tui-install" >/dev/null 2>&1; then + fail "interactive tui install rejects a name containing a slash" +fi +[[ $(cat "$tmp_dir/gum-count") == 1 ]] || + fail "interactive tui install refuses the name before asking the next question" +pass "interactive tui install refuses a slashed name before any launcher work" + +# The property the escaping exists for: a newline in a value must not be able to +# start a second key line, in the Name field or anywhere else -- the Exec line +# included. +inject_name=$(printf 'Inject\nExec=evil') +run_install "$inject_name" "lazydocker" tile hey >/dev/null +inject_file="$apps_dir/$inject_name.desktop" +[[ -f $inject_file ]] || + fail "tui install writes a desktop entry for a name containing a newline" +(( $(grep -c '^Exec=' "$inject_file") == 1 )) || + fail "a newline in the name cannot inject a second Exec" "$(cat "$inject_file")" +grep -Fqx 'Name=Inject\nExec=evil' "$inject_file" || + fail "a newline in the name is escaped in the Name field" "$(cat "$inject_file")" +pass "tui install escapes newlines instead of writing a second Exec" + +# A normal name still installs and removes. +run_install "Example App" "lazydocker" tile hey >/dev/null +[[ -f "$apps_dir/Example App.desktop" ]] || + fail "tui install writes the launcher for an ordinary name" +grep -Fqx 'Exec=xdg-terminal-exec --app-id=TUI.tile -e lazydocker' "$apps_dir/Example App.desktop" || + fail "tui install writes the launch command" "$(cat "$apps_dir/Example App.desktop")" +run_remove "Example App" >/dev/null +[[ -f "$apps_dir/Example App.desktop" ]] && + fail "tui remove deletes the launcher it installed" +pass "tui install and remove round-trip an ordinary name" + +# Anything installed by an older version can still be nested. Removal has to +# reach it, which a path rebuilt from the displayed name never could. +mkdir -p "$apps_dir/legacy/sub" +cat >"$apps_dir/legacy/sub/Nested.desktop" <<'DESKTOP' +[Desktop Entry] +Name=Nested +Exec=xdg-terminal-exec --app-id=TUI.tile -e lazydocker +Type=Application +DESKTOP + +run_remove "Nested" >/dev/null +[[ -f "$apps_dir/legacy/sub/Nested.desktop" ]] && + fail "tui remove deletes a launcher left nested by an older install" +pass "tui remove reaches a nested legacy launcher" + +# Removing by name on a machine with no applications directory yet must stay +# quiet: this removal is called by the launcher without hiding stderr. +noise=$(HOME="$tmp_dir/empty" PATH="$tmp_dir/bin:$PATH" OMARCHY_REMOVE_NOTIFY=false \ + "$ROOT/bin/omarchy-tui-remove" "Example App" 2>&1 >/dev/null) +[[ -n $noise ]] && + fail "tui remove stays quiet with no applications directory" "$noise" +pass "tui remove stays quiet when there is no applications directory" \ No newline at end of file From d0bcaf32a0c89113fec8ee9575a902b5204a1b0c Mon Sep 17 00:00:00 2001 From: Omarchybot <317366263+omarchybot@users.noreply.github.com> Date: Thu, 1 Oct 2026 18:23:15 +0200 Subject: [PATCH 2/2] Prefer the top-level TUI launcher when a nested one shares its name The removal index returned whichever launcher find met first, so with applications/Docker.desktop and a legacy applications/x/Docker.desktop, removing Docker could delete the nested entry and leave the one the launcher's remove action named. The top-level path now wins when it is indexed. Co-Authored-By: Claude Opus 5.5 --- bin/omarchy-tui-remove | 16 +++++++++++---- test/shell.d/tui-install-hardening-test.sh | 23 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/bin/omarchy-tui-remove b/bin/omarchy-tui-remove index ff36fad25264..f92d8d4d4a0d 100755 --- a/bin/omarchy-tui-remove +++ b/bin/omarchy-tui-remove @@ -22,15 +22,23 @@ while IFS= read -r -d '' file; do done < <(find "$DESKTOP_DIR" -name '*.desktop' -print0 2>/dev/null) # The launcher matching a chosen name, or empty when nothing was indexed under -# it (a TUI removed between the scan and the pick, say). +# it (a TUI removed between the scan and the pick, say). A top-level launcher +# wins over a nested legacy one of the same name, whatever order find walks in. path_for_tui() { - local wanted="$1" i + local wanted="$1" found="" i for i in "${!TUIS[@]}"; do if [[ ${TUIS[$i]} == "$wanted" ]]; then - printf '%s\n' "${TUI_PATHS[$i]}" - return 0 + if [[ ${TUI_PATHS[$i]} == "$DESKTOP_DIR$wanted.desktop" ]]; then + printf '%s\n' "${TUI_PATHS[$i]}" + return 0 + fi + found=${found:-${TUI_PATHS[$i]}} fi done + + if [[ -n $found ]]; then + printf '%s\n' "$found" + fi } if (( $# == 0 )); then diff --git a/test/shell.d/tui-install-hardening-test.sh b/test/shell.d/tui-install-hardening-test.sh index 570263c8e693..afefe67ec00d 100755 --- a/test/shell.d/tui-install-hardening-test.sh +++ b/test/shell.d/tui-install-hardening-test.sh @@ -109,6 +109,29 @@ run_remove "Nested" >/dev/null fail "tui remove deletes a launcher left nested by an older install" pass "tui remove reaches a nested legacy launcher" +# The launcher's own remove action names the top-level entry, so a nested legacy +# launcher sharing its name must not be the one deleted, whichever find meets first. +for order in "nested-first" "top-first"; do + if [[ $order == "nested-first" ]]; then + mkdir -p "$apps_dir/legacy" + launchers=("$apps_dir/legacy/Docker.desktop" "$apps_dir/Docker.desktop") + else + launchers=("$apps_dir/Docker.desktop" "$apps_dir/legacy/Docker.desktop") + fi + for launcher in "${launchers[@]}"; do + mkdir -p "$(dirname "$launcher")" + printf '[Desktop Entry]\nName=Docker\nExec=xdg-terminal-exec --app-id=TUI.tile -e lazydocker\nType=Application\n' >"$launcher" + done + + run_remove "Docker" >/dev/null + [[ -f "$apps_dir/Docker.desktop" ]] && + fail "tui remove deletes the top-level launcher it was named for ($order)" + [[ -f "$apps_dir/legacy/Docker.desktop" ]] || + fail "tui remove leaves a nested launcher of the same name alone ($order)" + rm -rf "$apps_dir/legacy" +done +pass "tui remove prefers the top-level launcher over a nested one of the same name" + # Removing by name on a machine with no applications directory yet must stay # quiet: this removal is called by the launcher without hiding stderr. noise=$(HOME="$tmp_dir/empty" PATH="$tmp_dir/bin:$PATH" OMARCHY_REMOVE_NOTIFY=false \