Skip to content

Commit bebe19b

Browse files
committed
Harden browser policy parent directories and validate theme RGB
install -d follows a planted ancestor symlink, and a writable parent can rename the managed leaf aside. chromium.theme is user-installed, so only a 0-255 RGB triple becomes a colour.
1 parent 87dfa14 commit bebe19b

5 files changed

Lines changed: 161 additions & 8 deletions

File tree

‎bin/omarchy-theme-set-browser‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33
# omarchy:summary=Apply the current theme color to Chromium, Chrome, Edge, and Brave
44
# omarchy:hidden=true
55

6+
source "$OMARCHY_PATH/install/helpers/browser-policy.sh"
7+
68
CHROMIUM_THEME=$HOME/.local/state/omarchy/current/theme/chromium.theme
9+
THEME_HEX_COLOR=$BROWSER_POLICY_DEFAULT_COLOR
710

811
if [[ -f $CHROMIUM_THEME ]]; then
9-
THEME_RGB_COLOR=$(<$CHROMIUM_THEME)
10-
THEME_HEX_COLOR=$(printf '#%02x%02x%02x' ${THEME_RGB_COLOR//,/ })
11-
else
12-
# Use a default, neutral grey if theme doesn't have a color
13-
THEME_HEX_COLOR="#1c2027"
12+
THEME_HEX_COLOR=$(browser_policy_theme_hex "$(<$CHROMIUM_THEME)")
1413
fi
1514

16-
source "$OMARCHY_PATH/install/helpers/browser-policy.sh"
17-
1815
set_browser_policy() {
1916
browser_policy_write_color "$1" "$THEME_HEX_COLOR"
2017
}

‎install/helpers/browser-policy.sh‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,26 @@ BROWSER_POLICY_MANAGED_DIRS=(
1313
/etc/brave/policies/managed
1414
)
1515

16+
# Ancestors of the managed dirs, shortest first. A writable or attacker-owned
17+
# parent can rename the leaf aside; install -d follows a planted symlink.
18+
BROWSER_POLICY_PARENT_DIRS=(
19+
/etc/chromium
20+
/etc/chromium/policies
21+
/etc/opt/chrome
22+
/etc/opt/chrome/policies
23+
/etc/opt/edge
24+
/etc/opt/edge/policies
25+
/etc/brave
26+
/etc/brave/policies
27+
)
28+
1629
BROWSER_POLICY_FIREFOX_DIRS=(
1730
/usr/lib/firefox/distribution
1831
/opt/zen-browser/distribution
1932
)
2033

34+
BROWSER_POLICY_DEFAULT_COLOR="#1c2027"
35+
2136
browser_policy_setup_group() {
2237
local provisioning_dir="${OMARCHY_PROVISIONING_DIR:-/var/lib/omarchy/provisioning}"
2338

@@ -59,13 +74,65 @@ browser_policy_dir_hardened() {
5974
[[ $(stat -c '%G' "$dir") == $BROWSER_POLICY_GROUP ]] || return 1
6075
}
6176

77+
browser_policy_parent_hardened() {
78+
local dir=$1
79+
80+
[[ -d $dir && ! -L $dir ]] || return 1
81+
[[ $(stat -c '%a' "$dir") == "755" ]] || return 1
82+
[[ $(stat -c '%U' "$dir") == "root" ]] || return 1
83+
}
84+
85+
browser_policy_parents_hardened() {
86+
local dir=$1
87+
local parent
88+
89+
for parent in "${BROWSER_POLICY_PARENT_DIRS[@]}"; do
90+
[[ $dir == "$parent"/* ]] || continue
91+
[[ -e $parent || -L $parent ]] || continue
92+
browser_policy_parent_hardened "$parent" || return 1
93+
done
94+
}
95+
96+
browser_policy_setup_parent() {
97+
local dir=$1
98+
99+
if [[ -L $dir || ( -e $dir && ! -d $dir ) ]]; then
100+
as_root rm -rf -- "$dir"
101+
fi
102+
as_root install -d -m 0755 -o root -g root "$dir"
103+
}
104+
105+
browser_policy_setup_parents_for() {
106+
local dir=$1
107+
local parent
108+
109+
for parent in "${BROWSER_POLICY_PARENT_DIRS[@]}"; do
110+
[[ $dir == "$parent"/* ]] || continue
111+
browser_policy_setup_parent "$parent"
112+
done
113+
}
114+
62115
browser_policy_setup_dir() {
63116
local dir=$1
64117

118+
browser_policy_setup_parents_for "$dir"
65119
as_root install -d -m 2775 -o root -g "$BROWSER_POLICY_GROUP" "$dir"
66120
browser_policy_purge_dir "$dir"
67121
}
68122

123+
# Themes are user-installed. Accept only three 0-255 components.
124+
browser_policy_theme_hex() {
125+
local theme_rgb=$1
126+
127+
if [[ $theme_rgb =~ ^[[:space:]]*([0-9]{1,3})[[:space:]]*,[[:space:]]*([0-9]{1,3})[[:space:]]*,[[:space:]]*([0-9]{1,3})[[:space:]]*$ ]] &&
128+
(( 10#${BASH_REMATCH[1]} < 256 && 10#${BASH_REMATCH[2]} < 256 && 10#${BASH_REMATCH[3]} < 256 )); then
129+
printf '#%02x%02x%02x' "$((10#${BASH_REMATCH[1]}))" "$((10#${BASH_REMATCH[2]}))" "$((10#${BASH_REMATCH[3]}))"
130+
return
131+
fi
132+
133+
printf '%s' "$BROWSER_POLICY_DEFAULT_COLOR"
134+
}
135+
69136
browser_policy_file_owner() {
70137
local user
71138

‎migrations/1787515927.sh‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ browser_policy_grant_user "${USER:-$(id -un)}"
88
repaired=0
99
for dir in "${BROWSER_POLICY_MANAGED_DIRS[@]}"; do
1010
[[ -d $dir ]] || continue
11-
browser_policy_dir_hardened "$dir" && continue
11+
if browser_policy_dir_hardened "$dir" && browser_policy_parents_hardened "$dir"; then
12+
continue
13+
fi
1214
browser_policy_setup_dir "$dir"
1315
repaired=1
1416
done

‎test/shell.d/browser-policy-dir-test.sh‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,87 @@ fi
168168
BROWSER_POLICY_GROUP=omarchy-browser-policy
169169
pass "a hardened directory must be root-owned"
170170

171+
saved_parent_dirs=("${BROWSER_POLICY_PARENT_DIRS[@]}")
172+
parent_root=$test_tmp/parents
173+
mkdir -p "$parent_root/etc/chromium/policies/managed/keep"
174+
printf 'keep\n' >"$parent_root/etc/chromium/policies/managed/keep/x"
175+
chmod 0777 "$parent_root/etc/chromium" "$parent_root/etc/chromium/policies"
176+
chmod 2775 "$parent_root/etc/chromium/policies/managed"
177+
BROWSER_POLICY_PARENT_DIRS=(
178+
"$parent_root/etc/chromium"
179+
"$parent_root/etc/chromium/policies"
180+
)
181+
as_root() { unprivileged_as_root "$@"; }
182+
if browser_policy_parents_hardened "$parent_root/etc/chromium/policies/managed"; then
183+
fail "a world-writable policy parent is not treated as hardened"
184+
fi
185+
browser_policy_setup_parents_for "$parent_root/etc/chromium/policies/managed"
186+
mode=$(stat -c '%a' "$parent_root/etc/chromium")
187+
[[ $mode == "755" ]] || fail "setup tightens /etc/chromium" "mode=$mode"
188+
mode=$(stat -c '%a' "$parent_root/etc/chromium/policies")
189+
[[ $mode == "755" ]] || fail "setup tightens /etc/chromium/policies" "mode=$mode"
190+
[[ -d $parent_root/etc/chromium/policies/managed/keep ]] ||
191+
fail "parent repair does not purge the managed directory"
192+
pass "policy parent directories are tightened to 0755 without purging the leaf"
193+
194+
symlink_root=$test_tmp/symlink-parents
195+
mkdir -p "$symlink_root/etc" "$symlink_root/attacker/policies/managed"
196+
printf 'planted\n' >"$symlink_root/attacker/policies/managed/evil.json"
197+
ln -s "$symlink_root/attacker" "$symlink_root/etc/chromium"
198+
BROWSER_POLICY_PARENT_DIRS=(
199+
"$symlink_root/etc/chromium"
200+
"$symlink_root/etc/chromium/policies"
201+
)
202+
as_root() { unprivileged_as_root "$@"; }
203+
browser_policy_setup_dir "$symlink_root/etc/chromium/policies/managed"
204+
[[ ! -L $symlink_root/etc/chromium ]] || fail "setup replaces a planted /etc/chromium symlink"
205+
[[ -d $symlink_root/etc/chromium && ! -L $symlink_root/etc/chromium ]] ||
206+
fail "setup recreates /etc/chromium as a real directory"
207+
[[ -d $symlink_root/etc/chromium/policies && ! -L $symlink_root/etc/chromium/policies ]] ||
208+
fail "setup recreates /etc/chromium/policies as a real directory"
209+
[[ ! -e $symlink_root/etc/chromium/policies/managed/evil.json ]] ||
210+
fail "setup does not keep policy that lived behind a planted parent symlink"
211+
grep -Fxq 'planted' "$symlink_root/attacker/policies/managed/evil.json" ||
212+
fail "replacing a parent symlink does not delete the symlink target"
213+
BROWSER_POLICY_PARENT_DIRS=("${saved_parent_dirs[@]}")
214+
pass "policy setup does not follow a planted parent symlink"
215+
216+
[[ $(browser_policy_theme_hex "242,240,229") == "#f2f0e5" ]] ||
217+
fail "theme colour converts an RGB triple to hex"
218+
[[ $(browser_policy_theme_hex $'14,31,41\n') == "#0e1f29" ]] ||
219+
fail "theme colour accepts a trailing newline"
220+
[[ $(browser_policy_theme_hex "0,0,0") == "#000000" ]] ||
221+
fail "theme colour pads single-digit components"
222+
[[ $(browser_policy_theme_hex " 12 , 11 , 12 ") == "#0c0b0c" ]] ||
223+
fail "theme colour tolerates surrounding whitespace"
224+
[[ $(browser_policy_theme_hex "08,09,10") == "#08090a" ]] ||
225+
fail "theme colour treats leading zeros as decimal"
226+
for malformed in "" "not,a,color" "1,2" "1,2,3,4" "256,0,0" "999,999,999" "-1,0,0" \
227+
"1,2,3;id" '1,2,$(id)' "0x10,0,0" "1,2,3 4,5,6"; do
228+
[[ $(browser_policy_theme_hex "$malformed") == "#1c2027" ]] ||
229+
fail "theme colour falls back to the neutral grey for '$malformed'"
230+
done
231+
pass "theme colour is six hex digits or the stock grey"
232+
233+
for theme in "$ROOT"/themes/*/chromium.theme; do
234+
[[ -f $theme ]] || continue
235+
rgb=$(<$theme)
236+
hex=$(browser_policy_theme_hex "$rgb")
237+
[[ $hex =~ ^#[0-9a-f]{6}$ ]] ||
238+
fail "shipped $(basename "$(dirname "$theme")") chromium.theme parses as hex" "got: $hex from $(printf %q "$rgb")"
239+
if [[ $hex == "#1c2027" && ! $rgb =~ ^[[:space:]]*28[[:space:]]*,[[:space:]]*32[[:space:]]*,[[:space:]]*39[[:space:]]*$ ]]; then
240+
fail "shipped $(basename "$(dirname "$theme")") chromium.theme is a valid RGB triple" "got: $(printf %q "$rgb")"
241+
fi
242+
done
243+
pass "shipped chromium.theme files parse as RGB triples"
244+
245+
grep -F 'browser_policy_theme_hex' "$ROOT/bin/omarchy-theme-set-browser" >/dev/null ||
246+
fail "omarchy-theme-set-browser parses chromium.theme through browser_policy_theme_hex"
247+
if grep -E 'printf.*THEME_RGB_COLOR' "$ROOT/bin/omarchy-theme-set-browser" >/dev/null; then
248+
fail "omarchy-theme-set-browser does not hand unvetted theme words to printf"
249+
fi
250+
pass "omarchy-theme-set-browser validates the theme colour"
251+
171252
fx_policy=$test_tmp/policies.json
172253
printf '%s\n' '{"policies":{}}' >"$fx_policy"
173254
chmod 644 "$fx_policy"
@@ -247,6 +328,8 @@ mapfile -t migrations < <(rg -l 'Stop world-writable Chromium and Firefox policy
247328
(( ${#migrations[@]} == 1 )) || fail "exactly one migration locks existing policy directories" "${migrations[*]}"
248329
grep -F 'browser_policy_dir_hardened' "${migrations[0]}" >/dev/null ||
249330
fail "the policy-directory migration no-ops a machine already repaired"
331+
grep -F 'browser_policy_parents_hardened' "${migrations[0]}" >/dev/null ||
332+
fail "the policy-directory migration repairs a world-writable parent of a hardened leaf"
250333
grep -F 'browser_policy_grant_user' "${migrations[0]}" >/dev/null ||
251334
fail "the policy-directory migration still grants the current user the group"
252335
grep -F 'BROWSER_POLICY_FIREFOX_DIRS' "${migrations[0]}" >/dev/null ||

‎test/shell.d/default-apps-test.sh‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ cmp -s "$ROOT/config/chromium-flags.conf" "$test_home/.config/chromium-flags.con
210210
fail "Chromium browser installer copies the default flags"
211211
grep -Fxq 'sudo:groupadd --system --force omarchy-browser-policy' "$setup_log" ||
212212
fail "Chromium browser installer creates the browser-policy group"
213+
grep -Fxq 'sudo:install -d -m 0755 -o root -g root /etc/chromium' "$setup_log" ||
214+
fail "Chromium browser installer creates a root-owned Chromium policy parent"
215+
grep -Fxq 'sudo:install -d -m 0755 -o root -g root /etc/chromium/policies' "$setup_log" ||
216+
fail "Chromium browser installer creates a root-owned Chromium policies parent"
213217
grep -Fxq 'sudo:install -d -m 2775 -o root -g omarchy-browser-policy /etc/chromium/policies/managed' "$setup_log" ||
214218
fail "Chromium browser installer creates a group-writable managed policy directory"
215219
grep -Fxq 'sudo:find /etc/chromium/policies/managed -mindepth 1 -maxdepth 1 ! -user root -exec rm -rf -- {} +' "$setup_log" ||

0 commit comments

Comments
 (0)