Skip to content

Add CORS Logging Middleware and Fix CORS Configuration#32

Open
KnellBalm wants to merge 2 commits intomainfrom
cors-fix-and-logging-8824180182849832873
Open

Add CORS Logging Middleware and Fix CORS Configuration#32
KnellBalm wants to merge 2 commits intomainfrom
cors-fix-and-logging-8824180182849832873

Conversation

@KnellBalm
Copy link
Copy Markdown
Owner

@KnellBalm KnellBalm commented Feb 7, 2026

Added CORSLoggingMiddleware to backend/common/middleware.py and registered it in backend/main.py to debug production CORS issues. Verified that the cloud origin regex and allowed origins list are correct using tests/test_cors_config.py. This change also triggers a redeployment which might resolve stale configuration issues.


PR created automatically by Jules for task 8824180182849832873 started by @KnellBalm

Summary by Sourcery

Add middleware to log CORS-related request and response details to aid debugging of CORS issues in production.

Enhancements:

  • Introduce CORSLoggingMiddleware to capture origin and CORS header information on incoming requests.
  • Register the CORS logging middleware in the main application so CORS requests are logged in production.

…nfig

Co-authored-by: KnellBalm <90038472+KnellBalm@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 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.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Feb 7, 2026

Reviewer's Guide

Adds a CORS logging middleware to observe and debug CORS behavior in production and wires it into the FastAPI app after the existing CORS middleware, without changing the underlying CORS config itself.

Sequence diagram for CORS request handling with CORSLoggingMiddleware

sequenceDiagram
    actor Browser
    participant FastAPIApp
    participant CORSLoggingMiddleware
    participant CORSMiddleware
    participant EndpointHandler

    Browser->>FastAPIApp: HTTP request with Origin header
    FastAPIApp->>CORSLoggingMiddleware: dispatch(request, call_next)
    CORSLoggingMiddleware->>CORSMiddleware: call_next(request)
    CORSMiddleware->>EndpointHandler: Process request
    EndpointHandler-->>CORSMiddleware: Response
    CORSMiddleware-->>CORSLoggingMiddleware: Response with CORS headers
    CORSLoggingMiddleware->>CORSLoggingMiddleware: Read Origin and CORS headers
    alt access_control_allow_origin present
        CORSLoggingMiddleware->>CORSLoggingMiddleware: logger.info CORS Success
    else access_control_allow_origin missing
        CORSLoggingMiddleware->>CORSLoggingMiddleware: logger.warning CORS Missing Header
    end
    CORSLoggingMiddleware-->>FastAPIApp: Final response
    FastAPIApp-->>Browser: HTTP response with CORS headers
Loading

Class diagram for new CORSLoggingMiddleware

classDiagram
    class BaseHTTPMiddleware {
    }

    class CORSLoggingMiddleware {
        +dispatch(request, call_next)
    }

    BaseHTTPMiddleware <|-- CORSLoggingMiddleware
Loading

File-Level Changes

Change Details Files
Introduce CORSLoggingMiddleware to log CORS-related request/response details for debugging.
  • Add CORSLoggingMiddleware class extending BaseHTTPMiddleware to inspect requests with an Origin header
  • Log successful CORS responses (when Access-Control-Allow-Origin is present) at info level with origin and path
  • Log missing CORS headers at warning level including origin, path, method, and response status
backend/common/middleware.py
Register CORSLoggingMiddleware after existing CORS middleware in app setup.
  • Add CORSLoggingMiddleware to the FastAPI app so it wraps the existing CORSMiddleware and sees requests first
  • Keep existing CORS configuration (origins, regex, methods, headers) unchanged while enabling logging
backend/main.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

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

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:

  • CORSLoggingMiddleware calls get_logger on every request; consider instantiating the logger once at module or class level to avoid repeated lookups on the hot path.
  • Logging every successful CORS response at info level may be too noisy in production; you might want to downgrade success logs to debug and keep warning only for missing headers.
  • Since CORSLoggingMiddleware logs the Origin header for all requests with an origin, verify that this doesn’t expose sensitive or high-volume data in your central logs and consider adding sampling or filtering if needed.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- `CORSLoggingMiddleware` calls `get_logger` on every request; consider instantiating the logger once at module or class level to avoid repeated lookups on the hot path.
- Logging every successful CORS response at `info` level may be too noisy in production; you might want to downgrade success logs to `debug` and keep `warning` only for missing headers.
- Since `CORSLoggingMiddleware` logs the `Origin` header for all requests with an origin, verify that this doesn’t expose sensitive or high-volume data in your central logs and consider adding sampling or filtering if needed.

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.

…ests

Co-authored-by: KnellBalm <90038472+KnellBalm@users.noreply.github.com>
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