Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 22 additions & 4 deletions bin/omarchy-locale-generate
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ set -euo pipefail
# Runs as root through the passwordless rule in etc/sudoers.d/omarchy-locale-generate,
# which names each registry code as the whole argument. Under sudo's
# secure_path a dev link prepends a user-writable checkout bin/ to PATH, and
# every helper this calls by bare name (grep, sed, locale-gen) is a system
# tool, so pin PATH to trusted system directories while holding root.
# every helper this calls by bare name (grep, sed, localedef) is a system
# tool, so pin PATH to trusted system directories while holding root. sudo
# keeps the caller's directory, where localedef looks for a source first.
if (( EUID == 0 )); then
export PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin
cd /
fi

# Root reads only the packaged registry and the real locale.gen; the
Expand All @@ -31,7 +33,7 @@ if (( $# != 1 )) || [[ ! $code =~ ^[a-z]{2,3}(-[A-Za-z]{2})?$ ]]; then
fi

# The code only selects a locale from the registry; nothing the caller passes
# reaches locale.gen or locale-gen itself.
# reaches locale.gen or localedef itself.
locale=$(jq -r --arg code "$code" '.[$code].locale // empty' "$registry")
if [[ ! $locale =~ ^[a-z]{2,3}_[A-Z]{2}\.UTF-8$ ]]; then
echo "Unknown language code: $code" >&2
Expand All @@ -45,4 +47,20 @@ if grep -qE "^#$base(\.UTF-8)? UTF-8" "$locale_gen" 2>/dev/null; then
elif ! grep -qE "^$base(\.UTF-8)? UTF-8" "$locale_gen" 2>/dev/null; then
echo "$locale UTF-8" >>"$locale_gen"
fi
locale-gen "$locale" >/dev/null

# locale-gen would delete the archive and rebuild every locale, ignoring each
# failure; add only this one, and ask the archive whether it landed, since
# localedef exits 1 both for warnings and for a locale it could not add.
# Like locale-gen, a source under /usr/local overrides the packaged one.
input=$base
for name in "$locale" "$base"; do
if [[ -e /usr/local/share/i18n/locales/$name ]]; then
input=/usr/local/share/i18n/locales/$name
break
fi
done
localedef -i "$input" -c -f UTF-8 -A /usr/share/locale/locale.alias "$locale" >/dev/null || true
if ! localedef --list-archive | grep -Fx "$base.utf8" >/dev/null; then
echo "Could not generate $locale" >&2
exit 1
fi
15 changes: 10 additions & 5 deletions test/shell.d/i18n-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,17 @@ export XDG_CONFIG_HOME=$work/config
session_env=$XDG_CONFIG_HOME/uwsm/env.d/50-omarchy-language
printf '#da_DK.UTF-8 UTF-8\n' >"$OMARCHY_LOCALE_GEN"

# Stub the outside world: locale-gen records its argument, notifications
# record their text, and omarchy-language itself is the real one via PATH.
# Stub the outside world: localedef records the locale it adds and lists those
# as its archive, notifications record their text, and omarchy-language itself
# is the real one via PATH.
mkdir -p "$work/stubs"
cat >"$work/stubs/locale-gen" <<'STUB'
cat >"$work/stubs/localedef" <<'STUB'
#!/bin/bash
echo "$@" >>"$LOCALE_GEN_LOG"
if [[ $1 == "--list-archive" ]]; then
sed 's/\.UTF-8$/.utf8/' "$LOCALE_GEN_LOG"
exit
fi
echo "${@: -1}" >>"$LOCALE_GEN_LOG"
STUB
cat >"$work/stubs/omarchy-notification-send" <<'STUB'
#!/bin/bash
Expand Down Expand Up @@ -400,7 +405,7 @@ da_changed_raw=$(jq -r '."language.changed"' "$ROOT/default/i18n/da/cli.json")
assert_bash_equal "$(omarchy-i18n language.changed 'Research & Development')" "${da_changed_raw//%1/"Research & Development"}" "omarchy-i18n keeps ampersands in arguments literal"

# With the locale already generated, picking the language needs no
# privilege at all: no password prompt, no locale-gen.
# privilege at all: no password prompt, no localedef.
cat >"$work/stubs/locale" <<'STUB'
#!/bin/bash
if [[ $1 == "-a" ]]; then printf 'en_US.utf8\nda_DK.utf8\n'; else echo "LANG=da_DK.UTF-8"; fi
Expand Down
44 changes: 38 additions & 6 deletions test/shell.d/locale-sudoers-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,70 @@ pass "omarchy-language asks sudo for the grant before prompting"
# Root resolves bare helpers under sudo's secure_path, which a dev link
# prepends a user-writable checkout bin/ to, so the helper pins PATH to system
# directories once it holds root, and only then.
gated=$(grep -A1 -E '^if \(\( EUID == 0 \)\); then$' "$generator" || true)
gated=$(sed -n '/^if (( EUID == 0 )); then$/,/^fi$/p' "$generator")
[[ $gated == *"export PATH=/usr/local/sbin:/usr/local/bin:/usr/bin"* ]] ||
fail "omarchy-locale-generate gates the trusted-PATH pin on holding root"
# localedef reads a bare-named source from the working directory first, and
# sudo keeps the caller's, so root leaves it.
[[ $gated == *$'\n cd /\n'* ]] || fail "omarchy-locale-generate leaves the caller's directory when it holds root"
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
# The test overrides must never reach root: root reads the packaged registry
# and the real locale.gen.
overrides=$(sed -n '/^if (( EUID != 0 )); then$/,/^fi$/p' "$generator")
[[ $overrides == *'OMARCHY_LOCALE_REGISTRY'* && $overrides == *'OMARCHY_LOCALE_GEN'* ]] ||
fail "omarchy-locale-generate honors its test overrides only without root"
pass "omarchy-locale-generate pins PATH and its files when it holds root"

# Unprivileged runs against fixtures: locale-gen is stubbed, the registry and
# Unprivileged runs against fixtures: localedef is stubbed, the registry and
# locale.gen are overridden.
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
mkdir -p "$work/bin"
cat >"$work/bin/locale-gen" <<'STUB'
# localedef lists the archive from a file, and adds to it unless told the
# addition fails, exiting with the status it is given either way.
cat >"$work/bin/localedef" <<'STUB'
#!/bin/bash
if [[ $1 == "--list-archive" ]]; then
cat "$LOCALEDEF_ARCHIVE"
exit
fi
echo "$@" >>"$LOCALE_GEN_LOG"
locale=${*: -1}
[[ -n ${LOCALEDEF_FAILS:-} ]] || echo "${locale%.UTF-8}.utf8" >>"$LOCALEDEF_ARCHIVE"
exit "${LOCALEDEF_STATUS:-0}"
STUB
chmod +x "$work/bin/locale-gen"
export LOCALE_GEN_LOG=$work/locale-gen.log OMARCHY_LOCALE_GEN=$work/locale.gen OMARCHY_LOCALE_REGISTRY=$registry
chmod +x "$work/bin/localedef"
export LOCALE_GEN_LOG=$work/locale-gen.log LOCALEDEF_ARCHIVE=$work/locale-archive OMARCHY_LOCALE_GEN=$work/locale.gen OMARCHY_LOCALE_REGISTRY=$registry
printf '#sv_SE.UTF-8 UTF-8\n#hi_IN UTF-8\n' >"$OMARCHY_LOCALE_GEN"
run() { PATH="$work/bin:$PATH" bash "$generator" "$@"; }

# The source lookup is the host's, so a /usr/local override here is a right answer.
: >"$LOCALE_GEN_LOG"
run sv || fail "omarchy-locale-generate generates a registry language"
grep -qx 'sv_SE.UTF-8 UTF-8' "$OMARCHY_LOCALE_GEN" || fail "the language's locale is enabled in locale.gen" "$(cat "$OMARCHY_LOCALE_GEN")"
[[ $(cat "$LOCALE_GEN_LOG") == "sv_SE.UTF-8" ]] || fail "locale-gen generates the language's locale" "$(cat "$LOCALE_GEN_LOG")"
[[ $(cat "$LOCALE_GEN_LOG") == "-i "?(/usr/local/share/i18n/locales/)sv_SE?(.UTF-8)" -c -f UTF-8 -A /usr/share/locale/locale.alias sv_SE.UTF-8" ]] ||
fail "localedef adds only the language's locale" "$(cat "$LOCALE_GEN_LOG")"
: >"$LOCALE_GEN_LOG"
run hi || fail "omarchy-locale-generate generates a locale locale.gen spells without its codeset"
grep -qx 'hi_IN UTF-8' "$OMARCHY_LOCALE_GEN" || fail "a codeset-less entry is enabled in place" "$(cat "$OMARCHY_LOCALE_GEN")"
[[ $(cat "$LOCALE_GEN_LOG") == "-i "?(/usr/local/share/i18n/locales/)hi_IN?(.UTF-8)" -c -f UTF-8 -A /usr/share/locale/locale.alias hi_IN.UTF-8" ]] ||
fail "localedef names a codeset-less entry with its codeset" "$(cat "$LOCALE_GEN_LOG")"
run fil || fail "omarchy-locale-generate adds a locale locale.gen doesn't list"
grep -qx 'fil_PH.UTF-8 UTF-8' "$OMARCHY_LOCALE_GEN" || fail "a missing locale is appended to locale.gen" "$(cat "$OMARCHY_LOCALE_GEN")"
pass "omarchy-locale-generate enables and generates a registry language's locale"

# localedef exits 1 for warnings and for a locale it could not add to a full
# archive alike, so only the archive says whether the language can switch.
: >"$LOCALEDEF_ARCHIVE"
LOCALEDEF_STATUS=1 run sv || fail "localedef warnings still generate the locale"
for status in 1 4 135; do
: >"$LOCALE_GEN_LOG"
if LOCALEDEF_FAILS=1 LOCALEDEF_STATUS=$status run da 2>/dev/null; then
fail "omarchy-locale-generate fails when localedef adds nothing and exits $status"
fi
[[ $(cat "$LOCALE_GEN_LOG") == *" da_DK.UTF-8" ]] || fail "the failure is localedef's, not an earlier refusal" "$(cat "$LOCALE_GEN_LOG")"
done
pass "omarchy-locale-generate fails when the locale is not generated"

for bad in "" "xx" "../da" "da;id" "da sv" "DA"; do
: >"$LOCALE_GEN_LOG"
if run "$bad" 2>/dev/null; then
Expand Down
Loading