feat: allow caller data in request user agents - #11047
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/poetry/utils/user_agent.py" line_range="12-15" />
<code_context>
+ extras: list[tuple[str, str]] = []
+ user_data = os.environ.get("POETRY_USER_AGENT_USER_DATA")
+ if user_data is not None:
+ extras.append(("user_data", user_data))
+
+ return requests_user_agent("poetry", __version__, extras=extras)
</code_context>
<issue_to_address>
**issue (bug_risk):** `requests_toolbelt.user_agent` expects `extras` to contain strings such as `"user_data/build/42"`, but this code appends a tuple. When `POETRY_USER_AGENT_USER_DATA` is set, User-Agent construction raises `TypeError` instead of returning a header, breaking repository requests and uploads.
**Triggers:** When `POETRY_USER_AGENT_USER_DATA` is set.
**Suggested fix:** Append `f"user_data/{user_data}"` as a string rather than the tuple `("user_data", user_data)`.
```suggestion
extras: list[str] = []
user_data = os.environ.get("POETRY_USER_AGENT_USER_DATA")
if user_data is not None:
extras.append(f"user_data/{user_data}")
```
</issue_to_address>Sourcery assessment
Needs a human reviewer. 1 finding to address first, and if the configured value is sensitive or malformed, Poetry will send it to package repositories in download and upload request headers, and those disclosures cannot be undone by reverting the change. The behavior is opt-in and future exposure stops on revert, but already transmitted caller data cannot be recalled.
Blocking findings: src/poetry/utils/user_agent.py:15
| extras: list[tuple[str, str]] = [] | ||
| user_data = os.environ.get("POETRY_USER_AGENT_USER_DATA") | ||
| if user_data is not None: | ||
| extras.append(("user_data", user_data)) |
There was a problem hiding this comment.
issue (bug_risk): requests_toolbelt.user_agent expects extras to contain strings such as "user_data/build/42", but this code appends a tuple. When POETRY_USER_AGENT_USER_DATA is set, User-Agent construction raises TypeError instead of returning a header, breaking repository requests and uploads.
Triggers: When POETRY_USER_AGENT_USER_DATA is set.
Suggested fix: Append f"user_data/{user_data}" as a string rather than the tuple ("user_data", user_data).
| extras: list[tuple[str, str]] = [] | |
| user_data = os.environ.get("POETRY_USER_AGENT_USER_DATA") | |
| if user_data is not None: | |
| extras.append(("user_data", user_data)) | |
| extras: list[str] = [] | |
| user_data = os.environ.get("POETRY_USER_AGENT_USER_DATA") | |
| if user_data is not None: | |
| extras.append(f"user_data/{user_data}") |
|
The existing tuple is intentional: requests-toolbelt 1.0.0 documents |
44942e4 to
0aa0a3f
Compare
0aa0a3f to
4d59b92
Compare
Resolves: #11012
Summary
POETRY_USER_AGENT_USER_DATAto append caller context to Poetry's User-Agent.Validation
python -m pytest -o addopts='' -q tests/utils/test_authenticator.py tests/publishing/test_uploader.pypython -m ruff check src/poetry/utils/user_agent.py src/poetry/utils/authenticator.py src/poetry/publishing/uploader.py tests/utils/test_authenticator.py tests/publishing/test_uploader.pypython -m ruff format --check src/poetry/utils/user_agent.py src/poetry/utils/authenticator.py src/poetry/publishing/uploader.py tests/utils/test_authenticator.py tests/publishing/test_uploader.pygit diff --checkAdded tests for changed code.
Updated documentation for changed code.