Skip to content

Commit f400bef

Browse files
authored
Merge pull request #1500 from ADawidowski/enhance_webtheme_validation
Enhance theme validation
2 parents 3d8f194 + 5dc25ed commit f400bef

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

src/s6/debian-root/usr/local/bin/bash_functions.sh

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -416,19 +416,16 @@ setup_web_port() {
416416
}
417417

418418
setup_web_theme(){
419-
# Parse the WEBTHEME variable, if it exists, and set the selected theme if it is one of the supported values.
420-
# If an invalid theme name was supplied, setup WEBTHEME to use the default-light theme.
419+
# Parse the WEBTHEME variable, if it exists, and set the selected theme if it is one of the supported values (i.e. it is one of the existing theme
420+
# file names and passes a regexp sanity check). If an invalid theme name was supplied, setup WEBTHEME to use the default-light theme.
421421
if [ -n "${WEBTHEME}" ]; then
422-
case "${WEBTHEME}" in
423-
"default-dark" | "default-darker" | "default-light" | "default-auto" | "high-contrast" | "high-contrast-dark" | "lcars")
424-
echo " [i] Setting Web Theme based on WEBTHEME variable, using value ${WEBTHEME}"
425-
change_setting "WEBTHEME" "${WEBTHEME}"
426-
;;
427-
*)
428-
echo " [!] Invalid theme name supplied: ${WEBTHEME}, falling back to default-light."
429-
change_setting "WEBTHEME" "default-light"
430-
;;
431-
esac
422+
if grep -qf <(find /var/www/html/admin/style/themes/ -type f -printf '%f\n' | sed -ne 's/^\([a-zA-Z0-9_-]\+\)\.css$/\1/gp') -xF - <<< "${WEBTHEME}"; then
423+
echo " [i] Setting Web Theme based on WEBTHEME variable, using value ${WEBTHEME}"
424+
change_setting "WEBTHEME" "${WEBTHEME}"
425+
else
426+
echo " [!] Invalid theme name supplied: ${WEBTHEME}, falling back to default-light."
427+
change_setting "WEBTHEME" "default-light"
428+
fi
432429
fi
433430
}
434431

test/tests/test_bash_functions.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,48 @@ def test_setup_lighttpd_bind(
288288
assert "server.bind" not in config.stdout
289289
else:
290290
assert f'server.bind = "{expected_bind}"' in config.stdout
291+
292+
@pytest.fixture(autouse=True)
293+
def run_around_test_setup_web_theme(docker):
294+
"""Fixture to execute around test_setup_web_theme"""
295+
docker.run("touch /var/www/html/admin/style/themes/{badtheme,bad.theme.css,goodtheme.css}")
296+
297+
yield
298+
299+
docker.run("rm /var/www/html/admin/style/themes/{badtheme,bad.theme.css,goodtheme.css}")
300+
301+
@pytest.mark.parametrize(
302+
"args_env,test_theme,expected_success",
303+
[
304+
("-e WEBTHEME=asd", "asd", False),
305+
("-e WEBTHEME=default-light", "default-light", True),
306+
#("-e WEBTHEME=", "", False), # the tested function does nothing in this case
307+
("-e WEBTHEME=default-dark", "default-dark", True),
308+
("-e WEBTHEME=efault-dark", "efault-dark", False),
309+
("-e WEBTHEME=efault-dar", "efault-dar", False),
310+
("-e WEBTHEME=default-dar", "default-dar", False),
311+
("-e WEBTHEME=xdefault-dark", "xdefault-dark", False),
312+
("-e WEBTHEME=xdefault-darkx", "xdefault-darkx", False),
313+
("-e WEBTHEME=default-darkx", "default-darkx", False),
314+
("-e WEBTHEME=badtheme", "badtheme", False), # the theme file does not have the right extension
315+
("-e WEBTHEME=badtheme.css", "badtheme.css", False), # hacking attempt ?
316+
("-e WEBTHEME=bad.theme", "bad.theme", False), # invalid name - has dot
317+
("-e WEBTHEME=goodtheme", "goodtheme", True),
318+
("-e WEBTHEME=goodtheme.css", "goodtheme.css", False), # hacking attempt ?
319+
("-e WEBTHEME=+", "+", False),
320+
("-e WEBTHEME=.", ".", False),
321+
],
322+
)
323+
def test_setup_web_theme(
324+
docker, args_env, test_theme, expected_success
325+
):
326+
"""Web theme name validation works"""
327+
DEFAULT_THEME = "default-light"
328+
function = docker.run(". /usr/local/bin/bash_functions.sh ; setup_web_theme")
329+
330+
if expected_success:
331+
assert f' [i] setting web theme based on webtheme variable, using value {test_theme}' in function.stdout.lower()
332+
assert docker.run(_grep(f'^WEBTHEME={test_theme}$', SETUPVARS_LOC)).rc == 0
333+
else:
334+
assert f' [!] invalid theme name supplied: {test_theme}, falling back to {DEFAULT_THEME}.' in function.stdout.lower()
335+
assert docker.run(_grep(f'^WEBTHEME={DEFAULT_THEME}$', SETUPVARS_LOC)).rc == 0

0 commit comments

Comments
 (0)