Skip to content
Merged
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
12 changes: 12 additions & 0 deletions bin/omarchy-hook
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ if (( $# < 1 )); then
fi

HOOK=$1

# Hook names are fixed labels chosen by Omarchy code (post-update, theme-set,
# font-set). The name becomes a filename under the hooks directory. A slash
# would turn it into directory levels, and a bare `.` or `..` would point bash
# at the directory itself or its parent. Refuse those rather than follow them.
# Dots inside a name (a..b) are fine; once slashes are out, only the whole
# name being `.` or `..` can leave the directory.
if [[ -z $HOOK || $HOOK == */* || $HOOK == "." || $HOOK == ".." ]]; then
echo "Invalid hook name: $HOOK" >&2
exit 2
fi

HOOK_PATH="$HOME/.config/omarchy/hooks/$1"
HOOK_DIR="$HOOK_PATH.d"
shift
Expand Down
11 changes: 11 additions & 0 deletions bin/omarchy-hook-install
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ fi

HOOK_TYPE=$1
HOOK_FILE=$2

# Hook types are the same labels omarchy-hook runs (post-update, theme-set).
# The type becomes a directory under the hooks directory. A slash would turn
# it into directory levels. A bare `.` or `..` is a name the runner already
# refuses, so installing under it would write a hook nothing can run. Refuse
# those rather than mkdir/cp into them. Dots inside a name (a..b) are fine.
if [[ -z $HOOK_TYPE || $HOOK_TYPE == */* || $HOOK_TYPE == "." || $HOOK_TYPE == ".." ]]; then
echo "Invalid hook name: $HOOK_TYPE" >&2
exit 2
fi

HOOK_DIR="$HOME/.config/omarchy/hooks/$HOOK_TYPE.d"
HOOK_NAME=$(basename "$HOOK_FILE")
HOOK_PATH="$HOOK_DIR/$HOOK_NAME"
Expand Down
15 changes: 14 additions & 1 deletion bin/omarchy-state
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ if [[ -z $STATE_NAME ]]; then
fi

case "$COMMAND" in
set) touch "$STATE_DIR/$STATE_NAME" ;;
set)
# State names are fixed labels (reboot-required, restart-*-required). The
# name becomes a filename under the state directory. A slash would turn it
# into directory levels, and a bare `.` or `..` would touch the directory
# itself or its parent. Refuse those. Dots inside a name (a..b) are fine;
# once slashes are out, only the whole name being `.` or `..` can leave the
# directory. clear needs no such guard: find -name matches basenames only,
# so a pattern can never walk out of the directory.
if [[ $STATE_NAME == */* || $STATE_NAME == "." || $STATE_NAME == ".." ]]; then
echo "Invalid state name: $STATE_NAME" >&2
exit 2
fi
touch "$STATE_DIR/$STATE_NAME"
;;
clear) find "$STATE_DIR" -maxdepth 1 -type f -name "$STATE_NAME" -delete ;;
esac
158 changes: 158 additions & 0 deletions test/shell.d/hook-state-name-guard-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#!/bin/bash

set -euo pipefail

source "$(dirname "${BASH_SOURCE[0]}")/base-test.sh"

work_dir=$(mktemp -d)
trap 'rm -rf "$work_dir"' EXIT

fake_home="$work_dir/home"
mkdir -p "$fake_home/.config/omarchy/hooks" "$fake_home/.local/state/omarchy"

# --- omarchy-hook --------------------------------------------------------------

# A hook name is a label, not a path. One carrying a slash, or one that is a
# bare `.` or `..`, would run a script from outside the hooks directory.

cat >"$fake_home/.config/omarchy/hooks/test-hook" <<'SH'
touch "$HOME/hook-ran"
SH

HOME="$fake_home" "$ROOT/bin/omarchy-hook" test-hook
[[ -f $fake_home/hook-ran ]] ||
fail "omarchy hook runs a named hook from the hooks directory"
pass "omarchy hook runs a named hook from the hooks directory"

# Dots inside a name are not a path. a..b stays inside the hooks directory.
cat >"$fake_home/.config/omarchy/hooks/a..b" <<'SH'
touch "$HOME/dotted-hook-ran"
SH

HOME="$fake_home" "$ROOT/bin/omarchy-hook" a..b
[[ -f $fake_home/dotted-hook-ran ]] ||
fail "omarchy hook accepts a hook name with dots in the middle"
pass "omarchy hook accepts a hook name with dots in the middle"

for name in . ..; do
status=0
HOME="$fake_home" "$ROOT/bin/omarchy-hook" "$name" >/dev/null 2>&1 || status=$?
(( status == 2 )) ||
fail "omarchy hook refuses a hook name of $name" "exit: $status"
pass "omarchy hook refuses a hook name of $name"
done

# This file sits where a name of ../../evil would resolve: hooks/../.. is
# ~/.config.
cat >"$fake_home/.config/evil" <<'SH'
touch "$HOME/escape-ran"
SH
chmod +x "$fake_home/.config/evil"

status=0
HOME="$fake_home" "$ROOT/bin/omarchy-hook" "../../evil" >/dev/null 2>&1 || status=$?
(( status == 2 )) ||
fail "omarchy hook refuses a hook name with a dot-dot" "exit: $status"
[[ ! -e $fake_home/escape-ran ]] ||
fail "omarchy hook runs nothing when it refuses the name"
pass "omarchy hook refuses a hook name with a dot-dot"

status=0
HOME="$fake_home" "$ROOT/bin/omarchy-hook" "sub/dir" >/dev/null 2>&1 || status=$?
(( status == 2 )) ||
fail "omarchy hook refuses a hook name with a slash" "exit: $status"
pass "omarchy hook refuses a hook name with a slash"

# --- omarchy-hook-install ------------------------------------------------------

# The installer joins the type into ~/.config/omarchy/hooks/<type>.d before
# mkdir/cp. The runner already refuses a slashed type; install must too, or a
# name the runner will not run still lands on disk.

source_hook="$work_dir/source-hook"
cat >"$source_hook" <<'SH'
#!/bin/bash
true
SH

HOME="$fake_home" "$ROOT/bin/omarchy-hook-install" post-update "$source_hook" >/dev/null
[[ -f $fake_home/.config/omarchy/hooks/post-update.d/source-hook ]] ||
fail "omarchy hook install still installs a named hook"
pass "omarchy hook install still installs a named hook"

HOME="$fake_home" "$ROOT/bin/omarchy-hook-install" a..b "$source_hook" >/dev/null
[[ -f $fake_home/.config/omarchy/hooks/a..b.d/source-hook ]] ||
fail "omarchy hook install accepts a hook name with dots in the middle"
pass "omarchy hook install accepts a hook name with dots in the middle"

for name in . ..; do
status=0
HOME="$fake_home" "$ROOT/bin/omarchy-hook-install" "$name" "$source_hook" >/dev/null 2>&1 || status=$?
(( status == 2 )) ||
fail "omarchy hook install refuses a hook name of $name" "exit: $status"
[[ ! -e $fake_home/.config/omarchy/hooks/${name}.d ]] ||
fail "omarchy hook install creates no directory for a hook name of $name"
pass "omarchy hook install refuses a hook name of $name"
done

# hooks/../../evil.d is ~/.config/evil.d. The guard must fire before mkdir.
status=0
HOME="$fake_home" "$ROOT/bin/omarchy-hook-install" "../../evil" "$source_hook" >/dev/null 2>&1 || status=$?
(( status == 2 )) ||
fail "omarchy hook install refuses a hook name with a dot-dot" "exit: $status"
[[ ! -e $fake_home/.config/evil.d ]] ||
fail "omarchy hook install creates nothing outside the hooks directory"
pass "omarchy hook install refuses a hook name with a dot-dot"

status=0
HOME="$fake_home" "$ROOT/bin/omarchy-hook-install" "sub/dir" "$source_hook" >/dev/null 2>&1 || status=$?
(( status == 2 )) ||
fail "omarchy hook install refuses a hook name with a slash" "exit: $status"
[[ ! -e $fake_home/.config/omarchy/hooks/sub ]] ||
fail "omarchy hook install creates no nested directory from a slashed name"
pass "omarchy hook install refuses a hook name with a slash"

# --- omarchy-state -------------------------------------------------------------

state_dir="$fake_home/.local/state/omarchy"

HOME="$fake_home" "$ROOT/bin/omarchy-state" set reboot-required
[[ -f $state_dir/reboot-required ]] ||
fail "omarchy state set still creates a plain state file"
pass "omarchy state set still creates a plain state file"

HOME="$fake_home" "$ROOT/bin/omarchy-state" set v1..2
[[ -f $state_dir/v1..2 ]] ||
fail "omarchy state set accepts a state name with dots in the middle"
pass "omarchy state set accepts a state name with dots in the middle"

for name in . ..; do
status=0
HOME="$fake_home" "$ROOT/bin/omarchy-state" set "$name" >/dev/null 2>&1 || status=$?
(( status == 2 )) ||
fail "omarchy state set refuses a state name of $name" "exit: $status"
pass "omarchy state set refuses a state name of $name"
done

# state/../.. is ~/.local. The guard must fire before touch gets there.
status=0
HOME="$fake_home" "$ROOT/bin/omarchy-state" set "../../escape" >/dev/null 2>&1 || status=$?
(( status == 2 )) ||
fail "omarchy state set refuses a state name with a dot-dot" "exit: $status"
[[ ! -e $fake_home/.local/escape ]] ||
fail "omarchy state set creates nothing outside the state directory"
pass "omarchy state set refuses a state name with a dot-dot"

status=0
HOME="$fake_home" "$ROOT/bin/omarchy-state" set "sub/dir" >/dev/null 2>&1 || status=$?
(( status == 2 )) ||
fail "omarchy state set refuses a state name with a slash" "exit: $status"
pass "omarchy state set refuses a state name with a slash"

# clear takes patterns by design ("state-name-or-pattern") and matches
# basenames through find -name, so it can never walk out of the directory.
touch "$state_dir/restart-a-required" "$state_dir/restart-b-required" "$state_dir/keep-me"
HOME="$fake_home" "$ROOT/bin/omarchy-state" clear "restart-*-required"
[[ ! -e $state_dir/restart-a-required && ! -e $state_dir/restart-b-required && -f $state_dir/keep-me ]] ||
fail "omarchy state clear still clears matching patterns only"
pass "omarchy state clear still clears matching patterns only"