[BUG] Device-flow login raises JSONDecodeError or KeyError instead of the token endpoint HTTP error

Đang mở
#9,818 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@Kiko0301 đang làm issue này rồi.

Từ ngày 26/11/2026.

  • #9823 của @Kiko0301 — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
authentication, backend

Hướng nghiên cứu

Start with lib/crewai-core/src/crewai_core/auth/oauth2.py and inspect AuthenticationCommand._poll_for_token, which the issue identifies as the failing poll loop. Check existing tests for device-flow token polling and cover non-200 responses with invalid JSON and JSON lacking an error field. Done means these responses raise an httpx.HTTPError that reports the endpoint status or OAuth error; an open linked pull request #9823 indicates work is already underway.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Description

The device-flow poll loop in lib/crewai-core/src/crewai_core/auth/oauth2.py (AuthenticationCommand._poll_for_token) reads the response body before looking at the status code, and then indexes token_data["error"] unconditionally:

        while time.monotonic() < deadline:
            response = httpx.post(
                self.oauth2_provider.get_token_url(), data=token_payload, timeout=30
            )
            token_data = response.json()

            if response.status_code == 200:
                ...
            if token_data["error"] == "expired_token":
                break
            if token_data["error"] not in ("authorization_pending", "slow_down"):
                raise httpx.HTTPError(
                    token_data.get("error_description") or token_data.get("error")
                )

The loop runs for the lifetime of the device code (minutes). During that window the endpoint can answer with something that is not an OAuth device-flow answer:

  • A proxy or gateway in front of the IdP answers 502 with an HTML page: response.json() raises json.JSONDecodeError: Expecting value: line 1 column 1 (char 0), which is raised out of crewai login with no indication that the token endpoint returned 502.
  • A rate limiter answers 429 with JSON that has no error member (for example {"message": "rate limited"}): token_data["error"] raises KeyError: 'error'.

In both cases the real failure (an HTTP error from the endpoint) is replaced by a parsing error from the wrong response shape.

Steps to Reproduce

from unittest.mock import MagicMock, patch

import httpx
from crewai_cli.authentication.main import AuthenticationCommand

command = AuthenticationCommand.__new__(AuthenticationCommand)
command.oauth2_provider = MagicMock()
command.oauth2_provider.get_token_url.return_value = "https://idp.example.com/token"
command.oauth2_provider.get_client_id.return_value = "client"

response = MagicMock()
response.status_code = 502
response.json.side_effect = ValueError("Expecting value: line 1 column 1 (char 0)")

with patch("crewai_core.auth.oauth2.httpx.post", return_value=response):
    command._poll_for_token({"device_code": "d", "expires_in": 60, "interval": 0})

Actual: ValueError: Expecting value: line 1 column 1 (char 0). With a 429 whose body is {"message": "rate limited"}: KeyError: 'error'.

Expected Behavior

Only a 200 is a device-flow answer; read the body in that branch. For any other status, take the OAuth error from a body that has one, and when it has none raise an httpx.HTTPError that names the HTTP status, so crewai login reports what the endpoint returned rather than a JSON/KeyError from a body it did not expect.

AI assistance

I used an AI coding agent to investigate, reproduce, and prepare this report, and reviewed its results. Per CONTRIBUTING.md, please apply the llm-generated label; external contributors cannot add it with my current permissions.

Ngôn ngữ chính
Python
Star
61.2k
Fork
9.2k
Merge trung bình
1 ngày 14 giờ
Pull request đã merge (30 ngày)
180

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của crewAIInc/crewAI

Tất cả issue của crewAIInc/crewAI

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.