Clearer diagnostic when an API Key SID is passed without `account_sid`
Maintainer thường phản hồi trong vòng 1 ngày
Đá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
- 82/100
Hướng nghiên cứu
Bắt đầu với twilio/base/client_base.py tại phần fallback của account_sid và xem lại các kiểm thử đơn vị liên quan trong tests/unit. Bổ sung kiểm thử cho trường hợp API Key SID không có account_sid, đồng thời kiểm tra rằng account_sid được chỉ định tường minh và thiết lập token hiện có vẫn hoạt động. Công việc hoàn tất khi trường hợp thiếu Account SID đưa ra lỗi có hướng xử lý và các kiểm thử liên quan đều đạt.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Location (twilio/twilio-python @ default branch 74b9629):
self.account_sid = account_sid or self.username
Problem
The README shows API-key authentication as Client(api_key, api_secret, account_sid). If the third argument is forgotten, e.g. Client("SKxxxx...", "secret"), the constructor accepts it and ClientBase.__init__ falls back to self.account_sid = account_sid or self.username, so the API Key SID is used as the account SID in account-scoped resource paths. The fallback is silent: there is no validation or hint in the constructor, and the mistake only surfaces later as an authentication/not-found error from the API, which does not point at the missing account_sid.
Reproduction
from twilio.rest import Client
client = Client("SK" + "a" * 32, "my-api-secret") # no error
print(client.account_sid) # 'SKaaaa...'
client.messages.create(...) # fails later with an API error
Environment:
- Commit:
74b96296b0805986252b798bda4bca90341487ee(main, twilio 9.12.0) - Toolchain: Python 3.13.18
Workaround: always pass account_sid= explicitly when authenticating with API keys.
Already fixed in twilio/twilio-node
PR: https://github.com/twilio/twilio-node/pull/828 — "fix: give more descriptive error for accountSid init issues" (merged 2022-11-28T17:54:36Z, merge commit e18440d317c). When the SID used as accountSid starts with SK, the constructor error now says that an API Key requires the accountSid to be passed as an additional option. twilio-node already rejects non-AC account SIDs; that check is not being proposed here, only the API Key hint.
How twilio-node fixed it:
--- a/lib/base/BaseTwilio.ts
+++ b/lib/base/BaseTwilio.ts
@@ -88,7 +88,11 @@ export class BaseTwilio {
...
if (!this.accountSid?.startsWith("AC")) {
- throw new Error("accountSid must start with AC");
+ const apiKeyMsg = this.accountSid.startsWith("SK")
+ ? ". The given SID indicates an API Key which requires the accountSid to be passed as an additional option"
+ : "";
+
+ throw new Error("accountSid must start with AC" + apiKeyMsg);
Suggested fix
Mirroring twilio-node's API Key hint, ClientBase.__init__ raises a TwilioException with an actionable message when username starts with SK and neither account_sid nor a credential_provider is given; no AC prefix is enforced and other setups are unchanged:
diff --git a/twilio/base/client_base.py b/twilio/base/client_base.py
--- a/twilio/base/client_base.py
+++ b/twilio/base/client_base.py
@@ -4,6 +4,7 @@ from typing import Dict, List, MutableMapping, Optional, Tuple
from urllib.parse import urlparse, urlunparse
from twilio import __version__
+from twilio.base.exceptions import TwilioException
from twilio.http import HttpClient
from twilio.http.http_client import TwilioHttpClient
from twilio.http.response import Response
@@ -54,6 +55,17 @@ class ClientBase(object):
self.credential_provider = credential_provider or None
""" :type : CredentialProvider """
+ if (
+ not account_sid
+ and not self.credential_provider
+ and self.username
+ and self.username.startswith("SK")
+ ):
+ raise TwilioException(
+ "The given username indicates an API Key SID, which requires "
+ "your Account SID to be passed via account_sid"
+ )
+
self.account_sid = account_sid or self.username
""" :type : str """
self.auth = (self.username, self.password)
Verified on 74b9629 (Python 3.13.18, pytest 9.2.0): a unit test for the reproduction above (Client("SK...", "my-api-secret") must raise an error mentioning the API Key and account_sid, while Client("SK...", secret, account_sid="AC...") and Client("AC...", token) keep working) fails without this patch and passes with it; no other test results change in tests/unit.
I can submit a PR for this.
Checked: #259 / #286 (2016) were about API-key auth not being supported/how to pass the account SID; they predate the current account_sid parameter and don't cover diagnosing the omission.
- Ngôn ngữ chính
- Python
- Star
- 2.1k
- Fork
- 821
- Merge trung bình
- 9 giờ 33 phút
- Pull request đã merge (30 ngày)
- 3
Chuẩn bị môi trường
- Có Dockerfile hoặc tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của twilio/twilio-python
-
Docs: "Handling Exceptions" should mention that network errors are not `TwilioRestException`Có thể đã có người làm @henricop đã nhận 13 ngày trước. Đang mở
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 90/100
twilio/twilio-python#986 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
TokenPagination.next_page_async() makes a sync request and fails with an async http clientCó thể đã có người làm @sebxt đã nhận 31 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
twilio/twilio-python#976 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
AsyncTwilioHttpClient does not JSON-serialize request bodies for application/json endpoints (sync client does) → 400 on JSON-body v2 endpointsCó thể đã có người làm @Steve0x2a đã nhận 77 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
twilio/twilio-python#932 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
twilio/twilio-python#930 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG] AsyncTwilioHttpClient.request does not use self.timeoutCó thể đã có người làm @Hashim1999164 đã nhận 96 ngày trước. Đang mởtype: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
twilio/twilio-python#925 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của twilio/twilio-python
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Harden tools/pr_threads.sh: pass all printed thread fields through the deterministic sanitizerĐang mởarea:cli area:security enhancement priority:medium size:small
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
RonaldHensbergen/composable-data-stack#1338 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area:stdlib effort:S impact:med platform:linux type:bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày
-
automated endpoint-dead needs-curation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
almanac-data/climate-almanac#139 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
UKGovernmentBEIS/inspect_ai#6695 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày