From 35a1d98529ddabff97a8536d9f1ec1f6c2a02a45 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 28 Nov 2026 11:08:04 +0000 Subject: [PATCH] fix(web-app-jitsi): skip realigning service accounts that already carry the declared password MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prosody 13 closes every session of an account whenever its password is set, even to the same value. The realign rewrote focus@auth on every deploy, which kicked the focus session; mod_client_proxy on focus. then had no target, so the client's conference request got service-unavailable (main run 46079003545, Moodle#3). realign_accounts.sh now checks the declared password with prosodyctl shell test_password, passing hex-encoded Lua strings so any character is safe, and leaves accounts that already carry it untouched. Five unit tests against a stubbed prosodyctl cover unchanged, stale, missing, metacharacter and empty passwords. 🎯 Fix: 86 % 💥 Break: 3 % 💸 Tokens: – ⏱️ Velocity: – 🛡️ Security: 100 % 🏆 Score: 83 🚦 Gate: ⛔ fix 86 % < 90 Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 --- .../files/shell/realign_accounts.sh | 17 +- .../python/roles/web-app-jitsi/__init__.py | 0 .../roles/web-app-jitsi/files/__init__.py | 0 .../web-app-jitsi/files/shell/__init__.py | 0 .../files/shell/test_realign_accounts.py | 164 ++++++++++++++++++ 5 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 tests/unit/python/roles/web-app-jitsi/__init__.py create mode 100644 tests/unit/python/roles/web-app-jitsi/files/__init__.py create mode 100644 tests/unit/python/roles/web-app-jitsi/files/shell/__init__.py create mode 100644 tests/unit/python/roles/web-app-jitsi/files/shell/test_realign_accounts.py diff --git a/roles/web-app-jitsi/files/shell/realign_accounts.sh b/roles/web-app-jitsi/files/shell/realign_accounts.sh index 778ed2a393..897dfd6cda 100644 --- a/roles/web-app-jitsi/files/shell/realign_accounts.sh +++ b/roles/web-app-jitsi/files/shell/realign_accounts.sh @@ -5,7 +5,7 @@ # JITSI_REALIGN_JVB_PASSWORD declared password of jvb@ # JITSI_REALIGN_JIGASI_PASSWORD declared password of jigasi@ # JITSI_REALIGN_JIBRI_PASSWORD declared password of jibri@ -# Output: one [CHANGED] line per account on stdout, [FAIL] plus the prosody error on stderr. +# Output: one [CHANGED] line per rewritten or registered account on stdout, [FAIL] plus the prosody error on stderr. # Exit: 0 when every account carries the declared password, 1 otherwise. set -eu @@ -15,6 +15,16 @@ prosody_shell() { prosodyctl --config "$PROSODY_CFG" shell "$@" 2>&1 } +lua_string() { + printf '(("%s"):gsub("..", function(h) return string.char(tonumber(h, 16)) end))' \ + "$(printf '%s' "$1" | od -An -v -tx1 | tr -d ' \n')" +} + +carries_password() { + output=$(prosody_shell "> prosody.hosts[$(lua_string "$JITSI_REALIGN_AUTH_DOMAIN")].users.test_password($(lua_string "$1"), $(lua_string "$2"))") || return 1 + [ "$(printf '%s' "$output" | tail -n 1)" = "Result: true" ] +} + realign() { user="$1" password="$2" @@ -23,6 +33,11 @@ realign() { echo "[FAIL] ${jid}: no password supplied" >&2 return 1 fi + # Prosody 13 closes every session of an account whose password is set, even to the same value; + # a kicked focus session leaves focus. (mod_client_proxy) without a target, so conference requests get service-unavailable. + if carries_password "$user" "$password"; then + return 0 + fi if output=$(prosody_shell user password "$jid" "$password"); then echo "[CHANGED] ${jid} now carries the declared password" return 0 diff --git a/tests/unit/python/roles/web-app-jitsi/__init__.py b/tests/unit/python/roles/web-app-jitsi/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/unit/python/roles/web-app-jitsi/files/__init__.py b/tests/unit/python/roles/web-app-jitsi/files/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/unit/python/roles/web-app-jitsi/files/shell/__init__.py b/tests/unit/python/roles/web-app-jitsi/files/shell/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/unit/python/roles/web-app-jitsi/files/shell/test_realign_accounts.py b/tests/unit/python/roles/web-app-jitsi/files/shell/test_realign_accounts.py new file mode 100644 index 0000000000..a66f912204 --- /dev/null +++ b/tests/unit/python/roles/web-app-jitsi/files/shell/test_realign_accounts.py @@ -0,0 +1,164 @@ +import os +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +from utils import PROJECT_ROOT +from utils.cache.files import read_text + +SCRIPT = PROJECT_ROOT / "roles/web-app-jitsi/files/shell/realign_accounts.sh" +DOMAIN = "auth.meet.jitsi" +ACCOUNTS = ("focus", "jvb", "jigasi", "jibri") +DECLARED = { + "focus": "focus-secret", + "jvb": "jvb-secret", + "jigasi": "jigasi-secret", + "jibri": "jibri-secret", +} + +PROSODYCTL_STUB = """#!{python} +import os +import re +import sys +from pathlib import Path + +store = Path(os.environ["STUB_ACCOUNTS"]) +log = Path(os.environ["STUB_LOG"]) +args = sys.argv[sys.argv.index("shell") + 1:] + + +def record(entry): + with log.open("a", encoding="utf-8") as handle: + handle.write(entry + "\\n") + + +if len(args) == 1 and args[0].startswith(">"): + host, user, password = ( + bytes.fromhex(value).decode("utf-8") + for value in re.findall( + r'^> prosody\\.hosts\\[\\(\\("([0-9a-f]*)"\\).*\\]\\.users\\.test_password' + r'\\(\\(\\("([0-9a-f]*)"\\).*, \\(\\("([0-9a-f]*)"\\).*\\)$', + args[0], + )[0] + ) + account = store / f"{{user}}@{{host}}" + record(f"test {{user}}@{{host}}") + if account.is_file() and account.read_text(encoding="utf-8") == password: + print("Result: true") + sys.exit(0) + print("Error: Auth failed. Invalid username or password.", file=sys.stderr) + sys.exit(1) + +section, action, jid, password = args +account = store / jid +record(f"{{action}} {{jid}}") +if action == "password": + if not account.is_file(): + print("Error: No such user", file=sys.stderr) + sys.exit(1) + account.write_text(password, encoding="utf-8") + print("OK: User password changed") + sys.exit(0) +if action == "create": + if account.is_file(): + print("Error: User exists", file=sys.stderr) + sys.exit(1) + account.write_text(password, encoding="utf-8") + print("OK: User created") + sys.exit(0) +sys.exit(2) +""" + + +class TestRealignAccounts(unittest.TestCase): + def setUp(self): + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + root = Path(tmp.name) + self.store = root / "accounts" + self.store.mkdir() + self.log = root / "prosodyctl.log" + self.log.touch() + bin_dir = root / "bin" + bin_dir.mkdir() + stub = bin_dir / "prosodyctl" + stub.write_text(PROSODYCTL_STUB.format(python=sys.executable)) + stub.chmod(0o755) + self.env = { + **os.environ, + "PATH": f"{bin_dir}{os.pathsep}{os.environ['PATH']}", + "STUB_ACCOUNTS": str(self.store), + "STUB_LOG": str(self.log), + "JITSI_REALIGN_AUTH_DOMAIN": DOMAIN, + **{ + f"JITSI_REALIGN_{user.upper()}_PASSWORD": password + for user, password in DECLARED.items() + }, + } + + def plant(self, **passwords): + for user, password in passwords.items(): + (self.store / f"{user}@{DOMAIN}").write_text(password, encoding="utf-8") + + def realign(self, **env): + done = subprocess.run( + ["sh", "-s"], + input=read_text(str(SCRIPT)), + env={**self.env, **env}, + capture_output=True, + text=True, + check=False, + ) + self.assertEqual(done.returncode, 0, done.stderr) + return done + + def writes(self): + text = self.log.read_text(encoding="utf-8") # nocheck: cache-read tempdir + return [call for call in text.splitlines() if not call.startswith("test ")] + + def stored(self, user): + account = self.store / f"{user}@{DOMAIN}" + return account.read_text(encoding="utf-8") # nocheck: cache-read tempdir + + def test_accounts_that_already_carry_the_declared_password_are_not_rewritten(self): + self.plant(**DECLARED) + done = self.realign() + self.assertEqual(self.writes(), []) + self.assertNotIn("[CHANGED]", done.stdout) + + def test_only_the_stale_account_is_rewritten(self): + self.plant(**{**DECLARED, "focus": "stale"}) + done = self.realign() + self.assertEqual(self.writes(), [f"password focus@{DOMAIN}"]) + self.assertEqual(self.stored("focus"), DECLARED["focus"]) + self.assertEqual(done.stdout.count("[CHANGED]"), 1) + + def test_missing_accounts_are_registered_with_the_declared_password(self): + done = self.realign() + for user in ACCOUNTS: + self.assertEqual(self.stored(user), DECLARED[user]) + self.assertEqual(done.stdout.count("[CHANGED]"), len(ACCOUNTS)) + + def test_a_password_with_lua_and_shell_metacharacters_verifies_unchanged(self): + awkward = "q\"'\\]]==]%s $(x) `y` ümlaut" + self.plant(**{**DECLARED, "jvb": awkward}) + self.realign(JITSI_REALIGN_JVB_PASSWORD=awkward) + self.assertEqual(self.writes(), []) + + def test_an_empty_declared_password_fails(self): + done = subprocess.run( + ["sh", "-s"], + input=read_text(str(SCRIPT)), + env={**self.env, "JITSI_REALIGN_FOCUS_PASSWORD": ""}, + capture_output=True, + text=True, + check=False, + ) + self.assertNotEqual(done.returncode, 0) + self.assertIn("[FAIL]", done.stderr) + + +if __name__ == "__main__": + unittest.main()