Skip to content

fix: evaluate TRUSTED_PROXY_COUNT dynamically inside function body - #372

Closed
google-labs-jules[bot] wants to merge 0 commit into
mainfrom
fix/trusted-proxy-late-binding-917366035391238728
Closed

fix: evaluate TRUSTED_PROXY_COUNT dynamically inside function body#372
google-labs-jules[bot] wants to merge 0 commit into
mainfrom
fix/trusted-proxy-late-binding-917366035391238728

Conversation

@google-labs-jules

@google-labs-jules google-labs-jules Bot commented May 28, 2026

Copy link
Copy Markdown

This PR fixes a bug related to late-binding of dynamic configurations during testing.

Fix

  • Modified extract_client_ip_from_forwarded in backend/src/agent/security.py so that trusted_proxy_count uses None as its default instead of evaluating the module-level constant at import time. The value is now safely evaluated within the function block, enabling pytest to successfully modify it via monkeypatch.
  • Updated multiple tests across test_api_security.py, test_rate_limiter_proxy.py, and test_proxy_security.py to correctly apply monkeypatching.
  • Corrected test logic in test_spoofing_vulnerability to correctly intercept fallback mocking mechanisms.
  • Cleaned up internal testing scripts to ensure repo hygiene.

PR created automatically by Jules for task 917366035391238728 started by @MasumRab

Summary by Sourcery

Adjust client IP extraction to evaluate trusted proxy configuration at call time and align tests with dynamic proxy settings and updated spoofing behavior.

Bug Fixes:

  • Defer evaluation of the trusted proxy count in client IP extraction to respect runtime configuration changes.
  • Correct spoofing vulnerability test expectations to align with fallback to the mocked client connection IP.

Tests:

  • Update rate limiter and proxy security tests to monkeypatch trusted proxy settings explicitly and validate behavior with dynamic configuration.

@google-labs-jules

Copy link
Copy Markdown
Author

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@trunk-io

trunk-io Bot commented May 28, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@sourcery-ai

sourcery-ai Bot commented May 28, 2026

Copy link
Copy Markdown

Reviewer's Guide

Refactors client IP extraction to evaluate TRUSTED_PROXY_COUNT at call time instead of import time, and updates proxy-related rate limiting tests to monkeypatch security configuration explicitly while correcting a spoofing test’s expectations.

Sequence diagram for dynamic TRUSTED_PROXY_COUNT evaluation in extract_client_ip_from_forwarded

sequenceDiagram
    actor Test
    participant monkeypatch
    participant security_module
    participant extract_client_ip_from_forwarded

    Test->>monkeypatch: set TRUSTED_PROXY_COUNT
    Test->>extract_client_ip_from_forwarded: call(forwarded, trusted_proxy_count=None)
    activate extract_client_ip_from_forwarded
    extract_client_ip_from_forwarded->>security_module: read TRUSTED_PROXY_COUNT
    security_module-->>extract_client_ip_from_forwarded: TRUSTED_PROXY_COUNT (patched value)
    extract_client_ip_from_forwarded-->>Test: client_ip
    deactivate extract_client_ip_from_forwarded
Loading

File-Level Changes

Change Details Files
Evaluate TRUSTED_PROXY_COUNT lazily inside client IP extraction helper to support dynamic test monkeypatching.
  • Changed the extract_client_ip_from_forwarded signature to accept trusted_proxy_count: int
None with a default of None instead of binding the module-level constant at import time.
  • Added an internal initialization that replaces a None trusted_proxy_count with the current TRUSTED_PROXY_COUNT value before processing.
  • Kept the rest of the trust-bound extraction algorithm unchanged to avoid behavioral regressions.
  • Update rate limiter and proxy security tests to monkeypatch security configuration and align expectations with current IP sanitization behavior.
    • Added pytest monkeypatch fixtures to tests that depend on trusted proxy configuration and set agent.security.TRUSTED_PROXY_COUNT to 1 where needed.
    • Adjusted rate limiter truncation test to expect the fallback mocked client IP (127.0.0.1) instead of the literal string 'unknown'.
    • Updated spoofing vulnerability test to monkeypatch TRUSTED_PROXIES rather than relying on indirect mocking behavior.
    • Ensured proxy-trusting tests explicitly configure TRUSTED_PROXY_COUNT when asserting X-Forwarded-For behavior.
    • Modified FastAPI API security test to inject TRUSTED_PROXY_COUNT via monkeypatch before constructing rate-limited middleware.
    backend/tests/agent/test_rate_limiter_proxy.py
    backend/tests/test_proxy_security.py
    backend/tests/agent/test_api_security.py

    Tips and commands

    Interacting with Sourcery

    • Trigger a new review: Comment @sourcery-ai review on the pull request.
    • Continue discussions: Reply directly to Sourcery's review comments.
    • Generate a GitHub issue from a review comment: Ask Sourcery to create an
      issue from a review comment by replying to it. You can also reply to a
      review comment with @sourcery-ai issue to create an issue from it.
    • Generate a pull request title: Write @sourcery-ai anywhere in the pull
      request title to generate a title at any time. You can also comment
      @sourcery-ai title on the pull request to (re-)generate the title at any time.
    • Generate a pull request summary: Write @sourcery-ai summary anywhere in
      the pull request body to generate a PR summary at any time exactly where you
      want it. You can also comment @sourcery-ai summary on the pull request to
      (re-)generate the summary at any time.
    • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
      request to (re-)generate the reviewer's guide at any time.
    • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
      pull request to resolve all Sourcery comments. Useful if you've already
      addressed all the comments and don't want to see them anymore.
    • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
      request to dismiss all existing Sourcery reviews. Especially useful if you
      want to start fresh with a new review - don't forget to comment
      @sourcery-ai review to trigger a new review!

    Customizing Your Experience

    Access your dashboard to:

    • Enable or disable review features such as the Sourcery-generated pull request
      summary, the reviewer's guide, and others.
    • Change the review language.
    • Add, remove or edit custom review instructions.
    • Adjust other review settings.

    Getting Help

    @sourcery-ai sourcery-ai Bot left a comment

    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Hey - I've left some high level feedback:

    • The repeated import agent.security + monkeypatch.setattr(..., 'TRUSTED_PROXY_COUNT', 1) setup across multiple tests could be consolidated into a shared fixture (e.g., trusted_proxy_count_1) to reduce duplication and keep test intent clearer.
    • In extract_client_ip_from_forwarded, consider documenting in the docstring that trusted_proxy_count=None means "use the global TRUSTED_PROXY_COUNT" so callers understand the special meaning of None from the type signature.
    Prompt for AI Agents
    Please address the comments from this code review:
    
    ## Overall Comments
    - The repeated `import agent.security` + `monkeypatch.setattr(..., 'TRUSTED_PROXY_COUNT', 1)` setup across multiple tests could be consolidated into a shared fixture (e.g., `trusted_proxy_count_1`) to reduce duplication and keep test intent clearer.
    - In `extract_client_ip_from_forwarded`, consider documenting in the docstring that `trusted_proxy_count=None` means "use the global TRUSTED_PROXY_COUNT" so callers understand the special meaning of `None` from the type signature.

    Sourcery is free for open source - if you like our reviews please consider sharing them ✨
    Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

    @MasumRab
    MasumRab enabled auto-merge (squash) May 30, 2026 08:13
    @MasumRab

    Copy link
    Copy Markdown
    Owner

    /gemini review

    @gemini-code-assist gemini-code-assist Bot left a comment

    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Code Review

    This pull request refactors the extract_client_ip_from_forwarded function in backend/src/agent/security.py to accept an optional trusted_proxy_count parameter, defaulting to TRUSTED_PROXY_COUNT when it is None. To support this change and ensure consistent behavior, several rate-limiting and proxy security tests have been updated to use monkeypatch to mock TRUSTED_PROXY_COUNT and TRUSTED_PROXIES. There are no review comments, and I have no feedback to provide.

    @google-labs-jules

    Copy link
    Copy Markdown
    Author

    /gemini review

    I have consolidated the repeated monkeypatch setup into a shared fixture mock_trusted_proxy_count_1 in backend/tests/conftest.py to keep the test intent clearer. I also updated the extract_client_ip_from_forwarded docstring to clarify that passing trusted_proxy_count=None implies using the global TRUSTED_PROXY_COUNT.

    @MasumRab
    MasumRab force-pushed the fix/trusted-proxy-late-binding-917366035391238728 branch from 76d3a0b to f2f4fe1 Compare June 9, 2026 18:11
    @mergify

    mergify Bot commented Jun 9, 2026

    Copy link
    Copy Markdown

    ⚠️ The sha of the head commit of this PR conflicts with #370. Mergify cannot evaluate rules on this PR. Once #370 is merged or closed, Mergify will resume processing this PR. ⚠️

    @MasumRab
    MasumRab force-pushed the fix/trusted-proxy-late-binding-917366035391238728 branch from f2f4fe1 to 76d3a0b Compare June 9, 2026 18:41
    @MasumRab MasumRab closed this Jun 9, 2026
    auto-merge was automatically disabled June 9, 2026 18:50

    Pull request was closed

    @MasumRab
    MasumRab force-pushed the fix/trusted-proxy-late-binding-917366035391238728 branch from 76d3a0b to 4e4b8da Compare June 9, 2026 18:50
    @sonarqubecloud

    sonarqubecloud Bot commented Jun 9, 2026

    Copy link
    Copy Markdown

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    None yet

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    1 participant