Skip to content
Open
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
8 changes: 8 additions & 0 deletions bin/omarchy-launch-browser
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
default_browser=$(omarchy-cmd-default-browser)
browser_exec=$(sed -n 's/^Exec=\([^ ]*\).*/\1/p' {~/.local,~/.nix-profile,/usr}/share/applications/$default_browser 2>/dev/null | head -1)

# An entry whose command is this launcher would recurse: each generation probes
# the browser's flags and hands the URL to another generation through
# systemd-run, doubling until the process table fills. Refuse instead.
if [[ $(basename -- "$browser_exec") == omarchy-launch-browser ]]; then
echo "omarchy-launch-browser: the default browser entry ${default_browser:-<none>} runs omarchy-launch-browser itself; refusing to recurse" >&2
exit 1
fi

# Only a private launch needs the browser's flag, and probing a Firefox-family
# browser's --help starts the whole binary, so ordinary launches skip it and
# leave their arguments as they are.
Expand Down
53 changes: 47 additions & 6 deletions test/shell.d/launch-browser-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ chmod +x "$mock_bin"/*
launch_log="$test_tmp/launch"
focus_log="$test_tmp/focus"
xdg_settings_browser="$test_tmp/xdg-settings-browser"
HOME="$test_home" PATH="$mock_bin:$PATH" HYPRLAND_INSTANCE_SIGNATURE=test \
HOME="$test_home" PATH="$mock_bin:$ROOT/bin:$PATH" HYPRLAND_INSTANCE_SIGNATURE=test \
OMARCHY_TEST_BROWSER_LAUNCH="$launch_log" OMARCHY_TEST_BROWSER_FOCUS="$focus_log" \
bash "$ROOT/bin/omarchy-launch-browser"

[[ ! -e $focus_log ]] || fail "browser launcher leaves a new window on the current workspace"

HOME="$test_home" PATH="$mock_bin:$PATH" HYPRLAND_INSTANCE_SIGNATURE=test \
HOME="$test_home" PATH="$mock_bin:$ROOT/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

[[ ! -e $focus_log ]] || fail "private browser launcher leaves a new window on the current workspace"

HOME="$test_home" PATH="$mock_bin:$PATH" HYPRLAND_INSTANCE_SIGNATURE=test \
HOME="$test_home" PATH="$mock_bin:$ROOT/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/authorize"

Expand All @@ -68,7 +68,7 @@ grep -Fx '^chromium.*$' "$focus_log" >/dev/null || fail "browser launcher focuse

rm -f "$focus_log" "$xdg_settings_browser"

HOME="$test_home" PATH="$mock_bin:$PATH" HYPRLAND_INSTANCE_SIGNATURE=test \
HOME="$test_home" PATH="$mock_bin:$ROOT/bin:$PATH" HYPRLAND_INSTANCE_SIGNATURE=test \
BROWSER=omarchy-launch-browser OMARCHY_TEST_XDG_SETTINGS_EMPTY=1 \
OMARCHY_TEST_BROWSER_LAUNCH="$launch_log" OMARCHY_TEST_BROWSER_FOCUS="$focus_log" \
OMARCHY_TEST_XDG_SETTINGS_BROWSER="$xdg_settings_browser" \
Expand All @@ -90,7 +90,7 @@ cat >"$mock_bin/omarchy-cmd-browser-handoff" <<'SH'
SH
chmod +x "$mock_bin/omarchy-cmd-browser-handoff"

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

[[ ! -e $launch_log ]] || fail "browser launcher starts no browser when the running one takes the URL"
Expand All @@ -104,7 +104,7 @@ cat >"$mock_bin/chromium" <<'SH'
SH
chmod +x "$mock_bin/chromium"
launch() {
HOME="$test_home" PATH="$mock_bin:$PATH" OMARCHY_TEST_BROWSER_LAUNCH="$launch_log" \
HOME="$test_home" PATH="$mock_bin:$ROOT/bin:$PATH" OMARCHY_TEST_BROWSER_LAUNCH="$launch_log" \
OMARCHY_TEST_BROWSER_FOCUS="$focus_log" OMARCHY_TEST_BROWSER_HELP="$help_log" \
bash "$ROOT/bin/omarchy-launch-browser" "$@"
}
Expand All @@ -118,3 +118,44 @@ launch --private
[[ -s $help_log ]] || fail "a private launch probes the browser's private flag"
grep -F -- 'chromium --incognito' "$launch_log" >/dev/null || fail "a private launch passes the browser's private flag"
pass "a private launch passes the browser's private flag"

# A default-handler entry whose Exec is this launcher used to recurse: each
# generation probed the command's --help and handed the URL to another
# generation through systemd-run, doubling until the process table filled.
cat >"$test_home/.local/share/applications/selfref.desktop" <<'EOF'
[Desktop Entry]
Exec=omarchy-launch-browser %U
EOF
cat >"$mock_bin/omarchy-launch-browser" <<'SH'
#!/bin/bash
printf '%s\n' "$*" >>"$OMARCHY_TEST_BROWSER_RECURSE"
SH
Comment on lines +129 to +132

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 Re-entry skips the test stub

The new omarchy-launch-browser stub is never made executable. Since $ROOT/bin follows $mock_bin in PATH, an unexpected call skips the stub and runs the real launcher instead. Add chmod +x "$mock_bin/omarchy-launch-browser" so the test can safely record re-entry in recurse_log.

Suggested change
cat >"$mock_bin/omarchy-launch-browser" <<'SH'
#!/bin/bash
printf '%s\n' "$*" >>"$OMARCHY_TEST_BROWSER_RECURSE"
SH
cat >"$mock_bin/omarchy-launch-browser" <<'SH'
#!/bin/bash
printf '%s\n' "$*" >>"$OMARCHY_TEST_BROWSER_RECURSE"
SH
chmod +x "$mock_bin/omarchy-launch-browser"

mkdir -p "$test_home/.config"
cat >"$test_home/.config/mimeapps.list" <<'EOF'
[Default Applications]
x-scheme-handler/http=selfref.desktop
x-scheme-handler/https=selfref.desktop
EOF
recurse_log="$test_tmp/recurse"
error_log="$test_tmp/error"
rm -f "$launch_log"

set +e
HOME="$test_home" PATH="$mock_bin:$ROOT/bin:$PATH" \
OMARCHY_TEST_BROWSER_LAUNCH="$launch_log" OMARCHY_TEST_BROWSER_RECURSE="$recurse_log" \
bash "$ROOT/bin/omarchy-launch-browser" "https://example.test/loop" 2>"$error_log"
launcher_status=$?
set -e

(( launcher_status != 0 )) || fail "a self-referencing default browser entry exits with an error"
[[ ! -s $recurse_log ]] || fail "a self-referencing default browser entry runs omarchy-launch-browser again

$(cat "$recurse_log")"
[[ ! -e $launch_log ]] || fail "a self-referencing default browser entry hands the URL to systemd-run

$(cat "$launch_log")"
grep -F 'selfref.desktop' "$error_log" >/dev/null ||
fail "a self-referencing default browser entry names the offending entry

$(cat "$error_log")"
pass "a self-referencing default browser entry is refused instead of recursing"