Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions bin/omarchy-tui-install
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Newline names cannot be removed

The new guard accepts names containing newlines, and the new test installs one. During interactive removal, omarchy-tui-remove prints each indexed name on a separate line before sorting, so that name becomes multiple menu choices. Selecting a fragment cannot remove the launcher and may instead remove a different launcher named after that fragment. Reject newlines in names or preserve names intact through selection.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At 3b7b242, the installer comes unchanged from quattro: #19965 already merged its name guard and desktop-value escaping. This PR now changes only the remover's top-level namesake preference and regression coverage. Newline names splitting in the picker is inherited from quattro, including its webapp picker, and is unchanged by this fix.

Checked by Codex High, Opus 5.5 High and muse-wonder via pi (thinking high); the reviewers agreed, and independence is not guaranteed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. At 3b7b242, omarchy-tui-install’s name guard and desktop escaping are already present in the base branch, and this PR does not change the picker’s line-based handling. The newline-name picker issue is pre-existing (and also affects the webapp picker), so I’m withdrawing the finding as out of scope for this change. The underlying behavior may still merit a separate fix.

echo "App name cannot contain '/': $1"
exit 1
fi
}

icon_name_from_ref() {
local ref="$1"
local name
Expand All @@ -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"
Expand Down Expand Up @@ -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" <<EOF
[Desktop Entry]
Version=1.0
Name=$APP_NAME
Comment=$APP_NAME
Exec=xdg-terminal-exec --app-id=$APP_CLASS -e $APP_EXEC
Name=$name_field
Comment=$comment_field
Exec=$exec_field
Terminal=false
Type=Application
Icon=$ICON_VALUE
Icon=$icon_field
StartupNotify=true
EOF

Expand Down
40 changes: 33 additions & 7 deletions bin/omarchy-tui-remove
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,39 @@ ICON_DIR="$HOME/.local/share/icons/hicolor/256x256/apps"
OLD_ICON_DIR="$HOME/.local/share/applications/icons"
DESKTOP_DIR="$HOME/.local/share/applications/"

if (( $# == 0 )); then
# Find all TUIs
while IFS= read -r -d '' file; do
if grep -qE '^Exec=.*(\$TERMINAL|xdg-terminal-exec).*-e' "$file"; then
TUIS+=("$(basename "${file%.desktop}")")
# Always index the launchers, so removal deletes the file that was found rather
# than a path rebuilt from the displayed name. Installs predating the name
# validation could nest the launcher inside directories, and those are exactly
# the ones a reconstructed path cannot reach.
TUI_PATHS=()
while IFS= read -r -d '' file; do
if grep -qE '^Exec=.*(\$TERMINAL|xdg-terminal-exec).*-e' "$file"; then
TUIS+=("$(basename "${file%.desktop}")")
TUI_PATHS+=("$file")
fi
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). 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" found="" i
for i in "${!TUIS[@]}"; do
if [[ ${TUIS[$i]} == "$wanted" ]]; then
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 < <(find "$DESKTOP_DIR" -name '*.desktop' -print0)
done

if [[ -n $found ]]; then
printf '%s\n' "$found"
fi
}

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)
Expand All @@ -34,7 +59,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"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Nested launcher icons are mishandled

When this lookup removes a legacy launcher such as foo/Bar.desktop, icon cleanup uses only Bar. The installer named downloaded or copied icons from the full app name foo/Bar, producing foo-bar.png. Removal therefore leaves that icon behind and can delete an unrelated bar.png icon. Derive icon cleanup from the matched legacy entry.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At 3b7b242, the nested-entry lookup and icon cleanup come from quattro through #19965. The only production change is to prefer an indexed top-level TUI over a nested namesake. The legacy icon-name mismatch remains an inherited issue in both versions; this change preserves the existing cleanup.

Checked by Codex High, Opus 5.5 High and muse-wonder via pi (thinking high); the reviewers agreed, and independence is not guaranteed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. The icon-name mismatch is inherited behavior, not introduced by this PR; the diff’s production change only makes an indexed top-level launcher take precedence over a nested namesake. I’ll withdraw this finding as out of scope for the change.

if [[ ${OMARCHY_REMOVE_NOTIFY:-true} != "false" ]]; then
omarchy-notification-send -g  "TUI removed" "$APP_NAME"
Expand Down
141 changes: 141 additions & 0 deletions test/shell.d/tui-install-hardening-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/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"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Exec and Icon escaping untested

This test injects a newline only through the app name, although the change also escapes the Exec and Icon values. A regression in either of those write paths would pass the hardening test. Add newline-bearing command and icon cases to cover them.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At 3b7b242, desktop-value escaping is unchanged from quattro after #19965. The standalone hardening test has been consolidated into the existing webapp-name-test.sh; the production diff contains no Exec or Icon writer change. The added regression forces both find orders and checks which launcher is deleted and retained, including the nested fallback.

Checked by Codex High, Opus 5.5 High and muse-wonder via pi (thinking high); the reviewers agreed, and independence is not guaranteed.


# 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"

# 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 \
"$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"