Files
Comfy-Org-ComfyUI-Manager/tests/test_setting_values.py
T
Dr.Lt.Data 480dc05d99 Keep sharing credentials on the server and tighten the security-level and POST checks
Seven changes, each in the CHANGELOG.

- Keep saved sharing credentials out of server answers, and log in to
  matrix.org only
- Hold a switch of ComfyUI to an older version to the security level
- Take a JSON body only as application/json
- Hold a snapshot restore to the security level of a switch to an older
  version
- Save only a choice of the share menu as the share option
- Write the saved Matrix account as a file of its user only
- Hold every POST that changes something to application/json, and save
  the share option with a POST

Reported by:
- gistrec
- sondt99
- dungNHVhust
- ascanner
- godzeo
- naroSEC
- Liam-G
- manus-pi
- eliteaja11-dev
2026-11-19 20:43:37 +09:00

61 lines
2.7 KiB
Python

"""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),
"_reject_non_json_content_type": lambda request: None,
"core": SimpleNamespace(get_config=lambda: config, write_config=lambda: written.append(dict(config))),
}
exec(compile(ast.Module(body=nodes, type_ignores=[]), "<setting handlers>", "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)