Repository navigation
fix: per-request settings clone in bitbucket-server & azuredevops webhooks (#2345) - #2351
Conversation
PR Summary by QodoFix settings state leak by cloning per-request settings in ADO/Bitbucket webhooks
AI Description
Diagram
High-Level Assessment
Files changed (3)
|
Code Review by Qodo
1. Clone before signature check
|
|
PR Description updated to latest commit (fc8ab42) |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨
|
Looked into the BackgroundTask concern — false positiveThe auto @app.post("/webhook")
async def handle_webhook(background_tasks: BackgroundTasks, request: Request):
data = await request.json()
context["settings"] = f"clone-for-{data['repo']}"
background_tasks.add_task(check, data["repo"])
Two sequential requests:
request 'A' → context['settings'] = 'clone-for-A' ✅
request 'B' → context['settings'] = 'clone-for-B' ✅
starlette_context uses a ContextVar and the middleware wraps the request with with request_cycle_context(...).
Starlette's BackgroundTask is awaited inside Response.__call__, before the middleware's with block exits — so
background tasks see the per-request clone, not the global singleton. Same reason the 5 existing webhook servers
using this pattern work fine.
No code change needed. |
|
Hey @alonz101 — just flagging that #2346 also targets issue #2345, but at the config-loader layer rather than per-webhook. Different trade-off:
Posting on both so maintainers see the full picture and can pick whichever shape fits the project better. No competitive intent — leak is annoying either way it gets fixed. |
|
Code review by qodo was updated up to the latest commit fc8ab42 |
…hooks (The-PR-Agent#2345) apply_repo_settings() mutates the module-level global_settings singleton. In long-lived webhook servers, .pr_agent.toml from a previously-reviewed repo leaked into PRs from other repos. 5 of 7 servers already cloned global_settings into context["settings"] per request - this aligns the last 2. Adds regression tests including the unknown-section case.
fc8ab42 to
c4e868c
Compare
|
Code review by qodo was updated up to the latest commit c4e868c |
Address Qodo review on The-PR-Agent#2351: move the per-request context['settings'] clone below the webhook signature verification and connection-test early return, so rejected/unauthenticated requests don't pay the deepcopy cost. The clone still precedes apply_repo_settings() (which mutates get_settings()), preserving per-request isolation. Azure DevOps already gates auth via a route dependency, so its clone is already post-auth and unchanged. Co-authored-by: alonz101 <alonz101@users.noreply.github.com>
|
Code review by qodo was updated up to the latest commit 981e2a3 |
|
Thanks @alonz101 !! |
…nt#2345) Complements the per-request settings clone merged in The-PR-Agent#2351. That fix isolates every webhook server; this one covers callers that run many PRs in one process WITHOUT a per-request clone — notably github_polling — where a previous repo's .pr_agent.toml would otherwise persist in the shared global_settings when the next repo has no config file (the loader early-exits on empty repo settings). Approach: note_repo_setting_override() records the pre-override value of each key a repo/extra .pr_agent.toml writes; reset_repo_settings_overrides(), called at the top of apply_repo_settings(), reverts exactly those keys. Only repo-settings-managed keys are touched, so runtime/base config set outside the merge (config.extra_config_url, config.is_auto_command, ...) is preserved. Details: - Record/revert go through get_settings(), so a per-request context['settings'] clone is handled as well as global_settings. - Revert rebuilds each affected section once: Dynaconf's unset() cannot drop a nested key, and the rebuild preserves sibling keys not in the ledger. - set_claude_model() also rewrites config.model_weak / config.fallback_models; those are recorded too so the claude shorthand doesn't leak derived keys. - Record/revert are guarded by an RLock. Tests: tests/unittest/test_apply_repo_settings.py keeps the clone-based cases from The-PR-Agent#2351 and adds a class covering the no-clone path: leak reverted, runtime flags preserved, non-repo config preserved, revert targets the effective settings object, and no claude-shorthand model-key leak. Full unit suite: 1732 passed. Co-authored-by: Tomasz Haratynowicz <75249676+tharatynowicz@users.noreply.github.com>
User description
Summary
Fixes #2345 —
apply_repo_settings()leaking.pr_agent.tomlstate across PRs from different repositories.Root cause
global_settingsis a module-level Dynaconf singleton.apply_repo_settings()mutates it without resetting between requests, so settings loaded for one repo persist into subsequent reviews of unrelated repos in the same long-lived webhook process.Fix
5 of 7 webhook servers already isolate per-request settings with
context["settings"] = copy.deepcopy(global_settings)(github_app, gitlab_webhook, gitea_app, bitbucket_app, gerrit_server). This PR aligns the last 2 (bitbucket_server_webhook,azuredevops_server_webhook) with that existing pattern.get_settings()already preferscontext["settings"]overglobal_settings, so the per-request clone is sufficient.Tests
tests/unittest/test_apply_repo_settings.pycovers:[pr_reviewer]extra_instructions don't leak into a subsequent Repo B request that has no.pr_agent.toml[my_custom_repo_section]) doesn't leak eitherTest plan
PYTHONPATH=. .venv/bin/pytest tests/unittest/test_apply_repo_settings.py -v(3 passed)PYTHONPATH=. .venv/bin/pytest tests/unittest -q(297 passed, no regressions)pre-commit run --files <changed>(clean)PR Type
Bug fix, Tests
Description
Fix
.pr_agent.tomlstate leaking across PRs from different repositoriesAdd
context["settings"] = copy.deepcopy(global_settings)per-request isolation tobitbucket_server_webhookandazuredevops_server_webhookhandlersAdd regression tests covering settings isolation, unknown section leaks, and TOML application
Minor whitespace/formatting cleanup in both webhook server files
Diagram Walkthrough
File Walkthrough
azuredevops_server_webhook.py
Add per-request settings clone to Azure DevOps webhook handlerpr_agent/servers/azuredevops_server_webhook.py
import copyand importedcontextfromstarlette_contextandglobal_settingsfromconfig_loadercontext["settings"] = copy.deepcopy(global_settings)at thestart of
handle_webhook()to isolate per-request settingsfunctions)
bitbucket_server_webhook.py
Add per-request settings clone to Bitbucket Server webhook handlerpr_agent/servers/bitbucket_server_webhook.py
import copyand importedcontextfromstarlette_contextandglobal_settingsfromconfig_loadercontext["settings"] = copy.deepcopy(global_settings)at thestart of
handle_webhook()to isolate per-request settingsfunctions)
BitbucketServerProvidertest_apply_repo_settings.py
Add regression tests for cross-repo settings isolationtests/unittest/test_apply_repo_settings.py
FakeGitProviderstub andfresh_global_settingsfixture
test_repo_settings_from_toml_are_applied: verifies TOML settings areapplied within a request context
test_repo_without_toml_does_not_inherit_previous_repo_settings:verifies repo A's settings don't leak into repo B with no
.pr_agent.tomltest_unknown_section_does_not_leak_to_next_repo: verifiescustom/unknown TOML sections don't persist across requests