"""Exercise the real router CGI POST with LF/CRLF and a strict POSIX shell."""
import importlib.util
import os
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
import unittest
from unittest import mock
from urllib.parse import urlencode
ROOT = Path(__file__).resolve().parents[1]
STATE = tempfile.TemporaryDirectory(prefix="owrt-xray-apply-")
spec = importlib.util.spec_from_file_location("xray_apply_hub", ROOT / "vps/owrt-remote-hub.py")
hub = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = hub
with mock.patch.dict(os.environ, {"OWRT_REMOTE_STATE_DIR": STATE.name}):
spec.loader.exec_module(hub)
SH = shutil.which("sh")
STRICT_SH = shutil.which("dash") or SH
if os.name == "nt":
git_bin = Path(os.environ.get("ProgramFiles", "C:/Program Files")) / "Git/usr/bin"
SH = str(git_bin / "sh.exe") if (git_bin / "sh.exe").exists() else SH
STRICT_SH = str(git_bin / "dash.exe") if (git_bin / "dash.exe").exists() else STRICT_SH
else:
git_bin = None
@unittest.skipUnless(SH and STRICT_SH, "POSIX sh and a strict parser are required")
class XrayApplyPostTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory(dir=STATE.name)
self.addCleanup(self.temp.cleanup)
self.directory = Path(self.temp.name)
self.directory.joinpath("key").write_text("test-web-key", encoding="utf-8")
tools = self.directory / "bin"
tools.mkdir()
tools.joinpath("owrt-remote").write_text(
'#!/bin/sh\n[ "$1" != render-client ] || [ "${RENDER_FAIL:-0}" = 0 ] || exit 7\n'
'printf "%s\\n" "$*" >>"$TEST_ROOT/operations"\n', encoding="utf-8", newline="\n")
tools.joinpath("owrt-remote").chmod(0o755)
tools.joinpath("uci").write_text(
'#!/bin/sh\n[ "$1" != -q ] || shift\n'
'[ "$#" = 2 ] || exit 255\n'
'case "$1" in\n'
' get) exit 1 ;;\n'
' set) printf "%s\\n" "$2" >>"$TEST_ROOT/uci-values" ;;\n'
' commit) printf "commit\\n" >>"$TEST_ROOT/operations" ;;\n'
' *) exit 255 ;;\nesac\n', encoding="utf-8", newline="\n")
tools.joinpath("uci").chmod(0o755)
self.env = {**os.environ, "TMPDIR": self.directory.as_posix(),
"TEST_ROOT": self.directory.as_posix(), "KEY_FILE": (self.directory / "key").as_posix(),
"TEST_STRICT_SH": Path(STRICT_SH).as_posix()}
if git_bin:
self.env["PATH"] = str(git_bin) + os.pathsep + self.env["PATH"]
self.env["PATH"] = str(tools) + os.pathsep + self.env["PATH"]
cgi = (ROOT / "files/www/cgi-bin/owrt-remote").read_text(encoding="utf-8")
mocks = r'''
sh() {
if [ "$1" = -n ]; then
cp "$2" "$TEST_ROOT/checked.sh"
printf 'check\n' >>"$TEST_ROOT/calls"
else
cp "$2" "$TEST_ROOT/executed.sh"
printf 'execute\n' >>"$TEST_ROOT/calls"
fi
"$TEST_STRICT_SH" "$@"
}
render_page() { printf 'KIND=%s\nTITLE=%s\nBODY=%s\nDRAFT=%s\n' "$RESULT_KIND" "$RESULT_TITLE" "$RESULT_BODY" "$XRAY_SETTINGS_DRAFT"; }
'''
self.cgi = self.directory / "cgi"
self.cgi.write_text(cgi.replace("\nread_request\n", mocks + "\nread_request\n", 1), encoding="utf-8", newline="\n")
self.render_cgi = self.directory / "render-cgi"
template = cgi.split('\tif [ "$SHOW_XRAY_SETTINGS" = "1" ]; then\n', 1)[1].split('\n\tfi\n', 1)[0]
render = '\nrender_page() {\nkey="$(param key)"\n' + template + '\n}\n'
self.render_cgi.write_text(cgi.replace("\nread_request\n", render + "\nread_request\n", 1), encoding="utf-8", newline="\n")
def post(self, script, key="test-web-key", render=False, action="apply_xray_settings"):
body = urlencode({"key": key, "action": action, "xray_settings_script": script}).encode("ascii")
result = subprocess.run([SH, (self.render_cgi if render else self.cgi).as_posix()], input=body, capture_output=True,
env={**self.env, "REQUEST_METHOD": "POST", "CONTENT_TYPE": "application/x-www-form-urlencoded",
"CONTENT_LENGTH": str(len(body))}, timeout=20)
self.assertEqual(result.returncode, 0, result.stderr.decode("utf-8", errors="replace"))
self.assertFalse(list(self.directory.glob("owrt-remote-*")), "CGI temporary files must be removed")
return result.stdout.decode("utf-8")
def test_mobile_smart_quotes_reproduce_uci_255_and_are_repaired(self):
script = 'uci set owrtremote.main.router_name=‘Дом у моря’\nuci commit owrtremote\n'
# UCI returns 255 for too many arguments; shell syntax validation alone
# accepts these typography quotes and splits the name into three words.
broken = self.directory / "broken.sh"
broken.write_text(script, encoding="utf-8", newline="\n")
result = subprocess.run([STRICT_SH, "-e", broken.as_posix()], env=self.env, capture_output=True)
self.assertEqual(result.returncode, 255)
response = self.post(script.replace("\n", "\r\n"))
self.assertIn("KIND=ok", response)
self.assertEqual(self.directory.joinpath("uci-values").read_text(encoding="utf-8"),
"owrtremote.main.router_name=Дом у моря\n")
def test_mobile_quotes_bom_and_nonbreaking_separators(self):
script = '\ufeffuci\u00a0-q\u202fset\u00a0owrtremote.main.router_name=“Папа\'s $HOME `id` & дом”\r\n'
self.assertIn("KIND=ok", self.post(script))
self.assertEqual(self.directory.joinpath("uci-values").read_text(encoding="utf-8"),
"owrtremote.main.router_name=Папа's $HOME `id` & дом\n")
def test_ascii_shell_quotes_preserve_unicode_values(self):
value = "Дом ‘у моря’\u00a0— “офис”\u202f+ 100% & $HOME `id`"
script = "uci set owrtremote.main.router_name='" + value + "'\n"
self.assertIn("KIND=ok", self.post(script))
self.assertEqual(self.directory.joinpath("uci-values").read_text(encoding="utf-8"),
"owrtremote.main.router_name=" + value + "\n")
self.assertEqual(self.directory.joinpath("executed.sh").read_text(encoding="utf-8"),
"#!/bin/sh\n" + script)
def test_genuine_uci_error_is_not_reported_as_success_and_keeps_draft(self):
script = 'uci set owrtremote.main.router_name=broken name\nuci commit owrtremote\n'
response = self.post(script)
self.assertIn("KIND=bad", response)
self.assertIn("rc=255", response)
self.assertIn("DRAFT=" + script.rstrip("\n"), response)
self.assertFalse(self.directory.joinpath("operations").exists())
def test_mobile_form_disables_text_rewriting_and_escapes_failed_draft(self):
# Render the actual CGI textarea template on an unsuccessful POST.
script = "printf '%s' '&'\nexit 255\n"
response = self.post(script, render=True)
self.assertIn('autocorrect="off" autocapitalize="off" autocomplete="off"', response)
self.assertIn('wrap="soft" spellcheck="false"', response)
self.assertIn("</textarea><script>bad</script>&", response)
self.assertNotIn("", response)
cleared = self.post(script, render=True, action="show_xray_settings")
self.assertNotIn("<script>bad</script>", cleared)
def test_uci_looking_lines_in_multiline_values_and_heredocs_stay_literal(self):
value = 'before\nuci set owrtremote.main.router_name=‘Дом у моря’\nafter\n'
for script in ("printf '%s' '" + value + "' >\"$TEST_ROOT/literal\"\n",
'cat >"$TEST_ROOT/literal" <<\'EOF\'\n' + value + 'EOF\n'):
with self.subTest(script=script):
self.assertIn("KIND=ok", self.post(script))
self.assertEqual(self.directory.joinpath("executed.sh").read_bytes(),
("#!/bin/sh\n" + script).encode("utf-8"))
self.assertFalse(self.directory.joinpath("uci-values").exists())
def test_form_crlf_is_normalized_before_strict_shell_parsing(self):
script = "if [ x = x ]; then\n printf '%s' 'name + percent% & quote\"'\nfi\n"
response = self.post(script.replace("\n", "\r\n"))
self.assertIn("KIND=ok", response)
self.assertIn('BODY=name + percent% & quote"', response)
self.assertNotIn(b"\r", self.directory.joinpath("checked.sh").read_bytes())
self.assertNotIn(b"\r", self.directory.joinpath("executed.sh").read_bytes())
self.assertEqual(self.directory.joinpath("calls").read_text(), "check\nexecute\n")
def test_lf_and_crlf_heredoc_keep_lines_and_blank_lines(self):
script = 'cat >"$TEST_ROOT/applied" <<\'EOF\'\nfirst\n\nlast + % &\nEOF\n'
for ending in ("\n", "\r\n"):
with self.subTest(ending=repr(ending)):
self.assertIn("KIND=ok", self.post(script.replace("\n", ending)))
self.assertEqual(self.directory.joinpath("applied").read_bytes(), b"first\n\nlast + % &\n")
def test_incomplete_script_is_rejected_before_any_changes(self):
response = self.post('printf changed >"$TEST_ROOT/applied"\nif [ x = x ]; then\n')
self.assertIn("KIND=bad", response)
self.assertIn("rc=2", response)
self.assertIn("Настройки не изменены", response)
self.assertFalse(self.directory.joinpath("applied").exists())
self.assertFalse(self.directory.joinpath("executed.sh").exists())
def test_failed_render_stops_before_later_restart_or_heartbeat(self):
self.env["RENDER_FAIL"] = "1"
script = 'owrt-remote render-client\nprintf restarted >"$TEST_ROOT/restarted"\n'
response = self.post(script)
self.assertIn("KIND=bad", response)
self.assertIn("rc=7", response)
self.assertFalse(self.directory.joinpath("restarted").exists())
def test_empty_and_unauthorized_form_do_not_execute_commands(self):
self.assertIn("KIND=warn", self.post(" \r\n\t\r\n"))
self.assertFalse(self.directory.joinpath("calls").exists())
response = self.post('printf changed >"$TEST_ROOT/applied"', key="wrong-key")
self.assertIn("Доступ запрещен", response)
self.assertFalse(self.directory.joinpath("applied").exists())
self.assertFalse(self.directory.joinpath("calls").exists())
def test_full_hub_config_through_form_preserves_manual_host_and_wan(self):
self.directory.joinpath("uci").write_text("owrtremote.main.vps_host=192.0.2.25\nowrtremote.main.wan_interface=wwan\n", encoding="utf-8")
mocks = r'''
uci() {
[ "$1" != -q ] || shift
case "$1" in
get)
[ "$2" != owrtremote.main ] || { echo remote; return; }
awk -F= -v key="$2" '$1 == key { sub(/^[^=]*=/, ""); print; found=1; exit } END { if(!found) exit 1 }' "$TEST_ROOT/uci" ;;
set)
key="${2%%=*}"; value="${2#*=}"
awk -F= -v key="$key" '$1 != key' "$TEST_ROOT/uci" >"$TEST_ROOT/uci.new"
printf '%s=%s\n' "$key" "$value" >>"$TEST_ROOT/uci.new"
mv "$TEST_ROOT/uci.new" "$TEST_ROOT/uci" ;;
commit) printf 'commit\n' >>"$TEST_ROOT/operations" ;;
esac
}
test_service() { printf 'service %s\n' "$*" >>"$TEST_ROOT/operations"; }
'''
row = {"id": "main", "name": "test + quote's & name", "vps_host": "hub.example", "reverse_tag": "reverse-in"}
with mock.patch.object(hub, "agent_token", return_value="test-agent-token"):
generated = hub.make_openwrt_config(row, "https://hub.example")
generated = generated.replace("/etc/init.d/owrt-remote", "test_service")
response = self.post((mocks + generated).replace("\n", "\r\n"))
self.assertIn("KIND=ok", response)
state = self.directory.joinpath("uci").read_bytes()
self.assertNotIn(b"\r", state)
self.assertIn(b"owrtremote.main.vps_host=192.0.2.25\n", state)
self.assertIn(b"owrtremote.main.wan_interface=wwan\n", state)
self.assertIn(b"owrtremote.main.router_name=test + quote's & name\n", state)
self.assertEqual(self.directory.joinpath("operations").read_text(),
"commit\nrender-client\nservice enable\nservice restart\nheartbeat\n")
if __name__ == "__main__":
unittest.main()