diff --git a/README.md b/README.md index 642d9041e..46625592b 100644 --- a/README.md +++ b/README.md @@ -483,7 +483,7 @@ Notes: - `comfy run --prompt ""` (no `--workflow`) runs a bundled default text2img graph. **That graph loads the SD1.5 checkpoint `v1-5-pruned-emaonly.ckpt`, which comfy-cli does not download for you** — install it into `models/checkpoints`, or point the graph at a checkpoint you do have with `comfy run --prompt "…" --set checkpoint=`. The same applies on cloud, where the checkpoint must exist in your cloud assets. - `comfy download` also reads a `prompt_id` from piped stdin, so `comfy run ... --where cloud | comfy download --where cloud` works. - Models and custom nodes must exist on the cloud side. `comfy models search --where cloud` lists the cloud asset catalog, and `comfy nodes ls --where cloud` lists the node classes cloud can run. -- Sign out with `comfy cloud logout`. If a run fails with `cloud_unauthorized`, your session expired — re-run `comfy cloud login`. +- Sign out with `comfy cloud logout`. If a run fails with `cloud_unauthorized`, your session expired — re-run `comfy cloud login`. Re-login is also required after Comfy Cloud adds permissions: a session from before the change fails with a `cloud_unauthorized` that says it predates a permission change, and refreshing cannot fix it. ### Calling partner nodes (`comfy generate`) diff --git a/comfy_cli/cloud/__init__.py b/comfy_cli/cloud/__init__.py index 6eb5e20a4..65ab99804 100644 --- a/comfy_cli/cloud/__init__.py +++ b/comfy_cli/cloud/__init__.py @@ -71,6 +71,11 @@ def get_base_url() -> str: "comfy-cloud:settings:read", "comfy-cloud:settings:write", "comfy-cloud:billing:read", + # Append only, never reorder: the joined string is what the authorize URL + # sends. A refresh cannot widen a grant (RFC 6749 §6), so a session minted + # before a scope was added here needs a fresh `comfy cloud login`. + "comfy-cloud:secrets:write", + "comfy-cloud:agent:write", ) CONFIG_KEY_RESOURCE_URL = "cloud_resource_url" diff --git a/comfy_cli/cloud/command.py b/comfy_cli/cloud/command.py index b29734a59..95bf68029 100644 --- a/comfy_cli/cloud/command.py +++ b/comfy_cli/cloud/command.py @@ -431,6 +431,15 @@ def status_cmd( status_body = _billing_get(status_url, target) except urllib.error.HTTPError as e: if e.code in (401, 403): + from comfy_cli.command._cloud_errors import insufficient_scope_error, read_unauthorized_body + + scope_body, _ = read_unauthorized_body(e) + scope_error = insufficient_scope_error( + e.code, scope_body, getattr(e, "headers", None), details={"url": status_url} + ) + if scope_error is not None: + renderer.error(**scope_error) + raise typer.Exit(code=1) from e renderer.error( code="cloud_unauthorized", message=f"Comfy Cloud rejected the credential (HTTP {e.code}).", diff --git a/comfy_cli/comfy_client.py b/comfy_cli/comfy_client.py index a5b9a2a76..0af5bac56 100644 --- a/comfy_cli/comfy_client.py +++ b/comfy_cli/comfy_client.py @@ -101,15 +101,36 @@ def _parse_retry_after(headers: Any) -> float | None: return float(max(0, round((when - datetime.now(timezone.utc)).total_seconds()))) +def _www_authenticate(headers: Any) -> tuple[str, ...]: + """Every ``WWW-Authenticate`` value: a server may send one field per challenge.""" + if headers is None: + return () + try: + get_all = getattr(headers, "get_all", None) + values = get_all("WWW-Authenticate") if callable(get_all) else [headers.get("WWW-Authenticate")] + except Exception: # noqa: BLE001 + return () + return tuple(v for v in (values or []) if isinstance(v, str) and v) + + class HTTPError(Exception): """Server returned a non-2xx response. ``retry_after`` carries the server's ``Retry-After`` header (seconds) when one was present, so retry layers above ``_request`` (e.g. the - wait_for_completion poll loop) can honor it. + wait_for_completion poll loop) can honor it. ``www_authenticate`` carries + every ``WWW-Authenticate`` value, which names a missing OAuth scope. """ - def __init__(self, status: int, message: str, body: str = "", *, retry_after: float | None = None): + def __init__( + self, + status: int, + message: str, + body: str = "", + *, + retry_after: float | None = None, + www_authenticate: tuple[str, ...] = (), + ): # Redact before super().__init__ so str(self) is safe to log. message = _redact(message) body = _redact(body) @@ -118,6 +139,7 @@ def __init__(self, status: int, message: str, body: str = "", *, retry_after: fl self.message = message self.body = body self.retry_after = retry_after + self.www_authenticate = www_authenticate class Unauthenticated(Exception): @@ -340,6 +362,7 @@ def _request( e.reason or "HTTP error", body_text, retry_after=_parse_retry_after(getattr(e, "headers", None)), + www_authenticate=_www_authenticate(getattr(e, "headers", None)), ) from e # ----- submit ----- diff --git a/comfy_cli/command/_cloud_errors.py b/comfy_cli/command/_cloud_errors.py index da9d337a7..32540a94f 100644 --- a/comfy_cli/command/_cloud_errors.py +++ b/comfy_cli/command/_cloud_errors.py @@ -21,6 +21,7 @@ from __future__ import annotations import json +import re import urllib.error import typer @@ -29,6 +30,10 @@ # env-configurable, so a hostile or misbehaving endpoint must not be able to # OOM the CLI with an unbounded error response. _MAX_ERROR_BODY_BYTES = 1000 +# Cap on the 403 body read for ``insufficient_scope`` detection. A JSON body +# cut at ``_MAX_ERROR_BODY_BYTES`` no longer parses, so detection reads more +# than ``details.body`` shows — but still a bounded amount. +_MAX_SCOPE_BODY_BYTES = 64 * 1024 # A 401 is unambiguously an authentication failure. A 403 is not — it is also # how the server denies a forbidden resource, a quota, or an already-finished @@ -40,6 +45,134 @@ } +_INSUFFICIENT_SCOPE_MESSAGE = ( + "Your Comfy Cloud login predates a permission change; run `comfy cloud login` to re-authorize" +) +# One piece of a WWW-Authenticate field (RFC 7235 §2.1): an auth-param +# ``name = "quoted"`` / ``name = token``, or else a bare token, which starts a +# new challenge. Quoted values are consumed whole, so the scan never reads +# inside one such as ``error_description``. +_CHALLENGE_PART = re.compile( + r"([!#$%&'*+\-.^_`|~0-9A-Za-z]+)" + r'(?P\s*=\s*(?:"(?P(?:[^"\\]|\\.)*)"|(?P[^\s,"]*)))?' +) +# The plain-text body the scope middleware writes: ``insufficient_scope`` or +# ``insufficient_scope: `` and nothing else. Anchored at both ends so a +# body that merely mentions the token in prose is not mistaken for one. +_BODY_SCOPE = re.compile(r"\s*insufficient_scope(?:\s*:\s*(?P[^\r\n]*?))?\s*\Z") +# Structured fields a JSON error body may carry the error in. +_BODY_SCOPE_FIELDS = ("message", "error", "code", "type") +# ``required_scope`` comes from the server, and ``base_url`` is configurable, +# so bound it and keep only RFC 6749 §3.3 scope-token characters. +_SCOPE_TOKEN = re.compile(r"[\x21\x23-\x5B\x5D-\x7E]+") +_MAX_REQUIRED_SCOPE_CHARS = 200 + + +def _www_authenticate_values(headers) -> list[str]: + """Every ``WWW-Authenticate`` value as ``str``; ``[]`` on any odd headers object. + + ``headers`` may also be the values themselves (``HTTPError.www_authenticate``). + """ + if headers is None: + return [] + if isinstance(headers, (list, tuple)): + return [v for v in headers if isinstance(v, str) and v] + try: + get_all = getattr(headers, "get_all", None) + values = get_all("WWW-Authenticate") if callable(get_all) else [headers.get("WWW-Authenticate")] + except Exception: # noqa: BLE001 + return [] + return [v for v in (values or []) if isinstance(v, str) and v] + + +def _challenges(value: str) -> list[tuple[str, dict[str, str]]]: + """Split one ``WWW-Authenticate`` value into ``(scheme, params)`` challenges.""" + challenges: list[tuple[str, dict[str, str]]] = [] + for m in _CHALLENGE_PART.finditer(value): + if m.group("param") is None: + challenges.append((m.group(1).lower(), {})) + elif challenges: + raw = m.group("quoted") + param = re.sub(r"\\(.)", r"\1", raw) if raw is not None else m.group("token") + challenges[-1][1].setdefault(m.group(1).lower(), param) + return challenges + + +def _bearer_scope_challenge(values: list[str]) -> tuple[bool, str | None]: + """Whether a Bearer challenge says ``error=insufficient_scope``, and the ``scope`` that challenge names.""" + for value in values: + for scheme, params in _challenges(value): + if scheme == "bearer" and params.get("error") == "insufficient_scope": + return True, params.get("scope") + return False, None + + +def _body_scope_error(body: str) -> tuple[bool, str | None]: + """Whether the body is an ``insufficient_scope`` error, and the scope it names. + + A JSON body is judged by its structured fields only; a plain body must be + exactly the documented ``insufficient_scope[: ]`` form. + """ + candidates = [body] + try: + parsed = json.loads(body) + except ValueError: + parsed = None + if isinstance(parsed, dict): + candidates = [parsed[k] for k in _BODY_SCOPE_FIELDS if isinstance(parsed.get(k), str)] + nested = parsed.get("error") + if isinstance(nested, dict): + candidates += [nested[k] for k in _BODY_SCOPE_FIELDS if isinstance(nested.get(k), str)] + elif parsed is not None: + candidates = [parsed] if isinstance(parsed, str) else [] + for text in candidates: + m = _BODY_SCOPE.match(text) + if m: + return True, m.group("scope") + return False, None + + +def _clean_scope(scope: str | None) -> str | None: + if not scope: + return None + tokens = _SCOPE_TOKEN.findall(scope) + if not tokens or " ".join(tokens) != " ".join(scope.split()): + return None + cleaned = " ".join(tokens) + return cleaned if len(cleaned) <= _MAX_REQUIRED_SCOPE_CHARS else None + + +def insufficient_scope_error(status: int, body: str | None, headers=None, details: dict | None = None) -> dict | None: + """The envelope fields for a 403 ``insufficient_scope``, or ``None`` for any other response. + + Comfy Cloud answers a token whose grant lacks a route's scope with + ``403`` + ``WWW-Authenticate: Bearer error="insufficient_scope", scope="…"`` + (RFC 6750 §3.1) and an ``insufficient_scope: `` body. Refreshing + cannot widen a grant, so the only fix is a new authorize flow — the + envelope says so instead of the generic 403 hint. It never opens a + browser: the caller may be non-interactive. + """ + if status != 403: + return None + header_hit, header_scope = _bearer_scope_challenge(_www_authenticate_values(headers)) + body_hit, body_scope = _body_scope_error(body if isinstance(body, str) else "") + if not header_hit and not body_hit: + return None + out_details = {**(details or {}), "status": 403, "reason": "insufficient_scope"} + required_scope = _clean_scope(header_scope) or _clean_scope(body_scope) + if required_scope: + out_details["required_scope"] = required_scope + return { + "code": "cloud_unauthorized", + "message": _INSUFFICIENT_SCOPE_MESSAGE, + "hint": ( + "run `comfy cloud login` to re-authorize; refreshing the existing session cannot add the permission." + " If you set COMFY_CLOUD_SCOPES or the `cloud_scopes` config, add the required scope to it first" + ), + "details": out_details, + } + + def _read_error_body(e: urllib.error.HTTPError) -> str: """Best-effort read of a bounded slice of the server's error body. @@ -53,6 +186,21 @@ def _read_error_body(e: urllib.error.HTTPError) -> str: return "" +def read_unauthorized_body(e: urllib.error.HTTPError) -> tuple[str, str]: + """``(detection_body, display_body)`` for a 401/403, from one bounded read. + + Scope detection gets up to ``_MAX_SCOPE_BODY_BYTES`` so a structured + ``insufficient_scope`` field past the display cap is still seen; only the + ``details.body`` slice is capped at ``_MAX_ERROR_BODY_BYTES``. Read + failures degrade to empty bodies, as in ``_read_error_body``. + """ + try: + raw = e.read(_MAX_SCOPE_BODY_BYTES) or b"" + except Exception: + raw = b"" + return raw.decode("utf-8", "replace"), raw[:_MAX_ERROR_BODY_BYTES].decode("utf-8", "replace") + + def retry_after_from_headers(headers) -> float | None: """The server's ``Retry-After`` (delta-seconds) from a urllib error's headers.""" from comfy_cli.comfy_client import _parse_retry_after @@ -144,6 +292,8 @@ def emit_status_error( hint: str | None, details: dict, rate_limited_next_step: str = _DEFAULT_RATE_LIMITED_NEXT_STEP, + scope_body: str | None = None, + www_authenticate: tuple[str, ...] | str | None = None, ) -> None: """Emit the envelope for a cloud HTTP status that has no caller-specific code. @@ -160,7 +310,18 @@ def emit_status_error( ``rate_limited_next_step`` finishes the 429 hint for a caller that knows more (``run``'s submit: check the job list before re-running; its poll: the job exists, so follow it rather than re-running). + ``scope_body`` / ``www_authenticate`` let a caller whose ``details`` omit + the body (``run``'s poll) still have a 403 ``insufficient_scope`` detected. """ + scope_error = insufficient_scope_error( + status, + scope_body if scope_body is not None else details.get("body"), + (www_authenticate,) if isinstance(www_authenticate, str) else www_authenticate, + details=details, + ) + if scope_error is not None: + renderer.error(**scope_error) + return if status == 402: renderer.error(**payment_required_error(operation, 402, *_typed_error(details), details)) return @@ -241,12 +402,18 @@ def handle_cloud_http_error( details={**id_detail, "operation": operation, **(not_found_details or {})}, ) elif e.code in (401, 403): - renderer.error( - code="cloud_unauthorized", - message=f"HTTP {e.code} during {operation}", - hint=_UNAUTHORIZED_HINTS[e.code], - details={"status": e.code, "body": _read_error_body(e), "operation": operation, **id_detail}, - ) + scope_body, body = read_unauthorized_body(e) + details = {"status": e.code, "body": body, "operation": operation, **id_detail} + scope_error = insufficient_scope_error(e.code, scope_body, getattr(e, "headers", None), details=details) + if scope_error is not None: + renderer.error(**scope_error) + else: + renderer.error( + code="cloud_unauthorized", + message=f"HTTP {e.code} during {operation}", + hint=_UNAUTHORIZED_HINTS[e.code], + details=details, + ) else: emit_status_error( renderer, diff --git a/comfy_cli/command/cloud_http.py b/comfy_cli/command/cloud_http.py index 353c5f899..fb725864f 100644 --- a/comfy_cli/command/cloud_http.py +++ b/comfy_cli/command/cloud_http.py @@ -135,12 +135,19 @@ def handle_cloud_http_error(renderer, e, *, operation: str, workflow_id: str | N import urllib.error if isinstance(e, urllib.error.HTTPError): - # Best effort: a server that drops the connection partway through its - # error body must not keep the status from being classified. - try: - body = (e.read() or b"")[:1000].decode("utf-8", "replace") - except (http.client.HTTPException, OSError): - body = "" + if e.code in (401, 403): + from comfy_cli.command._cloud_errors import read_unauthorized_body + + # Detection sees more of the body than the capped `details.body`. + # read_unauthorized_body already degrades a dropped connection to empty bodies. + scope_body, body = read_unauthorized_body(e) + else: + # Best effort: a server that drops the connection partway through its + # error body must not keep the status from being classified. + try: + body = (e.read() or b"")[:1000].decode("utf-8", "replace") + except (http.client.HTTPException, OSError): + body = "" if e.code == 404: renderer.error( code="workflow_not_found", @@ -151,12 +158,20 @@ def handle_cloud_http_error(renderer, e, *, operation: str, workflow_id: str | N details={"workflow_id": workflow_id, "operation": operation}, ) elif e.code in (401, 403): - renderer.error( - code="cloud_unauthorized", - message=f"HTTP {e.code} during {operation}", - hint="re-run `comfy cloud login`", - details={"status": e.code}, + from comfy_cli.command._cloud_errors import insufficient_scope_error + + scope_error = insufficient_scope_error( + e.code, scope_body, getattr(e, "headers", None), details={"operation": operation, "body": body} ) + if scope_error is not None: + renderer.error(**scope_error) + else: + renderer.error( + code="cloud_unauthorized", + message=f"HTTP {e.code} during {operation}", + hint="re-run `comfy cloud login`", + details={"status": e.code}, + ) else: from comfy_cli.command._cloud_errors import emit_status_error, retry_after_from_headers diff --git a/comfy_cli/command/run/__init__.py b/comfy_cli/command/run/__init__.py index 872515f51..d921c351f 100644 --- a/comfy_cli/command/run/__init__.py +++ b/comfy_cli/command/run/__init__.py @@ -1223,7 +1223,7 @@ def execute_cloud( (the ``comfy run --prompt`` injected default), mirroring :func:`execute`. """ from comfy_cli.comfy_client import Client, HTTPError, Unauthenticated, _group_outputs - from comfy_cli.command._cloud_errors import emit_status_error + from comfy_cli.command._cloud_errors import emit_status_error, insufficient_scope_error from comfy_cli.target import resolve_target renderer = get_renderer() @@ -1409,6 +1409,9 @@ def execute_cloud( message=f"Cloud server rejected the workflow (HTTP {e.status}): {e.message}", hint="check the workflow is valid and the cloud server has the required nodes", details={"status": e.status, "body": e.body[:2000]}, + # Detection reads the whole body; only the rendered detail is capped. + scope_body=e.body, + www_authenticate=getattr(e, "www_authenticate", None), # A 429 alone does not prove the submit had no effect, and the # client never repeats a submit on its own. Look for the job before # re-running so one that did get through is not queued twice. @@ -1591,7 +1594,14 @@ def _probe(): ) else: state.status = "error" - state.error = {"code": "cloud_http_error", "message": str(e)} + # Record the same re-login verdict ``emit_status_error`` renders + # below, so a later `comfy jobs status` keeps the reason. + state.error = insufficient_scope_error( + e.status, + e.body, + getattr(e, "www_authenticate", None), + details={"status": e.status, "prompt_id": submit.prompt_id}, + ) or {"code": "cloud_http_error", "message": str(e)} jobs_state.write(state) emit_status_error( renderer, @@ -1602,6 +1612,8 @@ def _probe(): hint=None, details={"status": e.status, "prompt_id": submit.prompt_id}, rate_limited_next_step=follow_up, + scope_body=e.body, + www_authenticate=getattr(e, "www_authenticate", None), ) raise typer.Exit(code=1) from e except KeyboardInterrupt: diff --git a/docs/json-output.md b/docs/json-output.md index 675fa26b0..60320b248 100644 --- a/docs/json-output.md +++ b/docs/json-output.md @@ -235,7 +235,7 @@ the `--print-prompt` and `--no-wait` stream shapes. | `code` | Triggered when | `details` | Exit | | -------------------- | ------------------------------------------------------------------ | -------------------------------- | ---- | -| `cloud_unauthorized` | No usable cloud session, or the session was rejected — run `comfy cloud login` | — | 1 | +| `cloud_unauthorized` | No usable cloud session, or the session was rejected — run `comfy cloud login`. A `403` with `details.reason: "insufficient_scope"` means the login predates a permission Comfy Cloud added; refreshing cannot widen it, so run `comfy cloud login` again to re-authorize | `reason`, `required_scope` (when the server named it) on an insufficient-scope 403; otherwise varies | 1 | | `cloud_http_error` | The cloud API returned a non-2xx response on submit or while polling | `status` (int), `body` (str) on submit; `status`, `prompt_id` while polling | 1 | | `cloud_rate_limited` | The cloud API throttled the request (HTTP 429) on submit or while polling. Throttling is not a verdict on the workflow, but it does not by itself prove the submit had no effect, and the CLI never repeats a submit on its own. On submit, wait, then check `comfy jobs ls --where cloud` for the job before re-running; while polling, the job was already submitted, so follow it with `comfy jobs watch --where cloud` instead of re-running (the job record is left non-terminal). Cloud only: a local server's 429 is `client_error` | same as `cloud_http_error`, plus `retry_after` (seconds) when the server sent `Retry-After` | 1 | | `cloud_payment_required` | The cloud refused the request because the account's plan does not allow it (HTTP 402, or a legacy HTTP 429 whose body type is a plan refusal: free generations used up, subscription required, a partner node or model that needs a paid plan). Not throttling and not a workflow problem; nothing was queued, so do not retry or edit the workflow. A full queue stays `cloud_rate_limited` | `status` (402 or 429), `body` (str), `reason` (the server's refusal type, when it sent one) | 1 | diff --git a/tests/comfy_cli/auth/test_oauth.py b/tests/comfy_cli/auth/test_oauth.py index bfdb821fb..25d321551 100644 --- a/tests/comfy_cli/auth/test_oauth.py +++ b/tests/comfy_cli/auth/test_oauth.py @@ -78,6 +78,45 @@ def test_build_authorize_url_includes_every_required_param(): # Callback server — happy path + bad state + error param # --------------------------------------------------------------------------- +_PRE_SECRETS_AGENT_SCOPES = ( + "comfy-cloud:workflows:read comfy-cloud:workflows:write comfy-cloud:jobs:read comfy-cloud:jobs:write " + "comfy-cloud:files:read comfy-cloud:files:write comfy-cloud:assets:read comfy-cloud:assets:write " + "comfy-cloud:hub:read comfy-cloud:hub:write comfy-cloud:user:read comfy-cloud:settings:read " + "comfy-cloud:settings:write comfy-cloud:billing:read" +) + + +def test_default_scopes_append_secrets_and_agent_write(): + """The secrets/agent mutations are gated on these two scopes; they are + appended so the existing prefix of the joined string never reorders.""" + from comfy_cli.cloud import _DEFAULT_SCOPES + + assert " ".join(_DEFAULT_SCOPES) == ( + _PRE_SECRETS_AGENT_SCOPES + " comfy-cloud:secrets:write comfy-cloud:agent:write" + ) + + +def test_login_authorize_url_requests_default_scopes(monkeypatch: pytest.MonkeyPatch): + """With no env/config override, the authorize URL asks for the full default list.""" + from types import SimpleNamespace + + from comfy_cli import cloud, config_manager + + monkeypatch.delenv("COMFY_CLOUD_SCOPES", raising=False) + monkeypatch.setattr(config_manager, "ConfigManager", lambda: SimpleNamespace(get=lambda key: None)) + url = oauth._build_authorize_url( + base_url="https://testcloud.comfy.org", + client_id="comfy-cli", + redirect_uri="http://127.0.0.1:51234/callback", + scopes=cloud.get_scopes(), + state="STATE", + challenge="C" * 43, + resource="https://testcloud.comfy.org/api", + ) + scope = urllib.parse.parse_qs(urllib.parse.urlsplit(url).query)["scope"][0].split() + assert "comfy-cloud:secrets:write" in scope + assert "comfy-cloud:agent:write" in scope + def _drive_callback(*, expected_state: str, query: str) -> oauth._CallbackCapture: capture = oauth._CallbackCapture() @@ -524,6 +563,17 @@ def test_refreshes_expired_session_with_refresh_token(self, monkeypatch): assert result.is_expired() is False assert saved["access_token"] == "NEW" + def test_refresh_does_not_send_scope(self, monkeypatch): + """A refresh cannot widen a grant (RFC 6749 §6), so asking for the + current default list on refresh would only risk an invalid_scope.""" + seen: dict = {} + monkeypatch.setattr(auth_store, "get_cloud_session", lambda: self._expired()) + monkeypatch.setattr(auth_store, "save_cloud_session", lambda **kw: None) + monkeypatch.setattr(oauth, "_post_form", lambda url, body, **_kw: seen.update(body) or {"access_token": "NEW"}) + oauth.ensure_fresh_session() + assert seen["grant_type"] == "refresh_token" + assert "scope" not in seen + def test_no_refresh_token_returns_stale_without_calling_refresh(self, monkeypatch): called = [] monkeypatch.setattr(auth_store, "get_cloud_session", lambda: self._expired(refresh=None)) diff --git a/tests/comfy_cli/command/test_cloud_errors.py b/tests/comfy_cli/command/test_cloud_errors.py index d3e38a8f7..4284725c0 100644 --- a/tests/comfy_cli/command/test_cloud_errors.py +++ b/tests/comfy_cli/command/test_cloud_errors.py @@ -345,3 +345,329 @@ def test_registry_lists_cloud_payment_required(): entry = by_code["cloud_payment_required"] assert "429" in entry.meaning assert "retry" in entry.hint.lower() + + +# --- 403 insufficient_scope → re-login guidance ------------------------------ + +_SCOPE_HEADERS = {"WWW-Authenticate": 'Bearer error="insufficient_scope", scope="comfy-cloud:secrets:write"'} +_SCOPE_BODY = b'{"message":"insufficient_scope: comfy-cloud:secrets:write"}' + + +def test_403_insufficient_scope_header_says_relogin(): + """The enforced scope gate answers RFC 6750 insufficient_scope; refresh + cannot widen a grant, so the envelope must point at a fresh login.""" + renderer = _FakeRenderer() + e = urllib.error.HTTPError("https://x/api/secrets", 403, "Forbidden", _SCOPE_HEADERS, io.BytesIO(_SCOPE_BODY)) + + exit_exc = _handle(renderer, e) + + assert exit_exc.exit_code == 1 + (call,) = renderer.calls + assert call["code"] == "cloud_unauthorized" + assert "predates a permission change" in call["message"] + assert "comfy cloud login" in call["message"] + assert call["details"]["reason"] == "insufficient_scope" + assert call["details"]["required_scope"] == "comfy-cloud:secrets:write" + assert call["details"]["operation"] == "cancel" + + +def test_403_insufficient_scope_body_only_still_detected(): + renderer = _FakeRenderer() + _handle(renderer, _http_error(403, _SCOPE_BODY)) + + (call,) = renderer.calls + assert call["details"]["reason"] == "insufficient_scope" + # The documented body form names the scope too. + assert call["details"]["required_scope"] == "comfy-cloud:secrets:write" + + +@pytest.mark.parametrize( + "body", + [ + b"insufficient_scope: comfy-cloud:agent:write", + b"insufficient_scope", + b'{"error":"insufficient_scope"}', + b'{"error":{"code":"insufficient_scope: comfy-cloud:agent:write"}}', + ], +) +def test_403_documented_body_forms_are_detected(body: bytes): + renderer = _FakeRenderer() + _handle(renderer, _http_error(403, body)) + + (call,) = renderer.calls + assert call["details"]["reason"] == "insufficient_scope" + + +@pytest.mark.parametrize( + "body", + [ + b'{"message":"forbidden: this is not an insufficient_scope problem, the workspace is suspended"}', + b"Access denied. (Hint: insufficient_scope errors are reported separately.)", + b'{"message":"validation failed","details":"node text: insufficient_scope"}', + ], +) +def test_403_body_merely_mentioning_token_keeps_generic_hint(body: bytes): + """A body that only mentions the token in prose, or in a non-error field + (e.g. reflected workflow text), is not an insufficient_scope denial.""" + renderer = _FakeRenderer() + _handle(renderer, _http_error(403, body)) + + (call,) = renderer.calls + assert call["message"] == "HTTP 403 during cancel" + assert "reason" not in call["details"] + + +@pytest.mark.parametrize( + "www_auth", + [ + 'Bearer error = "insufficient_scope", scope="comfy-cloud:secrets:write"', + "Bearer error=insufficient_scope, scope=comfy-cloud:secrets:write", + 'Bearer realm="api", error_description="scope=\\"wrong\\"", error="insufficient_scope", ' + 'scope="comfy-cloud:secrets:write"', + ], +) +def test_403_header_challenge_parsing(www_auth: str): + renderer = _FakeRenderer() + e = urllib.error.HTTPError("https://x/y", 403, "Forbidden", {"WWW-Authenticate": www_auth}, io.BytesIO(b"denied")) + + _handle(renderer, e) + + (call,) = renderer.calls + assert call["details"]["reason"] == "insufficient_scope" + assert call["details"]["required_scope"] == "comfy-cloud:secrets:write" + + +def test_403_scope_in_second_challenge_is_found(): + import email.message + + headers = email.message.Message() + headers["WWW-Authenticate"] = 'Basic realm="x"' + headers["WWW-Authenticate"] = 'Bearer error="insufficient_scope", scope="comfy-cloud:agent:write"' + renderer = _FakeRenderer() + _handle(renderer, urllib.error.HTTPError("https://x/y", 403, "Forbidden", headers, io.BytesIO(b""))) + + (call,) = renderer.calls + assert call["details"]["required_scope"] == "comfy-cloud:agent:write" + + +def test_403_non_string_header_value_does_not_crash(): + class _OddHeaders: + def get(self, name): + return 42 + + renderer = _FakeRenderer() + e = urllib.error.HTTPError("https://x/y", 403, "Forbidden", _OddHeaders(), io.BytesIO(b'{"message":"forbidden"}')) + + _handle(renderer, e) + + (call,) = renderer.calls + assert call["message"] == "HTTP 403 during cancel" + + +@pytest.mark.parametrize("scope", ["x" * 5000, "comfy-cloud:secrets:write\x1b[31m"]) +def test_required_scope_is_bounded_and_sanitized(scope: str): + renderer = _FakeRenderer() + headers = {"WWW-Authenticate": f'Bearer error="insufficient_scope", scope="{scope}"'} + _handle(renderer, urllib.error.HTTPError("https://x/y", 403, "Forbidden", headers, io.BytesIO(b""))) + + (call,) = renderer.calls + assert call["details"]["reason"] == "insufficient_scope" + assert "required_scope" not in call["details"] + + +def test_insufficient_scope_hint_mentions_scope_override(): + """A COMFY_CLOUD_SCOPES / cloud_scopes override is sent verbatim, so a + re-login alone would request the same narrow grant.""" + renderer = _FakeRenderer() + _handle(renderer, _http_error(403, _SCOPE_BODY)) + + hint = renderer.calls[0]["hint"] + assert "COMFY_CLOUD_SCOPES" in hint + assert "cloud_scopes" in hint + + +def test_plain_403_keeps_generic_hint(): + renderer = _FakeRenderer() + _handle(renderer, _http_error(403, b'{"message":"forbidden"}')) + + (call,) = renderer.calls + assert call["message"] == "HTTP 403 during cancel" + assert "reason" not in call["details"] + + +def test_401_mentioning_insufficient_scope_is_not_a_scope_error(): + renderer = _FakeRenderer() + _handle(renderer, _http_error(401, _SCOPE_BODY)) + + (call,) = renderer.calls + assert call["message"] == "HTTP 401 during cancel" + + +def test_emit_status_error_maps_insufficient_scope(): + """``run`` submit routes its HTTPError through ``emit_status_error``.""" + from comfy_cli.command._cloud_errors import emit_status_error + + renderer = _FakeRenderer() + emit_status_error( + renderer, + status=403, + retry_after=None, + operation="submit", + message="Cloud server rejected the workflow (HTTP 403)", + hint="check the workflow", + details={"status": 403, "body": _SCOPE_BODY.decode()}, + ) + + (call,) = renderer.calls + assert call["code"] == "cloud_unauthorized" + assert call["details"]["reason"] == "insufficient_scope" + + +def test_emit_status_error_poll_detects_scope_without_body_in_details(): + """``run``'s poll keeps ``details`` to status/prompt_id, so it passes the + body and ``WWW-Authenticate`` separately for detection.""" + from comfy_cli.command._cloud_errors import emit_status_error + + renderer = _FakeRenderer() + emit_status_error( + renderer, + status=403, + retry_after=None, + operation="poll", + message="Cloud server error while polling (HTTP 403)", + hint=None, + details={"status": 403, "prompt_id": "p1"}, + scope_body="denied", + www_authenticate='Bearer error="insufficient_scope", scope="comfy-cloud:jobs:read"', + ) + + (call,) = renderer.calls + assert call["code"] == "cloud_unauthorized" + assert call["details"]["prompt_id"] == "p1" + assert call["details"]["required_scope"] == "comfy-cloud:jobs:read" + assert "body" not in call["details"] + + +def test_comfy_client_http_error_carries_www_authenticate(): + from unittest import mock + + from comfy_cli.comfy_client import Client, HTTPError + from comfy_cli.target import Target + + client = Client(Target(kind="local", base_url="https://cloud.example.com")) + err = urllib.error.HTTPError( + "https://cloud.example.com/api/x", 403, "Forbidden", _SCOPE_HEADERS, io.BytesIO(_SCOPE_BODY) + ) + with mock.patch("comfy_cli.comfy_client._OPENER.open", side_effect=err): + with pytest.raises(HTTPError) as excinfo: + client._request("POST", ["api", "prompt"], body={}) + + assert excinfo.value.www_authenticate == (_SCOPE_HEADERS["WWW-Authenticate"],) + + +def test_comfy_client_http_error_keeps_every_www_authenticate_value(): + """A Bearer challenge in a later header field must still reach detection.""" + import email.message + from unittest import mock + + from comfy_cli.comfy_client import Client, HTTPError + from comfy_cli.target import Target + + headers = email.message.Message() + headers["WWW-Authenticate"] = 'Basic realm="x"' + headers["WWW-Authenticate"] = 'Bearer error="insufficient_scope", scope="comfy-cloud:agent:write"' + client = Client(Target(kind="local", base_url="https://cloud.example.com")) + err = urllib.error.HTTPError("https://cloud.example.com/api/x", 403, "Forbidden", headers, io.BytesIO(b"denied")) + with mock.patch("comfy_cli.comfy_client._OPENER.open", side_effect=err): + with pytest.raises(HTTPError) as excinfo: + client._request("POST", ["api", "prompt"], body={}) + + assert excinfo.value.www_authenticate == ( + 'Basic realm="x"', + 'Bearer error="insufficient_scope", scope="comfy-cloud:agent:write"', + ) + from comfy_cli.command._cloud_errors import insufficient_scope_error + + found = insufficient_scope_error(403, excinfo.value.body, excinfo.value.www_authenticate) + assert found["details"]["required_scope"] == "comfy-cloud:agent:write" + + +@pytest.mark.parametrize( + "www_auth, required_scope", + [ + # The scope comes from the challenge that carries the error, not an earlier one. + ('Bearer realm="a", scope="old", Bearer error="insufficient_scope", scope="new"', "new"), + ('Basic realm="x", Bearer error=insufficient_scope, scope=comfy-cloud:agent:write', "comfy-cloud:agent:write"), + ], +) +def test_403_scope_is_read_from_the_matching_bearer_challenge(www_auth: str, required_scope: str): + from comfy_cli.command._cloud_errors import insufficient_scope_error + + found = insufficient_scope_error(403, "", {"WWW-Authenticate": www_auth}) + + assert found["details"]["required_scope"] == required_scope + + +def test_403_insufficient_scope_on_a_non_bearer_challenge_is_ignored(): + from comfy_cli.command._cloud_errors import insufficient_scope_error + + headers = {"WWW-Authenticate": 'Basic error="insufficient_scope", scope="comfy-cloud:agent:write"'} + + assert insufficient_scope_error(403, '{"message":"forbidden"}', headers) is None + + +def test_cloud_http_handler_maps_insufficient_scope(): + from comfy_cli.command.cloud_http import handle_cloud_http_error as workflow_handler + + renderer = _FakeRenderer() + e = urllib.error.HTTPError("https://x/api/x", 403, "Forbidden", _SCOPE_HEADERS, io.BytesIO(_SCOPE_BODY)) + + exit_exc = workflow_handler(renderer, e, operation="save") + + assert exit_exc.exit_code == 1 + (call,) = renderer.calls + assert call["code"] == "cloud_unauthorized" + assert call["details"]["required_scope"] == "comfy-cloud:secrets:write" + # The server's own explanation stays inspectable. + assert call["details"]["body"] == _SCOPE_BODY.decode() + + +# A JSON body whose `insufficient_scope` field sits past the 1000-byte display cap. +_LONG_SCOPE_BODY = ( + b'{"detail":"' + b"x" * (2 * _MAX_ERROR_BODY_BYTES) + b'","error":"insufficient_scope: comfy-cloud:agent:write"}' +) + + +def test_403_scope_field_past_display_cap_is_detected(): + renderer = _FakeRenderer() + _handle(renderer, _http_error(403, _LONG_SCOPE_BODY)) + + (call,) = renderer.calls + assert call["details"]["reason"] == "insufficient_scope" + assert call["details"]["required_scope"] == "comfy-cloud:agent:write" + # Only the rendered body is capped. + assert call["details"]["body"] == _LONG_SCOPE_BODY[:_MAX_ERROR_BODY_BYTES].decode() + + +def test_cloud_http_handler_detects_scope_field_past_display_cap(): + from comfy_cli.command.cloud_http import handle_cloud_http_error as workflow_handler + + renderer = _FakeRenderer() + e = urllib.error.HTTPError("https://x/api/x", 403, "Forbidden", {}, io.BytesIO(_LONG_SCOPE_BODY)) + + workflow_handler(renderer, e, operation="save") + + (call,) = renderer.calls + assert call["details"]["required_scope"] == "comfy-cloud:agent:write" + assert call["details"]["body"] == _LONG_SCOPE_BODY[:_MAX_ERROR_BODY_BYTES].decode() + + +def test_unauthorized_body_read_is_bounded_for_detection(): + from comfy_cli.command._cloud_errors import _MAX_SCOPE_BODY_BYTES, read_unauthorized_body + + e = _http_error(403, b"A" * (5 * 1024 * 1024)) + + scope_body, body = read_unauthorized_body(e) + + assert len(scope_body) == _MAX_SCOPE_BODY_BYTES + assert body == "A" * _MAX_ERROR_BODY_BYTES diff --git a/tests/comfy_cli/command/test_run_json.py b/tests/comfy_cli/command/test_run_json.py index f32bdaf15..0b8ab3154 100644 --- a/tests/comfy_cli/command/test_run_json.py +++ b/tests/comfy_cli/command/test_run_json.py @@ -2780,6 +2780,21 @@ def test_submit_402_is_cloud_payment_required(self, monkeypatch, workflow_file, assert err["details"]["status"] == 402 assert "comfy jobs ls" not in err["hint"] + def test_submit_403_scope_detected_past_the_rendered_body_cap(self, monkeypatch, workflow_file, capsys): + """Detection reads the full body; only ``details.body`` is capped.""" + import json as _json + + from comfy_cli.comfy_client import HTTPError + + body = _json.dumps({"padding": "x" * 3000, "error": "insufficient_scope: comfy-cloud:agent:write"}) + _install_cloud_stubs(monkeypatch, client_cls=_fake_client(submit_exc=HTTPError(403, "Forbidden", body))) + lines, _ = _cloud_capture(capsys, workflow_file, wait=False, timeout=5) + + err = _envelope(lines)["error"] + assert err["code"] == "cloud_unauthorized" + assert err["details"]["required_scope"] == "comfy-cloud:agent:write" + assert len(err["details"]["body"]) == 2000 + @pytest.mark.parametrize("status", [400, 500, 503]) def test_submit_other_statuses_keep_cloud_http_error(self, monkeypatch, workflow_file, capsys, status): from comfy_cli.comfy_client import HTTPError @@ -2917,6 +2932,41 @@ def wait_for_completion(self, prompt_id, **k): assert status == "error" assert error["code"] == "cloud_http_error" + def test_poll_403_insufficient_scope_records_the_relogin_reason(self, monkeypatch, workflow_file, capsys): + """The job record keeps the same re-login verdict the envelope renders.""" + from comfy_cli import jobs_state as jobs_state_mod + from comfy_cli.comfy_client import HTTPError + + base = _fake_client() + + class Denied(base): + def wait_for_completion(self, prompt_id, **k): + raise HTTPError( + 403, + "Forbidden", + "denied", + www_authenticate=( + 'Basic realm="x"', + 'Bearer error="insufficient_scope", scope="comfy-cloud:jobs:read"', + ), + ) + + _install_cloud_stubs(monkeypatch, client_cls=Denied) + writes: list[tuple] = [] + monkeypatch.setattr( + jobs_state_mod, "write", lambda state: writes.append((state.status, state.error)) or "/tmp/state.json" + ) + lines, exit_code = _cloud_capture(capsys, workflow_file, wait=True, timeout=5) + + assert exit_code == 1 + rendered = _envelope(lines)["error"] + status, error = writes[-1] + assert status == "error" + assert error["code"] == rendered["code"] == "cloud_unauthorized" + assert error["details"]["reason"] == "insufficient_scope" + assert error["details"]["required_scope"] == "comfy-cloud:jobs:read" + assert error["details"]["prompt_id"] == "cloud-pid" + # --------------------------------------------------------------------------- # --output-node-ids → partial_execution_targets