The Settings upgrade can switch to a chosen branch (#954)

* The Settings upgrade can switch to a chosen branch

The Upgrade panel gains a Branch input: a dropdown listing the current
branch, master, and release that also takes a typed branch name.
/api/upgrade/start accepts an optional {"branch": ...}; without it the
current branch is upgraded as before.  A branch is written into the systemd
env file and passed to `upgrade.sh -b`, so only plain git branch names are
accepted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* The Controller upgrade refuses a branch that is not a plain branch name

The Controller's upgrade script wrote its branch straight into the systemd
env file, so a newline in it added a variable, and a leading dash became an
upgrade.sh option.  It now accepts the same branch names as the API, and no
script parameter may span lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-11-08 08:48:58 -07:00 • committed by Roland
parent f3f762ef1b
commit d88db90ce9
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
10 changed files with 304 additions and 28 deletions

View file

@ -6,6 +6,7 @@ Uses systemd oneshot services for process survival across controller restarts.
"""
import logging
import re
import subprocess
import time
from datetime import datetime, timezone
@ -15,6 +16,22 @@ from controller.lib.config import is_docker_mode
logger = logging.getLogger(__name__)
# A branch is written into the systemd env file and passed to `upgrade.sh -b`, so only plain git branch names are
# accepted: no whitespace (a newline would add an env variable), no leading dash (an option), no `..`.
# Matches `wrolpi.upgrade.validate_branch`.
BRANCH_PATTERN = re.compile(r"[A-Za-z0-9_][A-Za-z0-9_.\-]*(/[A-Za-z0-9_][A-Za-z0-9_.\-]*)*")
MAX_BRANCH_LENGTH = 200
def is_valid_branch(branch: str) -> bool:
"""Return True if `branch` is a branch name `upgrade.sh` may be given."""
return isinstance(branch, str) \
and len(branch) <= MAX_BRANCH_LENGTH \
and BRANCH_PATTERN.fullmatch(branch) is not None \
and ".." not in branch \
and not branch.endswith((".", ".lock"))
# Available scripts with their configurations
# Parameters define inputs that scripts accept:
# - name: Parameter name (used as env var name, e.g., "branch" -> "BRANCH=value")
@ -254,6 +271,15 @@ def start_script(name: str, params: Optional[dict] = None) -> dict:
env_file = script_config.get("env_file")
if env_file:
values = {key: value for key, value in (params or {}).items() if value}
for parameter in parameters:
value = values.get(parameter["name"])
if value is None:
continue
# Each value is one line of the env file.
if parameter["type"] == "branch" and not is_valid_branch(value):
return {"success": False, "error": f"Invalid branch name: {value!r}"}
if "\n" in str(value) or "\r" in str(value):
return {"success": False, "error": f"Invalid {parameter['label']}: must be a single line"}
for parameter in parameters:
if parameter["type"] == "branch" and not values.get(parameter["name"]):
# upgrade.sh upgrades the current branch when it is not given one.