diff --git a/CHANGELOG.md b/CHANGELOG.md index 473315c7c..42a21d728 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,42 @@ ## Unreleased +### A setting chosen while the Manager menu is loading it is kept + +The Manager menu asks the server for the database mode, the component policy, +the update policy and the share option when it is first opened. A value chosen +before the answer arrived was saved, but the menu went back to showing the old +one. The menu keeps the chosen value. + +### Setting requests with an unknown value are refused + +The routes that set `db_mode`, `update_policy` and `component_policy` stored +whatever value a request carried and wrote it to `config.ini`. They answer 400 +for a value that is not one of those the Manager's menu offers, and for a JSON +body without a `value`, which ended with status 500. + +### Install requests with an unusable body are answered with 400 + +`/customnode/install/git_url` and `/customnode/install/pip` answered 500 when +the JSON body was not an object, or when `url` or `packages` was not a string. +They answer 400, as they do for a missing field. + +### Console messages show paths, URLs and package names as they are + +Manager printed many of its messages through rich, which reads square +brackets as markup. A package name such as `diffusers[torch]` was shown as +`diffusers`, a text such as `:100:` as an emoji, and a line longer than the +console was broken in the middle of a path or a URL. A text with a closing +tag such as `[/b]` raised `MarkupError` instead of being printed, in the +middle of the operation that reported it. These messages are now printed as +they are; numbers and quoted strings keep their colors. + +### The list of ComfyUI versions answers a failure with 400 again + +When the versions of ComfyUI could not be read, the request for the list ended +with status 500 and a `TypeError` in the log: the call that logs the cause was +given an argument it does not take. The cause is logged, and the answer is 400. + ### Restarting under comfy-cli no longer reports a closed log file Restarting ComfyUI from the Manager under comfy-cli printed `ValueError: I/O diff --git a/glob/manager_core.py b/glob/manager_core.py index f2fe8009f..2b0693486 100644 --- a/glob/manager_core.py +++ b/glob/manager_core.py @@ -27,7 +27,9 @@ orig_print = print -from rich import print +import rich +from rich.console import Console +from rich.markup import escape from packaging import version import uuid @@ -44,6 +46,12 @@ from node_package import InstalledNodePackage +def print(*objects, sep=" ", end="\n", file=None): + # rich's print with its highlighting only, so that a path, a URL or an error text is written as it is + console = rich.get_console() if file is None else Console(file=file) + console.print(*objects, sep=sep, end=end, markup=False, emoji=False, soft_wrap=True) + + version_code = [3, 43] version_str = f"V{version_code[0]}.{version_code[1]}" + (f'.{version_code[2]}' if len(version_code) > 2 else '') @@ -773,7 +781,7 @@ async def load_nightly(channel, mode): channel_url = normalize_channel(channel) if channel_url: if mode not in ['remote', 'local', 'cache']: - print(f"[bold red]ERROR: Invalid mode is specified `--mode {mode}`[/bold red]", file=sys.stderr) + rich.print(f"[bold red]ERROR: Invalid mode is specified `--mode {escape(mode)}`[/bold red]", file=sys.stderr) return {} # validate channel - only the channel set by the user is allowed. diff --git a/glob/manager_server.py b/glob/manager_server.py index de02ac86d..8bae983ae 100644 --- a/glob/manager_server.py +++ b/glob/manager_server.py @@ -237,13 +237,24 @@ def set_preview_method(method): ) +ALLOWED_COMPONENT_POLICIES = ('workflow', 'higher', 'mine') +ALLOWED_UPDATE_POLICIES = ('stable-comfyui', 'nightly-comfyui') +ALLOWED_DB_MODES = ('cache', 'local', 'remote') + + def set_component_policy(mode): + if mode not in ALLOWED_COMPONENT_POLICIES: + raise ValueError(f"Invalid component_policy {mode!r}; must be one of {ALLOWED_COMPONENT_POLICIES}") core.get_config()['component_policy'] = mode def set_update_policy(mode): + if mode not in ALLOWED_UPDATE_POLICIES: + raise ValueError(f"Invalid update_policy {mode!r}; must be one of {ALLOWED_UPDATE_POLICIES}") core.get_config()['update_policy'] = mode def set_db_mode(mode): + if mode not in ALLOWED_DB_MODES: + raise ValueError(f"Invalid db_mode {mode!r}; must be one of {ALLOWED_DB_MODES}") core.get_config()['db_mode'] = mode def print_comfyui_version(): @@ -1297,7 +1308,7 @@ def unzip_install(files): os.remove(temp_filename) except Exception as e: - logging.error(f"Install(unzip) error: {url} / {e}", file=sys.stderr) + logging.error(f"Install(unzip) error: {url} / {e}") return False logging.info("Installation was successful.") @@ -1319,7 +1330,7 @@ def copy_install(files, js_path_name=None): download_url(url, path, filename) except Exception as e: - logging.error(f"Install(copy) error: {url} / {e}", file=sys.stderr) + logging.error(f"Install(copy) error: {url} / {e}") return False logging.info("Installation was successful.") @@ -1340,7 +1351,7 @@ def copy_uninstall(files, js_path_name='.'): elif os.path.exists(file_path + ".disabled"): os.remove(file_path + ".disabled") except Exception as e: - logging.error(f"Uninstall(copy) error: {url} / {e}", file=sys.stderr) + logging.error(f"Uninstall(copy) error: {url} / {e}") return False logging.info("Uninstallation was successful.") @@ -1371,7 +1382,7 @@ def copy_set_active(files, is_disable, js_path_name='.'): os.rename(current_name, new_name) except Exception as e: - logging.error(f"{action_name}(copy) error: {url} / {e}", file=sys.stderr) + logging.error(f"{action_name}(copy) error: {url} / {e}") return False @@ -1601,12 +1612,14 @@ async def install_custom_node_git_url(request): # cannot forge an application/json body without a CORS preflight, which this # server does not answer — same body-handler convention as every other # request.json() route (see _reject_simple_form_content_type docstring). - # A malformed body or a missing 'url' is a client error (400), not a 500: - # don't let JSONDecodeError/KeyError bubble up as a server fault. + # A malformed body, a missing 'url' or one that is not a string is a client + # error (400), not a 500: don't let it bubble up as a server fault. try: json_data = await request.json() url = json_data['url'] - except (KeyError, ValueError): + except (KeyError, TypeError, ValueError): + url = None + if not isinstance(url, str): return web.Response(status=400, text="Invalid request body: expected JSON object with a 'url' field") res = await core.gitclone_install(url) @@ -1628,12 +1641,14 @@ async def install_custom_node_pip(request): return security_403_response(flag_token='allow_pip_install') # JSON body (not raw text) for the same preflight-forcing reason as - # /customnode/install/git_url above; malformed body or missing 'packages' - # is a 400, not a 500. + # /customnode/install/git_url above; malformed body, missing 'packages' or + # one that is not a string is a 400, not a 500. try: json_data = await request.json() packages = json_data['packages'] - except (KeyError, ValueError): + except (KeyError, TypeError, ValueError): + packages = None + if not isinstance(packages, str): return web.Response(status=400, text="Invalid request body: expected JSON object with a 'packages' field") core.pip_install(packages.split(' ')) @@ -1700,7 +1715,7 @@ async def comfyui_versions(request): res, current, latest = core.get_comfyui_versions() return web.json_response({'versions': res, 'current': current}, status=200, content_type='application/json') except Exception as e: - logging.error(f"ComfyUI update fail: {e}", file=sys.stderr) + logging.error(f"ComfyUI update fail: {e}") return web.Response(status=400) @@ -1812,7 +1827,12 @@ async def get_db_mode(request): @routes.post("/manager/db_mode") async def set_db_mode_handler(request): json_data = await request.json() - set_db_mode(json_data['value']) + try: + set_db_mode(json_data['value']) + except (KeyError, TypeError): + return web.Response(status=400, text='Invalid request') + except ValueError as e: + return web.Response(status=400, text=str(e)) core.write_config() return web.Response(status=200) @@ -1825,7 +1845,12 @@ async def get_component_policy(request): @routes.post("/manager/policy/component") async def set_component_policy_handler(request): json_data = await request.json() - set_component_policy(json_data['value']) + try: + set_component_policy(json_data['value']) + except (KeyError, TypeError): + return web.Response(status=400, text='Invalid request') + except ValueError as e: + return web.Response(status=400, text=str(e)) core.write_config() return web.Response(status=200) @@ -1838,7 +1863,12 @@ async def get_update_policy(request): @routes.post("/manager/policy/update") async def set_update_policy_handler(request): json_data = await request.json() - set_update_policy(json_data['value']) + try: + set_update_policy(json_data['value']) + except (KeyError, TypeError): + return web.Response(status=400, text='Invalid request') + except ValueError as e: + return web.Response(status=400, text=str(e)) core.write_config() return web.Response(status=200) diff --git a/js/comfyui-manager.js b/js/comfyui-manager.js index c7498087b..ad5bfdb88 100644 --- a/js/comfyui-manager.js +++ b/js/comfyui-manager.js @@ -235,6 +235,7 @@ var switch_comfyui_button = null; var update_all_button = null; var restart_stop_button = null; var update_policy_combo = null; +var update_policy_chosen = false; let share_option = 'all'; const share_options = [ @@ -642,10 +643,12 @@ async function switchComfyUI() { showVersionSelectorDialog(versions, obj.current, async (selected_version) => { if(selected_version == 'nightly') { + update_policy_chosen = true; update_policy_combo.value = 'nightly-comfyui'; api.fetchApi('/manager/policy/update', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ value: 'nightly-comfyui' }) }); } else { + update_policy_chosen = true; update_policy_combo.value = 'stable-comfyui'; api.fetchApi('/manager/policy/update', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ value: 'stable-comfyui' }) }); } @@ -996,11 +999,14 @@ class ManagerMenuDialog extends ComfyDialog { this.datasrc_combo.appendChild($el('option', { value: 'local', text: 'Local' }, [])); this.datasrc_combo.appendChild($el('option', { value: 'remote', text: 'Channel (remote)' }, [])); + // a choice made before the stored value arrives is not to be replaced by it + let db_mode_chosen = false; api.fetchApi('/manager/db_mode') .then(response => response.text()) - .then(data => { this.datasrc_combo.value = data; }); + .then(data => { if(!db_mode_chosen) this.datasrc_combo.value = data; }); this.datasrc_combo.addEventListener('change', function (event) { + db_mode_chosen = true; api.fetchApi('/manager/db_mode', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ value: event.target.value }) }); }); @@ -1142,14 +1148,18 @@ class ManagerMenuDialog extends ComfyDialog { share_combo.appendChild($el('option', { value: option[0], text: `${option[1]}` }, [])); } + let share_option_chosen = false; api.fetchApi('/manager/share_option') .then(response => response.text()) .then(data => { - share_option = to_share_option(data); - share_combo.value = share_option; + if(!share_option_chosen) { + share_option = to_share_option(data); + share_combo.value = share_option; + } }); share_combo.addEventListener('change', function (event) { + share_option_chosen = true; const value = event.target.value; share_option = value; api.fetchApi(`/manager/share_option?value=${value}`); @@ -1169,14 +1179,18 @@ class ManagerMenuDialog extends ComfyDialog { component_policy_combo.appendChild($el('option', { value: 'workflow', text: 'Use workflow version' }, [])); component_policy_combo.appendChild($el('option', { value: 'higher', text: 'Use higher version' }, [])); component_policy_combo.appendChild($el('option', { value: 'mine', text: 'Use my version' }, [])); + let component_policy_chosen = false; api.fetchApi('/manager/policy/component') .then(response => response.text()) .then(data => { - component_policy_combo.value = data; - set_component_policy(data); + if(!component_policy_chosen) { + component_policy_combo.value = data; + set_component_policy(data); + } }); component_policy_combo.addEventListener('change', function (event) { + component_policy_chosen = true; api.fetchApi('/manager/policy/component', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ value: event.target.value }) }); set_component_policy(event.target.value); }); @@ -1192,10 +1206,12 @@ class ManagerMenuDialog extends ComfyDialog { api.fetchApi('/manager/policy/update') .then(response => response.text()) .then(data => { - update_policy_combo.value = data; + if(!update_policy_chosen) + update_policy_combo.value = data; }); update_policy_combo.addEventListener('change', function (event) { + update_policy_chosen = true; api.fetchApi('/manager/policy/update', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ value: event.target.value }) }); }); diff --git a/tests/test_comfyui_versions_failure.py b/tests/test_comfyui_versions_failure.py new file mode 100644 index 000000000..ff700fcae --- /dev/null +++ b/tests/test_comfyui_versions_failure.py @@ -0,0 +1,41 @@ +"""`comfyui_versions` answers 400 when the versions of ComfyUI cannot be read. + +Each case runs the real handler of `glob/manager_server.py`; `core` is replaced. +""" +import ast +import asyncio +import logging +import sys +from types import SimpleNamespace + +from aiohttp import web + +from manager_test_utils import GLOB_DIR + + +def fail(): + raise RuntimeError("not a git repository") + + +def answer(get_comfyui_versions): + """The status and text `comfyui_versions` answers.""" + source = (GLOB_DIR / "manager_server.py").read_text(encoding="utf-8") + handler = [node for node in ast.parse(source).body + if isinstance(node, ast.AsyncFunctionDef) and node.name == "comfyui_versions"] + assert len(handler) == 1 + namespace = {"web": web, "logging": logging, "sys": sys, + "core": SimpleNamespace(get_comfyui_versions=get_comfyui_versions), + "routes": SimpleNamespace(get=lambda path: lambda handler: handler)} + exec(compile(ast.Module(body=handler, type_ignores=[]), "", "exec"), namespace) + response = asyncio.run(namespace["comfyui_versions"](None)) + return response.status, response.text + + +def test_version_list_that_cannot_be_read_is_answered_with_400(caplog): + assert answer(fail) == (400, None) + assert "ComfyUI update fail: not a git repository" in caplog.text + + +def test_version_list_is_answered_with_the_versions(): + assert answer(lambda: (["nightly", "v0.3.1"], "v0.3.1", "v0.3.1")) == ( + 200, '{"versions": ["nightly", "v0.3.1"], "current": "v0.3.1"}') diff --git a/tests/test_core_print.py b/tests/test_core_print.py new file mode 100644 index 000000000..896cc5cbe --- /dev/null +++ b/tests/test_core_print.py @@ -0,0 +1,46 @@ +"""`print` of `manager_core` writes its text as it is. + +The cases take what binds the name `print` out of `glob/manager_core.py`, an +import or a function, and call it. +""" +import ast +import io + +import pytest + +from manager_test_utils import GLOB_DIR + + +def core_print(): + source = (GLOB_DIR / "manager_core.py").read_text(encoding="utf-8") + nodes = [node for node in ast.parse(source).body + if isinstance(node, (ast.Import, ast.ImportFrom)) and "rich" in ast.unparse(node) + or isinstance(node, ast.FunctionDef) and node.name == "print"] + namespace = {} + exec(compile(ast.Module(body=nodes, type_ignores=[]), "", "exec"), namespace) + return namespace["print"] + + +def printed(*objects, **options): + out = io.StringIO() + core_print()(*objects, file=out, **options) + return out.getvalue() + + +@pytest.mark.parametrize("text", [ + "Install(git-clone) error: https://example.com/a[/b] / failed", + "## ComfyUI-Manager: EXECUTE => ['python', '-m', 'pip', 'install', 'diffusers[torch]']", + "skip black listed pip installation: 'requests[socks]'", + "Uninstall: custom_nodes/pack:100:backup", + "Download: git clone 'https://github.com/" + "a" * 120 + "'", +], ids=["closing tag", "list with an extra", "extra", "emoji code", "longer than 80 columns"]) +def test_text_is_written_as_it_is(text): + assert printed(text) == text + "\n" + + +def test_objects_are_written_as_their_text(): + assert printed("Update error:", ValueError("no section [/x]"), 3) == "Update error: no section [/x] 3\n" + + +def test_end_and_sep_are_kept(): + assert printed("a", "b", sep="-", end="") == "a-b" diff --git a/tests/test_install_body_types.py b/tests/test_install_body_types.py new file mode 100644 index 000000000..fd1590b85 --- /dev/null +++ b/tests/test_install_body_types.py @@ -0,0 +1,73 @@ +"""The two direct install routes answer 400 to a body they cannot use. + +Each case runs the real handler of `glob/manager_server.py` with the install +allowed; `core` is replaced and records what it is asked to install. +""" +import ast +import asyncio +import json +import logging +from types import SimpleNamespace + +import pytest +from aiohttp import web + +from manager_test_utils import GLOB_DIR + +GIT_URL, PIP = "install_custom_node_git_url", "install_custom_node_pip" + + +def post(name, body): + """The status the handler answers for the raw request body, and what it asked `core` to install.""" + source = (GLOB_DIR / "manager_server.py").read_text(encoding="utf-8") + handler = [node for node in ast.parse(source).body + if isinstance(node, ast.AsyncFunctionDef) and node.name == name] + assert len(handler) == 1 + asked = [] + + async def gitclone_install(url): + asked.append(url) + return SimpleNamespace(action="install-git", result=True, target=url, msg="") + + namespace = { + "web": web, "logging": logging, + "routes": SimpleNamespace(post=lambda path: lambda handler: handler), + "is_dedicated_install_allowed": lambda flag, listen: True, + "args": SimpleNamespace(listen="127.0.0.1"), + "core": SimpleNamespace(get_config=lambda: {"allow_git_url_install": True, "allow_pip_install": True}, + gitclone_install=gitclone_install, pip_install=asked.append), + } + exec(compile(ast.Module(body=handler, type_ignores=[]), "", "exec"), namespace) + + async def read(): + return json.loads(body) + + return asyncio.run(namespace[name](SimpleNamespace(json=read))).status, asked + + +NOT_AN_OBJECT = ["[]", '"text"', "7", "null"] +NOT_A_STRING = ["7", "null", '["a"]', '{"a": 1}'] + + +def test_git_url_is_installed(): + assert post(GIT_URL, '{"url": "https://github.com/a/b"}') == (200, ["https://github.com/a/b"]) + + +def test_packages_are_installed(): + assert post(PIP, '{"packages": "numpy scipy"}') == (200, [["numpy", "scipy"]]) + + +@pytest.mark.parametrize("name", [GIT_URL, PIP]) +@pytest.mark.parametrize("body", NOT_AN_OBJECT + ["{}", "not json"]) +def test_body_without_the_field_is_answered_with_400(name, body): + assert post(name, body) == (400, []) + + +@pytest.mark.parametrize("value", NOT_A_STRING) +def test_url_that_is_not_a_string_is_answered_with_400(value): + assert post(GIT_URL, '{"url": %s}' % value) == (400, []) + + +@pytest.mark.parametrize("value", NOT_A_STRING) +def test_packages_that_are_not_a_string_are_answered_with_400(value): + assert post(PIP, '{"packages": %s}' % value) == (400, []) diff --git a/tests/test_setting_values.py b/tests/test_setting_values.py new file mode 100644 index 000000000..3c351063a --- /dev/null +++ b/tests/test_setting_values.py @@ -0,0 +1,59 @@ +"""The server stores only a `db_mode`, an `update_policy` or a `component_policy` that the Manager knows. + +Each case runs the real handler of `glob/manager_server.py` with the functions +and the lists of values it uses; `core` holds a configuration in memory and +counts how often it is written. +""" +import ast +import asyncio +import json +from types import SimpleNamespace + +import pytest +from aiohttp import web + +from manager_test_utils import GLOB_DIR + +SETTINGS = [ + ("set_db_mode_handler", "db_mode", ["cache", "local", "remote"]), + ("set_update_policy_handler", "update_policy", ["stable-comfyui", "nightly-comfyui"]), + ("set_component_policy_handler", "component_policy", ["workflow", "higher", "mine"]), +] +NAMES = {handler for handler, key, values in SETTINGS} | {"set_db_mode", "set_update_policy", "set_component_policy"} +BEFORE = "the value before the request" + + +def post(handler, key, body): + """The status the handler answers, the stored value and how often the configuration was written.""" + source = (GLOB_DIR / "manager_server.py").read_text(encoding="utf-8") + nodes = [node for node in ast.parse(source).body + if (isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name in NAMES) + or (isinstance(node, ast.Assign) and getattr(node.targets[0], "id", "").startswith("ALLOWED_"))] + config, written = {key: BEFORE}, [] + namespace = { + "web": web, "routes": SimpleNamespace(post=lambda path: lambda function: function), + "core": SimpleNamespace(get_config=lambda: config, write_config=lambda: written.append(dict(config))), + } + exec(compile(ast.Module(body=nodes, type_ignores=[]), "", "exec"), namespace) + + async def read(): + return json.loads(body) + + return asyncio.run(namespace[handler](SimpleNamespace(json=read))).status, config[key], len(written) + + +@pytest.mark.parametrize("handler, key, value", [(handler, key, value) for handler, key, values in SETTINGS for value in values]) +def test_known_value_is_stored(handler, key, value): + assert post(handler, key, json.dumps({"value": value})) == (200, value, 1) + + +@pytest.mark.parametrize("handler, key, values", SETTINGS) +@pytest.mark.parametrize("value", ['"no-such-value"', '""', '"CACHE"', "7", "null", '["cache"]']) +def test_unknown_value_is_refused(handler, key, values, value): + assert post(handler, key, '{"value": %s}' % value) == (400, BEFORE, 0) + + +@pytest.mark.parametrize("handler, key, values", SETTINGS) +@pytest.mark.parametrize("body", ["{}", "[]", '"cache"', "7", "null"]) +def test_request_without_a_value_is_refused(handler, key, values, body): + assert post(handler, key, body) == (400, BEFORE, 0)