Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
132 changes: 121 additions & 11 deletions bin/omarchy-launch-browser
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,138 @@ default_browser=$(env -u BROWSER xdg-settings get default-web-browser)
if [[ -z $default_browser ]]; then
default_browser=$(xdg-mime query default x-scheme-handler/https)
fi
browser_exec=$(sed -n 's/^Exec=\([^ ]*\).*/\1/p' {~/.local,~/.nix-profile,/usr}/share/applications/$default_browser 2>/dev/null | head -1)

if $browser_exec --help 2>/dev/null | grep -q MOZ_LOG; then
desktop_file=$(ls {~/.local,~/.nix-profile,/usr}/share/applications/$default_browser 2>/dev/null | head -1)
exec_line=$(sed -n 's/^Exec=//p' "$desktop_file" 2>/dev/null | head -1)
if [[ -z $exec_line ]]; then
exec_line="$default_browser"
fi

# Parse a freedesktop Exec value without eval. Desktop entries can be user
# supplied, so shell evaluation here would turn a launcher lookup into code
# execution. This handles whitespace, quotes, and backslash escapes while
# keeping every token as a literal argv element.
parse_exec_line() {
local input="$1"
local token=""
local started=0
local in_single=0
local in_double=0
local escaped=0
local char
local index
tokens=()

for ((index = 0; index < ${#input}; index++)); do
char="${input:index:1}"
if (( escaped )); then
token+="$char"
escaped=0
started=1
elif [[ $char == "\\" && $in_single -eq 0 ]]; then
escaped=1
started=1
elif [[ $char == "'" && $in_double -eq 0 ]]; then
in_single=$((1 - in_single))
started=1
elif [[ $char == '"' && $in_single -eq 0 ]]; then
in_double=$((1 - in_double))
started=1
elif [[ $char == ' ' || $char == $'\t' ]]; then
if (( started )); then
tokens+=("$token")
token=""
started=0
fi
else
token+="$char"
started=1
fi
done

if (( escaped )); then token+='\\'; fi
if (( in_single || in_double )); then return 1; fi
if (( started )); then tokens+=("$token"); fi
}

if ! parse_exec_line "$exec_line" || [[ ${#tokens[@]} -eq 0 ]]; then
echo "Invalid browser Exec entry; refusing to launch." >&2
exit 1
fi

moz_regex='(firefox|librewolf|waterfox|tor-browser|mozilla)'
if [[ ${exec_line,,} =~ $moz_regex ]] || \
(${exec_line%% *} --help 2>/dev/null | grep -q MOZ_LOG); then
private_flag="--private-window"
elif [[ $browser_exec =~ edge ]]; then
elif [[ ${exec_line,,} =~ edge ]]; then
private_flag="--inprivate"
else
private_flag="--incognito"
fi

systemd-run --user --quiet --collect --unit="omarchy-browser-$(date +%s%N)" \
--property=StandardOutput=null --property=StandardError=null \
uwsm-app -- "$browser_exec" "${@/--private/$private_flag}"

is_private=false
url=""
for argument in "$@"; do
if [[ $argument != "--private" ]]; then
url=$argument
break
if [[ $argument == "--private" ]]; then
is_private=true
elif [[ -z $url ]]; then
url="$argument"
fi
done

cmd=()
has_url_slot=false

for token in "${tokens[@]}"; do
case "$token" in
%u|%U)
has_url_slot=true
if [[ -n $url ]]; then
if [[ $is_private == true ]]; then
cmd+=("$private_flag")
is_private=false
fi
cmd+=("$url")
fi
;;
%f|%F|%i|%c|%k|%%)
;;
*)
cmd+=("$token")
;;
esac
done

if [[ $is_private == true ]]; then
cmd+=("$private_flag")
fi

if [[ -n $url && $has_url_slot == false ]]; then
cmd+=("$url")
fi

systemd-run --user --quiet --collect --unit="omarchy-browser-$(date +%s%N)" \
--property=StandardOutput=null --property=StandardError=null \
uwsm-app -- "${cmd[@]}"

if [[ -n $url && -n ${HYPRLAND_INSTANCE_SIGNATURE:-} ]]; then
omarchy-hyprland-focus-app "^$(basename "$browser_exec" -stable).*$" || true
focus_name=""
for token in "${tokens[@]}"; do
base=$(basename "$token" 2>/dev/null || true)
base="${base%-stable}"
if [[ $base =~ ^(chromium|google-chrome|brave|firefox|edge|vivaldi|opera|helium|zen|librewolf|waterfox|tor-browser) ]]; then
focus_name="$base"
break
elif [[ $token =~ \.([^.]+)$ ]]; then
sub="${BASH_REMATCH[1],,}"
if [[ $sub =~ ^(chromium|chrome|brave|firefox|edge|vivaldi|opera|helium|zen|librewolf|waterfox) ]]; then
focus_name="$sub"
break
fi
fi
done
if [[ -z $focus_name ]]; then
focus_name="$(basename "${tokens[0]}" -stable)"
fi
omarchy-hyprland-focus-app "^${focus_name}.*$" || true
fi
66 changes: 65 additions & 1 deletion bin/omarchy-launch-webapp
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,68 @@ google-chrome* | brave* | microsoft-edge* | opera* | vivaldi* | helium*) ;;
*) browser="chromium.desktop" ;;
esac

exec setsid uwsm-app -- $(sed -n 's/^Exec=\([^ ]*\).*/\1/p' {~/.local,~/.nix-profile,/usr}/share/applications/$browser 2>/dev/null | head -1) --app="$1" "${@:2}"
desktop_file=$(ls {~/.local,~/.nix-profile,/usr}/share/applications/$browser 2>/dev/null | head -1)
exec_line=$(sed -n 's/^Exec=//p' "$desktop_file" 2>/dev/null | head -1)
if [[ -z $exec_line ]]; then
exec_line="$browser"
fi

# Parse the freedesktop Exec field as literal argv data. Never use eval here:
# a writable desktop entry must not gain shell execution during launch.
parse_exec_line() {
local input="$1"
local token=""
local started=0
local in_single=0
local in_double=0
local escaped=0
local char
local index
tokens=()

for ((index = 0; index < ${#input}; index++)); do
char="${input:index:1}"
if (( escaped )); then
token+="$char"
escaped=0
started=1
elif [[ $char == "\\" && $in_single -eq 0 ]]; then
escaped=1
started=1
elif [[ $char == "'" && $in_double -eq 0 ]]; then
in_single=$((1 - in_single))
started=1
elif [[ $char == '"' && $in_single -eq 0 ]]; then
in_double=$((1 - in_double))
started=1
elif [[ $char == ' ' || $char == $'\t' ]]; then
if (( started )); then
tokens+=("$token")
token=""
started=0
fi
else
token+="$char"
started=1
fi
done

if (( escaped )); then token+='\\'; fi
if (( in_single || in_double )); then return 1; fi
if (( started )); then tokens+=("$token"); fi
}

if ! parse_exec_line "$exec_line" || [[ ${#tokens[@]} -eq 0 ]]; then
echo "Invalid browser Exec entry; refusing to launch." >&2
exit 1
fi

cmd=()
for token in "${tokens[@]}"; do
case "$token" in
%u|%U|%f|%F|%i|%c|%k|%%) ;;
*) cmd+=("$token") ;;
esac
done

exec setsid uwsm-app -- "${cmd[@]}" --app="$1" "${@:2}"
62 changes: 61 additions & 1 deletion test/shell.d/launch-browser-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ cat >"$mock_bin/systemd-run" <<'SH'
#!/bin/bash
printf '%s\n' "$*" >"$OMARCHY_TEST_BROWSER_LAUNCH"
SH
cat >"$mock_bin/setsid" <<'SH'
#!/bin/bash
printf '%s\n' "$*" >"$OMARCHY_TEST_WEBAPP_LAUNCH"
SH
cat >"$mock_bin/omarchy-hyprland-focus-app" <<'SH'
#!/bin/bash
printf '%s\n' "$1" >"$OMARCHY_TEST_BROWSER_FOCUS"
Expand Down Expand Up @@ -78,4 +82,60 @@ grep -F 'https://example.test/fallback' "$launch_log" >/dev/null ||
grep -Fx '^chromium.*$' "$focus_log" >/dev/null ||
fail "browser launcher focuses the browser resolved from the HTTPS handler"

pass "browser launcher follows opened links to the browser workspace"
# Test wrapped Exec= entries (issue #11718: flatpak, env, wrapper scripts)
cat >"$test_home/.local/share/applications/flatpak-firefox.desktop" <<'EOF'
[Desktop Entry]
Exec=/usr/bin/flatpak run org.mozilla.firefox %u
EOF

cat >"$mock_bin/xdg-settings" <<'SH'
#!/bin/bash
echo flatpak-firefox.desktop
SH

rm -f "$launch_log" "$focus_log"
HOME="$test_home" PATH="$mock_bin:$PATH" HYPRLAND_INSTANCE_SIGNATURE=test \
OMARCHY_TEST_BROWSER_LAUNCH="$launch_log" OMARCHY_TEST_BROWSER_FOCUS="$focus_log" \
bash "$ROOT/bin/omarchy-launch-browser" "https://example.test/flatpak"

grep -F '/usr/bin/flatpak run org.mozilla.firefox https://example.test/flatpak' "$launch_log" >/dev/null ||
fail "browser launcher preserves full flatpak wrapped Exec command line"

rm -f "$launch_log" "$focus_log"
HOME="$test_home" PATH="$mock_bin:$PATH" HYPRLAND_INSTANCE_SIGNATURE=test \
OMARCHY_TEST_BROWSER_LAUNCH="$launch_log" OMARCHY_TEST_BROWSER_FOCUS="$focus_log" \
bash "$ROOT/bin/omarchy-launch-browser" --private "https://example.test/flatpak-private"

grep -F '/usr/bin/flatpak run org.mozilla.firefox --private-window https://example.test/flatpak-private' "$launch_log" >/dev/null ||
fail "browser launcher applies private flag to wrapped browser before the URL"

# A shell metacharacter in a desktop entry must stay an argument, never run.
pwned_file="$test_tmp/desktop-entry-pwned"
cat >"$test_home/.local/share/applications/unsafe.desktop" <<EOF
[Desktop Entry]
Exec=chromium --profile-directory=Default; touch $pwned_file %u
EOF
cat >"$mock_bin/xdg-settings" <<'SH'
#!/bin/bash
echo unsafe.desktop
SH
chmod +x "$mock_bin/xdg-settings"
rm -f "$launch_log" "$focus_log" "$pwned_file"
HOME="$test_home" PATH="$mock_bin:$PATH" HYPRLAND_INSTANCE_SIGNATURE=test \
OMARCHY_TEST_BROWSER_LAUNCH="$launch_log" OMARCHY_TEST_BROWSER_FOCUS="$focus_log" \
bash "$ROOT/bin/omarchy-launch-browser" "https://example.test/unsafe"
[[ ! -e $pwned_file ]] || fail "browser launcher evaluates a desktop Exec entry as shell code"

# The webapp launcher uses the same literal parser and must have the same
# no-eval guarantee.
cat >"$test_home/.local/share/applications/chromium.desktop" <<EOF
[Desktop Entry]
Exec=chromium --profile-directory=Default; touch $pwned_file
EOF
rm -f "$pwned_file"
HOME="$test_home" PATH="$mock_bin:$PATH" \
OMARCHY_TEST_WEBAPP_LAUNCH="$launch_log" \
bash "$ROOT/bin/omarchy-launch-webapp" "https://example.test/webapp"
[[ ! -e $pwned_file ]] || fail "webapp launcher evaluates a desktop Exec entry as shell code"

pass "browser launcher follows opened links to the browser workspace and supports wrapped Exec commands"